Move user functions into user.go (#17659)
* Move user functions into user.go * Fix test
This commit is contained in:
parent
6874fe90e5
commit
3c3855a05c
14 changed files with 101 additions and 72 deletions
18
models/admin/main_test.go
Normal file
18
models/admin/main_test.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
// Copyright 2020 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 admin
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
unittest.MainTest(m, filepath.Join("..", ".."),
|
||||
"notice.yml",
|
||||
)
|
||||
}
|
|
@ -1,11 +1,11 @@
|
|||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// Copyright 2021 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
|
||||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
|
@ -44,10 +44,11 @@ func (n *Notice) TrStr() string {
|
|||
|
||||
// CreateNotice creates new system notice.
|
||||
func CreateNotice(tp NoticeType, desc string, args ...interface{}) error {
|
||||
return createNotice(db.GetEngine(db.DefaultContext), tp, desc, args...)
|
||||
return CreateNoticeCtx(db.DefaultContext, tp, desc, args...)
|
||||
}
|
||||
|
||||
func createNotice(e db.Engine, tp NoticeType, desc string, args ...interface{}) error {
|
||||
// CreateNoticeCtx creates new system notice.
|
||||
func CreateNoticeCtx(ctx context.Context, tp NoticeType, desc string, args ...interface{}) error {
|
||||
if len(args) > 0 {
|
||||
desc = fmt.Sprintf(desc, args...)
|
||||
}
|
||||
|
@ -55,42 +56,43 @@ func createNotice(e db.Engine, tp NoticeType, desc string, args ...interface{})
|
|||
Type: tp,
|
||||
Description: desc,
|
||||
}
|
||||
_, err := e.Insert(n)
|
||||
return err
|
||||
return db.Insert(ctx, n)
|
||||
}
|
||||
|
||||
// CreateRepositoryNotice creates new system notice with type NoticeRepository.
|
||||
func CreateRepositoryNotice(desc string, args ...interface{}) error {
|
||||
return createNotice(db.GetEngine(db.DefaultContext), NoticeRepository, desc, args...)
|
||||
return CreateNoticeCtx(db.DefaultContext, NoticeRepository, desc, args...)
|
||||
}
|
||||
|
||||
// RemoveAllWithNotice removes all directories in given path and
|
||||
// creates a system notice when error occurs.
|
||||
func RemoveAllWithNotice(title, path string) {
|
||||
removeAllWithNotice(db.GetEngine(db.DefaultContext), title, path)
|
||||
RemoveAllWithNoticeCtx(db.DefaultContext, title, path)
|
||||
}
|
||||
|
||||
// RemoveStorageWithNotice removes a file from the storage and
|
||||
// creates a system notice when error occurs.
|
||||
func RemoveStorageWithNotice(bucket storage.ObjectStorage, title, path string) {
|
||||
removeStorageWithNotice(db.GetEngine(db.DefaultContext), bucket, title, path)
|
||||
removeStorageWithNotice(db.DefaultContext, bucket, title, path)
|
||||
}
|
||||
|
||||
func removeStorageWithNotice(e db.Engine, bucket storage.ObjectStorage, title, path string) {
|
||||
func removeStorageWithNotice(ctx context.Context, bucket storage.ObjectStorage, title, path string) {
|
||||
if err := bucket.Delete(path); err != nil {
|
||||
desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
|
||||
log.Warn(title+" [%s]: %v", path, err)
|
||||
if err = createNotice(e, NoticeRepository, desc); err != nil {
|
||||
if err = CreateNoticeCtx(ctx, NoticeRepository, desc); err != nil {
|
||||
log.Error("CreateRepositoryNotice: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func removeAllWithNotice(e db.Engine, title, path string) {
|
||||
// RemoveAllWithNoticeCtx removes all directories in given path and
|
||||
// creates a system notice when error occurs.
|
||||
func RemoveAllWithNoticeCtx(ctx context.Context, title, path string) {
|
||||
if err := util.RemoveAll(path); err != nil {
|
||||
desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
|
||||
log.Warn(title+" [%s]: %v", path, err)
|
||||
if err = createNotice(e, NoticeRepository, desc); err != nil {
|
||||
if err = CreateNoticeCtx(ctx, NoticeRepository, desc); err != nil {
|
||||
log.Error("CreateRepositoryNotice: %v", err)
|
||||
}
|
||||
}
|
||||
|
@ -142,16 +144,3 @@ func DeleteNoticesByIDs(ids []int64) error {
|
|||
Delete(new(Notice))
|
||||
return err
|
||||
}
|
||||
|
||||
// GetAdminUser returns the first administrator
|
||||
func GetAdminUser() (*User, error) {
|
||||
var admin User
|
||||
has, err := db.GetEngine(db.DefaultContext).Where("is_admin=?", true).Get(&admin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, ErrUserNotExist{}
|
||||
}
|
||||
|
||||
return &admin, nil
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package models
|
||||
package admin
|
||||
|
||||
import (
|
||||
"testing"
|
|
@ -5,6 +5,7 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
admin_model "code.gitea.io/gitea/models/admin"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
|
||||
"xorm.io/builder"
|
||||
|
@ -127,7 +128,7 @@ func DeleteOrphanedIssues() error {
|
|||
|
||||
// Remove issue attachment files.
|
||||
for i := range attachmentPaths {
|
||||
removeAllWithNotice(db.GetEngine(db.DefaultContext), "Delete issue attachment", attachmentPaths[i])
|
||||
admin_model.RemoveAllWithNoticeCtx(db.DefaultContext, "Delete issue attachment", attachmentPaths[i])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ import (
|
|||
|
||||
_ "image/jpeg" // Needed for jpeg support
|
||||
|
||||
admin_model "code.gitea.io/gitea/models/admin"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/models/unit"
|
||||
"code.gitea.io/gitea/models/webhook"
|
||||
|
@ -133,7 +134,7 @@ func NewRepoContext() {
|
|||
loadRepoConfig()
|
||||
unit.LoadUnitConfig()
|
||||
|
||||
RemoveAllWithNotice("Clean up repository temporary data", filepath.Join(setting.AppDataPath, "tmp"))
|
||||
admin_model.RemoveAllWithNotice("Clean up repository temporary data", filepath.Join(setting.AppDataPath, "tmp"))
|
||||
}
|
||||
|
||||
// RepositoryStatus defines the status of repository
|
||||
|
@ -1648,36 +1649,36 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
|
|||
|
||||
// Remove repository files.
|
||||
repoPath := repo.RepoPath()
|
||||
removeAllWithNotice(db.GetEngine(db.DefaultContext), "Delete repository files", repoPath)
|
||||
admin_model.RemoveAllWithNotice("Delete repository files", repoPath)
|
||||
|
||||
// Remove wiki files
|
||||
if repo.HasWiki() {
|
||||
removeAllWithNotice(db.GetEngine(db.DefaultContext), "Delete repository wiki", repo.WikiPath())
|
||||
admin_model.RemoveAllWithNotice("Delete repository wiki", repo.WikiPath())
|
||||
}
|
||||
|
||||
// Remove archives
|
||||
for i := range archivePaths {
|
||||
removeStorageWithNotice(db.GetEngine(db.DefaultContext), storage.RepoArchives, "Delete repo archive file", archivePaths[i])
|
||||
admin_model.RemoveStorageWithNotice(storage.RepoArchives, "Delete repo archive file", archivePaths[i])
|
||||
}
|
||||
|
||||
// Remove lfs objects
|
||||
for i := range lfsPaths {
|
||||
removeStorageWithNotice(db.GetEngine(db.DefaultContext), storage.LFS, "Delete orphaned LFS file", lfsPaths[i])
|
||||
admin_model.RemoveStorageWithNotice(storage.LFS, "Delete orphaned LFS file", lfsPaths[i])
|
||||
}
|
||||
|
||||
// Remove issue attachment files.
|
||||
for i := range attachmentPaths {
|
||||
RemoveStorageWithNotice(storage.Attachments, "Delete issue attachment", attachmentPaths[i])
|
||||
admin_model.RemoveStorageWithNotice(storage.Attachments, "Delete issue attachment", attachmentPaths[i])
|
||||
}
|
||||
|
||||
// Remove release attachment files.
|
||||
for i := range releaseAttachments {
|
||||
RemoveStorageWithNotice(storage.Attachments, "Delete release attachment", releaseAttachments[i])
|
||||
admin_model.RemoveStorageWithNotice(storage.Attachments, "Delete release attachment", releaseAttachments[i])
|
||||
}
|
||||
|
||||
// Remove attachment with no issue_id and release_id.
|
||||
for i := range newAttachmentPaths {
|
||||
RemoveStorageWithNotice(storage.Attachments, "Delete issue attachment", attachmentPaths[i])
|
||||
admin_model.RemoveStorageWithNotice(storage.Attachments, "Delete issue attachment", attachmentPaths[i])
|
||||
}
|
||||
|
||||
if len(repo.Avatar) > 0 {
|
||||
|
|
|
@ -22,6 +22,7 @@ import (
|
|||
|
||||
_ "image/jpeg" // Needed for jpeg support
|
||||
|
||||
admin_model "code.gitea.io/gitea/models/admin"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/models/login"
|
||||
"code.gitea.io/gitea/models/unit"
|
||||
|
@ -1148,7 +1149,8 @@ func deleteBeans(e db.Engine, beans ...interface{}) (err error) {
|
|||
return nil
|
||||
}
|
||||
|
||||
func deleteUser(e db.Engine, u *User) error {
|
||||
func deleteUser(ctx context.Context, u *User) error {
|
||||
e := db.GetEngine(ctx)
|
||||
// Note: A user owns any repository or belongs to any organization
|
||||
// cannot perform delete operation.
|
||||
|
||||
|
@ -1304,7 +1306,7 @@ func deleteUser(e db.Engine, u *User) error {
|
|||
path := UserPath(u.Name)
|
||||
if err = util.RemoveAll(path); err != nil {
|
||||
err = fmt.Errorf("Failed to RemoveAll %s: %v", path, err)
|
||||
_ = createNotice(e, NoticeTask, fmt.Sprintf("delete user '%s': %v", u.Name, err))
|
||||
_ = admin_model.CreateNoticeCtx(ctx, admin_model.NoticeTask, fmt.Sprintf("delete user '%s': %v", u.Name, err))
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -1312,7 +1314,7 @@ func deleteUser(e db.Engine, u *User) error {
|
|||
avatarPath := u.CustomAvatarRelativePath()
|
||||
if err = storage.Avatars.Delete(avatarPath); err != nil {
|
||||
err = fmt.Errorf("Failed to remove %s: %v", avatarPath, err)
|
||||
_ = createNotice(e, NoticeTask, fmt.Sprintf("delete user '%s': %v", u.Name, err))
|
||||
_ = admin_model.CreateNoticeCtx(ctx, admin_model.NoticeTask, fmt.Sprintf("delete user '%s': %v", u.Name, err))
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -1328,18 +1330,18 @@ func DeleteUser(u *User) (err error) {
|
|||
return fmt.Errorf("%s is an organization not a user", u.Name)
|
||||
}
|
||||
|
||||
sess := db.NewSession(db.DefaultContext)
|
||||
defer sess.Close()
|
||||
if err = sess.Begin(); err != nil {
|
||||
ctx, committer, err := db.TxContext()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer committer.Close()
|
||||
|
||||
if err = deleteUser(sess, u); err != nil {
|
||||
if err = deleteUser(ctx, u); err != nil {
|
||||
// Note: don't wrapper error here.
|
||||
return err
|
||||
}
|
||||
|
||||
return sess.Commit()
|
||||
return committer.Commit()
|
||||
}
|
||||
|
||||
// DeleteInactiveUsers deletes all inactive users and email addresses.
|
||||
|
@ -1825,3 +1827,16 @@ func GetUserByOpenID(uri string) (*User, error) {
|
|||
|
||||
return nil, ErrUserNotExist{0, uri, 0}
|
||||
}
|
||||
|
||||
// GetAdminUser returns the first administrator
|
||||
func GetAdminUser() (*User, error) {
|
||||
var admin User
|
||||
has, err := db.GetEngine(db.DefaultContext).Where("is_admin=?", true).Get(&admin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, ErrUserNotExist{}
|
||||
}
|
||||
|
||||
return &admin, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue