-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
user.go
130 lines (115 loc) · 2.63 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package op
import (
"time"
"github.com/Xhofe/go-cache"
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/singleflight"
"github.com/alist-org/alist/v3/pkg/utils"
)
var userCache = cache.NewMemCache(cache.WithShards[*model.User](2))
var userG singleflight.Group[*model.User]
var guestUser *model.User
var adminUser *model.User
func GetAdmin() (*model.User, error) {
if adminUser == nil {
user, err := db.GetUserByRole(model.ADMIN)
if err != nil {
return nil, err
}
adminUser = user
}
return adminUser, nil
}
func GetGuest() (*model.User, error) {
if guestUser == nil {
user, err := db.GetUserByRole(model.GUEST)
if err != nil {
return nil, err
}
guestUser = user
}
return guestUser, nil
}
func GetUserByRole(role int) (*model.User, error) {
return db.GetUserByRole(role)
}
func GetUserByName(username string) (*model.User, error) {
if username == "" {
return nil, errs.EmptyUsername
}
if user, ok := userCache.Get(username); ok {
return user, nil
}
user, err, _ := userG.Do(username, func() (*model.User, error) {
_user, err := db.GetUserByName(username)
if err != nil {
return nil, err
}
userCache.Set(username, _user, cache.WithEx[*model.User](time.Hour))
return _user, nil
})
return user, err
}
func GetUserById(id uint) (*model.User, error) {
return db.GetUserById(id)
}
func GetUsers(pageIndex, pageSize int) (users []model.User, count int64, err error) {
return db.GetUsers(pageIndex, pageSize)
}
func CreateUser(u *model.User) error {
u.BasePath = utils.FixAndCleanPath(u.BasePath)
return db.CreateUser(u)
}
func DeleteUserById(id uint) error {
old, err := db.GetUserById(id)
if err != nil {
return err
}
if old.IsAdmin() || old.IsGuest() {
return errs.DeleteAdminOrGuest
}
userCache.Del(old.Username)
return db.DeleteUserById(id)
}
func UpdateUser(u *model.User) error {
old, err := db.GetUserById(u.ID)
if err != nil {
return err
}
if u.IsAdmin() {
adminUser = nil
}
if u.IsGuest() {
guestUser = nil
}
userCache.Del(old.Username)
u.BasePath = utils.FixAndCleanPath(u.BasePath)
return db.UpdateUser(u)
}
func Cancel2FAByUser(u *model.User) error {
u.OtpSecret = ""
return UpdateUser(u)
}
func Cancel2FAById(id uint) error {
user, err := db.GetUserById(id)
if err != nil {
return err
}
return Cancel2FAByUser(user)
}
func DelUserCache(username string) error {
user, err := GetUserByName(username)
if err != nil {
return err
}
if user.IsAdmin() {
adminUser = nil
}
if user.IsGuest() {
guestUser = nil
}
userCache.Del(username)
return nil
}