diff options
Diffstat (limited to 'models')
-rw-r--r-- | models/admin/main_test.go | 18 | ||||
-rw-r--r-- | models/admin/notice.go (renamed from models/admin.go) | 43 | ||||
-rw-r--r-- | models/admin/notice_test.go (renamed from models/admin_test.go) | 2 | ||||
-rw-r--r-- | models/consistency.go | 3 | ||||
-rw-r--r-- | models/repo.go | 17 | ||||
-rw-r--r-- | models/user.go | 31 |
6 files changed, 69 insertions, 45 deletions
diff --git a/models/admin/main_test.go b/models/admin/main_test.go new file mode 100644 index 0000000000..f6c9dce57a --- /dev/null +++ b/models/admin/main_test.go @@ -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", + ) +} diff --git a/models/admin.go b/models/admin/notice.go index a003aff7e6..c41e49ed8c 100644 --- a/models/admin.go +++ b/models/admin/notice.go @@ -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 -} diff --git a/models/admin_test.go b/models/admin/notice_test.go index 8c1deda5f8..7272df4368 100644 --- a/models/admin_test.go +++ b/models/admin/notice_test.go @@ -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" diff --git a/models/consistency.go b/models/consistency.go index d7bf8ade57..83cfbd0e49 100644 --- a/models/consistency.go +++ b/models/consistency.go @@ -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 } diff --git a/models/repo.go b/models/repo.go index d45f1b2fc3..57806a86f7 100644 --- a/models/repo.go +++ b/models/repo.go @@ -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 { diff --git a/models/user.go b/models/user.go index a87929a35f..cc6c5a5578 100644 --- a/models/user.go +++ b/models/user.go @@ -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 +} |