2017-11-28 13:58:37 -07:00
|
|
|
// 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"
|
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-28 04:58:28 -07:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2021-11-09 12:57:58 -07:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 02:49:20 -07:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2018-01-27 09:48:15 -07:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-08-23 10:40:30 -06:00
|
|
|
|
2019-10-17 03:26:49 -06:00
|
|
|
"xorm.io/xorm"
|
2017-11-28 13:58:37 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// LFSLock represents a git lfs lock of repository.
|
|
|
|
type LFSLock struct {
|
2018-01-27 09:48:15 -07:00
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
Repo *Repository `xorm:"-"`
|
|
|
|
RepoID int64 `xorm:"INDEX NOT NULL"`
|
|
|
|
OwnerID int64 `xorm:"INDEX NOT NULL"`
|
|
|
|
Path string `xorm:"TEXT"`
|
|
|
|
Created time.Time `xorm:"created"`
|
2017-11-28 13:58:37 -07:00
|
|
|
}
|
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(LFSLock))
|
|
|
|
}
|
|
|
|
|
2017-11-28 13:58:37 -07:00
|
|
|
// BeforeInsert is invoked from XORM before inserting an object of this type.
|
|
|
|
func (l *LFSLock) BeforeInsert() {
|
2018-01-27 09:48:15 -07:00
|
|
|
l.RepoID = l.Repo.ID
|
2017-11-28 13:58:37 -07:00
|
|
|
l.Path = cleanPath(l.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
|
2018-01-27 09:48:15 -07:00
|
|
|
func (l *LFSLock) AfterLoad(session *xorm.Session) {
|
|
|
|
var err error
|
|
|
|
l.Repo, err = getRepositoryByID(session, l.RepoID)
|
|
|
|
if err != nil {
|
2019-04-02 01:48:31 -06:00
|
|
|
log.Error("LFS lock AfterLoad failed RepoId[%d] not found: %v", l.RepoID, err)
|
2018-01-27 09:48:15 -07:00
|
|
|
}
|
2017-11-28 13:58:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func cleanPath(p string) string {
|
2019-12-12 06:18:07 -07:00
|
|
|
return path.Clean("/" + p)[1:]
|
2017-11-28 13:58:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateLFSLock creates a new lock.
|
|
|
|
func CreateLFSLock(lock *LFSLock) (*LFSLock, error) {
|
2021-11-28 04:58:28 -07:00
|
|
|
err := CheckLFSAccessForRepo(lock.OwnerID, lock.Repo, perm.AccessModeWrite)
|
2017-11-28 13:58:37 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-12 06:18:07 -07:00
|
|
|
lock.Path = cleanPath(lock.Path)
|
|
|
|
|
2018-01-27 09:48:15 -07:00
|
|
|
l, err := GetLFSLock(lock.Repo, lock.Path)
|
2017-11-28 13:58:37 -07:00
|
|
|
if err == nil {
|
|
|
|
return l, ErrLFSLockAlreadyExist{lock.RepoID, lock.Path}
|
|
|
|
}
|
|
|
|
if !IsErrLFSLockNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-23 09:45:36 -06:00
|
|
|
_, err = db.GetEngine(db.DefaultContext).InsertOne(lock)
|
2017-11-28 13:58:37 -07:00
|
|
|
return lock, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLFSLock returns release by given path.
|
2018-01-27 09:48:15 -07:00
|
|
|
func GetLFSLock(repo *Repository, path string) (*LFSLock, error) {
|
2017-11-28 13:58:37 -07:00
|
|
|
path = cleanPath(path)
|
2018-01-27 09:48:15 -07:00
|
|
|
rel := &LFSLock{RepoID: repo.ID}
|
2021-09-23 09:45:36 -06:00
|
|
|
has, err := db.GetEngine(db.DefaultContext).Where("lower(path) = ?", strings.ToLower(path)).Get(rel)
|
2017-11-28 13:58:37 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !has {
|
2018-01-27 09:48:15 -07:00
|
|
|
return nil, ErrLFSLockNotExist{0, repo.ID, path}
|
2017-11-28 13:58:37 -07:00
|
|
|
}
|
|
|
|
return rel, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLFSLockByID returns release by given id.
|
|
|
|
func GetLFSLockByID(id int64) (*LFSLock, error) {
|
|
|
|
lock := new(LFSLock)
|
2021-09-23 09:45:36 -06:00
|
|
|
has, err := db.GetEngine(db.DefaultContext).ID(id).Get(lock)
|
2017-11-28 13:58:37 -07:00
|
|
|
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.
|
2019-12-12 06:18:07 -07:00
|
|
|
func GetLFSLockByRepoID(repoID int64, page, pageSize int) ([]*LFSLock, error) {
|
2021-11-21 08:41:00 -07:00
|
|
|
e := db.GetEngine(db.DefaultContext)
|
2019-12-12 06:18:07 -07:00
|
|
|
if page >= 0 && pageSize > 0 {
|
|
|
|
start := 0
|
|
|
|
if page > 0 {
|
|
|
|
start = (page - 1) * pageSize
|
|
|
|
}
|
2021-11-21 08:41:00 -07:00
|
|
|
e.Limit(pageSize, start)
|
2019-12-12 06:18:07 -07:00
|
|
|
}
|
|
|
|
lfsLocks := make([]*LFSLock, 0, pageSize)
|
2021-11-21 08:41:00 -07:00
|
|
|
return lfsLocks, e.Find(&lfsLocks, &LFSLock{RepoID: repoID})
|
2019-12-12 06:18:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// CountLFSLockByRepoID returns a count of all LFSLocks associated with a repository.
|
|
|
|
func CountLFSLockByRepoID(repoID int64) (int64, error) {
|
2021-09-23 09:45:36 -06:00
|
|
|
return db.GetEngine(db.DefaultContext).Count(&LFSLock{RepoID: repoID})
|
2017-11-28 13:58:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteLFSLockByID deletes a lock by given ID.
|
2021-11-24 02:49:20 -07:00
|
|
|
func DeleteLFSLockByID(id int64, u *user_model.User, force bool) (*LFSLock, error) {
|
2017-11-28 13:58:37 -07:00
|
|
|
lock, err := GetLFSLockByID(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-28 04:58:28 -07:00
|
|
|
err = CheckLFSAccessForRepo(u.ID, lock.Repo, perm.AccessModeWrite)
|
2017-11-28 13:58:37 -07:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-09-23 09:45:36 -06:00
|
|
|
_, err = db.GetEngine(db.DefaultContext).ID(id).Delete(new(LFSLock))
|
2017-11-28 13:58:37 -07:00
|
|
|
return lock, err
|
|
|
|
}
|
|
|
|
|
2021-03-14 12:52:12 -06:00
|
|
|
// CheckLFSAccessForRepo check needed access mode base on action
|
2021-11-28 04:58:28 -07:00
|
|
|
func CheckLFSAccessForRepo(ownerID int64, repo *Repository, mode perm.AccessMode) error {
|
2021-11-24 02:49:20 -07:00
|
|
|
if ownerID == 0 {
|
2018-01-27 09:48:15 -07:00
|
|
|
return ErrLFSUnauthorizedAction{repo.ID, "undefined", mode}
|
2017-11-28 13:58:37 -07:00
|
|
|
}
|
2021-11-24 02:49:20 -07:00
|
|
|
u, err := user_model.GetUserByID(ownerID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-28 04:26:14 -07:00
|
|
|
perm, err := GetUserRepoPermission(repo, u)
|
2017-11-28 13:58:37 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-11-28 04:26:14 -07:00
|
|
|
}
|
2021-11-09 12:57:58 -07:00
|
|
|
if !perm.CanAccess(mode, unit.TypeCode) {
|
2018-01-27 09:48:15 -07:00
|
|
|
return ErrLFSUnauthorizedAction{repo.ID, u.DisplayName(), mode}
|
2017-11-28 13:58:37 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|