mirror of
https://github.com/go-gitea/gitea
synced 2024-11-18 04:09:28 +01:00
9302eba971
* DBContext is just a Context This PR removes some of the specialness from the DBContext and makes it context This allows us to simplify the GetEngine code to wrap around any context in future and means that we can change our loadRepo(e Engine) functions to simply take contexts. Signed-off-by: Andrew Thornton <art27@cantab.net> * fix unit tests Signed-off-by: Andrew Thornton <art27@cantab.net> * another place that needs to set the initial context Signed-off-by: Andrew Thornton <art27@cantab.net> * avoid race Signed-off-by: Andrew Thornton <art27@cantab.net> * change attachment error Signed-off-by: Andrew Thornton <art27@cantab.net>
160 lines
4.1 KiB
Go
160 lines
4.1 KiB
Go
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package models
|
|
|
|
import (
|
|
"fmt"
|
|
"path"
|
|
"strings"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
// LFSLock represents a git lfs lock of repository.
|
|
type LFSLock struct {
|
|
ID int64 `xorm:"pk autoincr"`
|
|
Repo *Repository `xorm:"-"`
|
|
RepoID int64 `xorm:"INDEX NOT NULL"`
|
|
Owner *User `xorm:"-"`
|
|
OwnerID int64 `xorm:"INDEX NOT NULL"`
|
|
Path string `xorm:"TEXT"`
|
|
Created time.Time `xorm:"created"`
|
|
}
|
|
|
|
func init() {
|
|
db.RegisterModel(new(LFSLock))
|
|
}
|
|
|
|
// BeforeInsert is invoked from XORM before inserting an object of this type.
|
|
func (l *LFSLock) BeforeInsert() {
|
|
l.OwnerID = l.Owner.ID
|
|
l.RepoID = l.Repo.ID
|
|
l.Path = cleanPath(l.Path)
|
|
}
|
|
|
|
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
|
|
func (l *LFSLock) AfterLoad(session *xorm.Session) {
|
|
var err error
|
|
l.Owner, err = getUserByID(session, l.OwnerID)
|
|
if err != nil {
|
|
log.Error("LFS lock AfterLoad failed OwnerId[%d] not found: %v", l.OwnerID, err)
|
|
}
|
|
l.Repo, err = getRepositoryByID(session, l.RepoID)
|
|
if err != nil {
|
|
log.Error("LFS lock AfterLoad failed RepoId[%d] not found: %v", l.RepoID, err)
|
|
}
|
|
}
|
|
|
|
func cleanPath(p string) string {
|
|
return path.Clean("/" + p)[1:]
|
|
}
|
|
|
|
// CreateLFSLock creates a new lock.
|
|
func CreateLFSLock(lock *LFSLock) (*LFSLock, error) {
|
|
err := CheckLFSAccessForRepo(lock.Owner, lock.Repo, AccessModeWrite)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
lock.Path = cleanPath(lock.Path)
|
|
|
|
l, err := GetLFSLock(lock.Repo, lock.Path)
|
|
if err == nil {
|
|
return l, ErrLFSLockAlreadyExist{lock.RepoID, lock.Path}
|
|
}
|
|
if !IsErrLFSLockNotExist(err) {
|
|
return nil, err
|
|
}
|
|
|
|
_, err = db.GetEngine(db.DefaultContext).InsertOne(lock)
|
|
return lock, err
|
|
}
|
|
|
|
// GetLFSLock returns release by given path.
|
|
func GetLFSLock(repo *Repository, path string) (*LFSLock, error) {
|
|
path = cleanPath(path)
|
|
rel := &LFSLock{RepoID: repo.ID}
|
|
has, err := db.GetEngine(db.DefaultContext).Where("lower(path) = ?", strings.ToLower(path)).Get(rel)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !has {
|
|
return nil, ErrLFSLockNotExist{0, repo.ID, path}
|
|
}
|
|
return rel, nil
|
|
}
|
|
|
|
// GetLFSLockByID returns release by given id.
|
|
func GetLFSLockByID(id int64) (*LFSLock, error) {
|
|
lock := new(LFSLock)
|
|
has, err := db.GetEngine(db.DefaultContext).ID(id).Get(lock)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if !has {
|
|
return nil, ErrLFSLockNotExist{id, 0, ""}
|
|
}
|
|
return lock, nil
|
|
}
|
|
|
|
// GetLFSLockByRepoID returns a list of locks of repository.
|
|
func GetLFSLockByRepoID(repoID int64, page, pageSize int) ([]*LFSLock, error) {
|
|
sess := db.NewSession(db.DefaultContext)
|
|
defer sess.Close()
|
|
|
|
if page >= 0 && pageSize > 0 {
|
|
start := 0
|
|
if page > 0 {
|
|
start = (page - 1) * pageSize
|
|
}
|
|
sess.Limit(pageSize, start)
|
|
}
|
|
lfsLocks := make([]*LFSLock, 0, pageSize)
|
|
return lfsLocks, sess.Find(&lfsLocks, &LFSLock{RepoID: repoID})
|
|
}
|
|
|
|
// CountLFSLockByRepoID returns a count of all LFSLocks associated with a repository.
|
|
func CountLFSLockByRepoID(repoID int64) (int64, error) {
|
|
return db.GetEngine(db.DefaultContext).Count(&LFSLock{RepoID: repoID})
|
|
}
|
|
|
|
// DeleteLFSLockByID deletes a lock by given ID.
|
|
func DeleteLFSLockByID(id int64, u *User, force bool) (*LFSLock, error) {
|
|
lock, err := GetLFSLockByID(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = CheckLFSAccessForRepo(u, lock.Repo, AccessModeWrite)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !force && u.ID != lock.OwnerID {
|
|
return nil, fmt.Errorf("user doesn't own lock and force flag is not set")
|
|
}
|
|
|
|
_, err = db.GetEngine(db.DefaultContext).ID(id).Delete(new(LFSLock))
|
|
return lock, err
|
|
}
|
|
|
|
// CheckLFSAccessForRepo check needed access mode base on action
|
|
func CheckLFSAccessForRepo(u *User, repo *Repository, mode AccessMode) error {
|
|
if u == nil {
|
|
return ErrLFSUnauthorizedAction{repo.ID, "undefined", mode}
|
|
}
|
|
perm, err := GetUserRepoPermission(repo, u)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !perm.CanAccess(mode, UnitTypeCode) {
|
|
return ErrLFSUnauthorizedAction{repo.ID, u.DisplayName(), mode}
|
|
}
|
|
return nil
|
|
}
|