|
1 | | -// Copyright 2014 The Gogs Authors. All rights reserved. |
2 | | -// Copyright 2020 The Gitea Authors. All rights reserved. |
| 1 | +// Copyright 2021 The Gitea Authors. All rights reserved. |
3 | 2 | // Use of this source code is governed by a MIT-style |
4 | 3 | // license that can be found in the LICENSE file. |
5 | 4 |
|
6 | | -package models |
| 5 | +package admin |
7 | 6 |
|
8 | 7 | import ( |
| 8 | + "context" |
9 | 9 | "fmt" |
10 | 10 |
|
11 | 11 | "code.gitea.io/gitea/models/db" |
@@ -44,53 +44,55 @@ func (n *Notice) TrStr() string { |
44 | 44 |
|
45 | 45 | // CreateNotice creates new system notice. |
46 | 46 | func CreateNotice(tp NoticeType, desc string, args ...interface{}) error { |
47 | | - return createNotice(db.GetEngine(db.DefaultContext), tp, desc, args...) |
| 47 | + return CreateNoticeCtx(db.DefaultContext, tp, desc, args...) |
48 | 48 | } |
49 | 49 |
|
50 | | -func createNotice(e db.Engine, tp NoticeType, desc string, args ...interface{}) error { |
| 50 | +// CreateNoticeCtx creates new system notice. |
| 51 | +func CreateNoticeCtx(ctx context.Context, tp NoticeType, desc string, args ...interface{}) error { |
51 | 52 | if len(args) > 0 { |
52 | 53 | desc = fmt.Sprintf(desc, args...) |
53 | 54 | } |
54 | 55 | n := &Notice{ |
55 | 56 | Type: tp, |
56 | 57 | Description: desc, |
57 | 58 | } |
58 | | - _, err := e.Insert(n) |
59 | | - return err |
| 59 | + return db.Insert(ctx, n) |
60 | 60 | } |
61 | 61 |
|
62 | 62 | // CreateRepositoryNotice creates new system notice with type NoticeRepository. |
63 | 63 | func CreateRepositoryNotice(desc string, args ...interface{}) error { |
64 | | - return createNotice(db.GetEngine(db.DefaultContext), NoticeRepository, desc, args...) |
| 64 | + return CreateNoticeCtx(db.DefaultContext, NoticeRepository, desc, args...) |
65 | 65 | } |
66 | 66 |
|
67 | 67 | // RemoveAllWithNotice removes all directories in given path and |
68 | 68 | // creates a system notice when error occurs. |
69 | 69 | func RemoveAllWithNotice(title, path string) { |
70 | | - removeAllWithNotice(db.GetEngine(db.DefaultContext), title, path) |
| 70 | + RemoveAllWithNoticeCtx(db.DefaultContext, title, path) |
71 | 71 | } |
72 | 72 |
|
73 | 73 | // RemoveStorageWithNotice removes a file from the storage and |
74 | 74 | // creates a system notice when error occurs. |
75 | 75 | func RemoveStorageWithNotice(bucket storage.ObjectStorage, title, path string) { |
76 | | - removeStorageWithNotice(db.GetEngine(db.DefaultContext), bucket, title, path) |
| 76 | + removeStorageWithNotice(db.DefaultContext, bucket, title, path) |
77 | 77 | } |
78 | 78 |
|
79 | | -func removeStorageWithNotice(e db.Engine, bucket storage.ObjectStorage, title, path string) { |
| 79 | +func removeStorageWithNotice(ctx context.Context, bucket storage.ObjectStorage, title, path string) { |
80 | 80 | if err := bucket.Delete(path); err != nil { |
81 | 81 | desc := fmt.Sprintf("%s [%s]: %v", title, path, err) |
82 | 82 | log.Warn(title+" [%s]: %v", path, err) |
83 | | - if err = createNotice(e, NoticeRepository, desc); err != nil { |
| 83 | + if err = CreateNoticeCtx(ctx, NoticeRepository, desc); err != nil { |
84 | 84 | log.Error("CreateRepositoryNotice: %v", err) |
85 | 85 | } |
86 | 86 | } |
87 | 87 | } |
88 | 88 |
|
89 | | -func removeAllWithNotice(e db.Engine, title, path string) { |
| 89 | +// RemoveAllWithNoticeCtx removes all directories in given path and |
| 90 | +// creates a system notice when error occurs. |
| 91 | +func RemoveAllWithNoticeCtx(ctx context.Context, title, path string) { |
90 | 92 | if err := util.RemoveAll(path); err != nil { |
91 | 93 | desc := fmt.Sprintf("%s [%s]: %v", title, path, err) |
92 | 94 | log.Warn(title+" [%s]: %v", path, err) |
93 | | - if err = createNotice(e, NoticeRepository, desc); err != nil { |
| 95 | + if err = CreateNoticeCtx(ctx, NoticeRepository, desc); err != nil { |
94 | 96 | log.Error("CreateRepositoryNotice: %v", err) |
95 | 97 | } |
96 | 98 | } |
@@ -142,16 +144,3 @@ func DeleteNoticesByIDs(ids []int64) error { |
142 | 144 | Delete(new(Notice)) |
143 | 145 | return err |
144 | 146 | } |
145 | | - |
146 | | -// GetAdminUser returns the first administrator |
147 | | -func GetAdminUser() (*User, error) { |
148 | | - var admin User |
149 | | - has, err := db.GetEngine(db.DefaultContext).Where("is_admin=?", true).Get(&admin) |
150 | | - if err != nil { |
151 | | - return nil, err |
152 | | - } else if !has { |
153 | | - return nil, ErrUserNotExist{} |
154 | | - } |
155 | | - |
156 | | - return &admin, nil |
157 | | -} |
0 commit comments