Move user related model into models/user (#17781)

* Move user related model into models/user

* Fix lint for windows

* Fix windows lint

* Fix windows lint

* Move some tests in models

* Merge
This commit is contained in:
Lunny Xiao 2021-11-24 17:49:20 +08:00 committed by GitHub
parent 4e7ca946da
commit a666829a37
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
345 changed files with 4230 additions and 3813 deletions

View file

@ -12,6 +12,7 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
"xorm.io/xorm"
@ -22,7 +23,6 @@ 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"`
@ -34,7 +34,6 @@ func init() {
// 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)
}
@ -42,10 +41,6 @@ func (l *LFSLock) BeforeInsert() {
// 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)
@ -58,7 +53,7 @@ func cleanPath(p string) string {
// CreateLFSLock creates a new lock.
func CreateLFSLock(lock *LFSLock) (*LFSLock, error) {
err := CheckLFSAccessForRepo(lock.Owner, lock.Repo, AccessModeWrite)
err := CheckLFSAccessForRepo(lock.OwnerID, lock.Repo, AccessModeWrite)
if err != nil {
return nil, err
}
@ -123,13 +118,13 @@ func CountLFSLockByRepoID(repoID int64) (int64, error) {
}
// DeleteLFSLockByID deletes a lock by given ID.
func DeleteLFSLockByID(id int64, u *User, force bool) (*LFSLock, error) {
func DeleteLFSLockByID(id int64, u *user_model.User, force bool) (*LFSLock, error) {
lock, err := GetLFSLockByID(id)
if err != nil {
return nil, err
}
err = CheckLFSAccessForRepo(u, lock.Repo, AccessModeWrite)
err = CheckLFSAccessForRepo(u.ID, lock.Repo, AccessModeWrite)
if err != nil {
return nil, err
}
@ -143,10 +138,14 @@ func DeleteLFSLockByID(id int64, u *User, force bool) (*LFSLock, error) {
}
// CheckLFSAccessForRepo check needed access mode base on action
func CheckLFSAccessForRepo(u *User, repo *Repository, mode AccessMode) error {
if u == nil {
func CheckLFSAccessForRepo(ownerID int64, repo *Repository, mode AccessMode) error {
if ownerID == 0 {
return ErrLFSUnauthorizedAction{repo.ID, "undefined", mode}
}
u, err := user_model.GetUserByID(ownerID)
if err != nil {
return err
}
perm, err := GetUserRepoPermission(repo, u)
if err != nil {
return err