alist/internal/db/user.go

125 lines
2.8 KiB
Go
Raw Normal View History

2022-06-25 13:36:35 +00:00
package db
2022-06-16 08:06:10 +00:00
import (
2022-08-03 06:26:59 +00:00
"time"
2022-06-16 08:06:10 +00:00
"github.com/Xhofe/go-cache"
2022-06-25 14:05:02 +00:00
"github.com/alist-org/alist/v3/internal/errs"
2022-06-16 08:06:10 +00:00
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/singleflight"
"github.com/pkg/errors"
)
2022-06-16 13:59:17 +00:00
var userCache = cache.NewMemCache(cache.WithShards[*model.User](2))
2022-06-16 08:06:10 +00:00
var userG singleflight.Group[*model.User]
2022-06-27 06:29:36 +00:00
var guest *model.User
2022-06-28 06:18:10 +00:00
var admin *model.User
2022-06-16 08:06:10 +00:00
2022-06-25 14:05:02 +00:00
func GetAdmin() (*model.User, error) {
2022-06-28 06:18:10 +00:00
if admin != nil {
return admin, nil
}
2022-06-25 14:05:02 +00:00
user := model.User{Role: model.ADMIN}
if err := db.Where(user).Take(&user).Error; err != nil {
return nil, err
}
2022-06-28 06:18:10 +00:00
admin = &user
2022-06-25 14:05:02 +00:00
return &user, nil
2022-06-16 08:06:10 +00:00
}
2022-06-25 14:05:02 +00:00
func GetGuest() (*model.User, error) {
2022-06-27 06:29:36 +00:00
if guest != nil {
return guest, nil
}
2022-06-25 14:05:02 +00:00
user := model.User{Role: model.GUEST}
if err := db.Where(user).Take(&user).Error; err != nil {
return nil, err
}
2022-06-27 06:29:36 +00:00
guest = &user
2022-06-25 14:05:02 +00:00
return &user, nil
2022-06-16 08:06:10 +00:00
}
2022-06-25 13:34:44 +00:00
func GetUserByName(username string) (*model.User, error) {
2022-06-25 14:05:02 +00:00
if username == "" {
return nil, errors.WithStack(errs.EmptyUsername)
}
2022-06-25 13:34:44 +00:00
user, ok := userCache.Get(username)
2022-06-16 08:06:10 +00:00
if ok {
return user, nil
}
2022-06-25 13:34:44 +00:00
user, err, _ := userG.Do(username, func() (*model.User, error) {
user := model.User{Username: username}
2022-06-16 13:59:17 +00:00
if err := db.Where(user).First(&user).Error; err != nil {
2022-06-25 13:34:44 +00:00
return nil, errors.Wrapf(err, "failed find user")
2022-06-16 08:06:10 +00:00
}
userCache.Set(username, &user, cache.WithEx[*model.User](time.Hour))
2022-06-16 08:06:10 +00:00
return &user, nil
})
return user, err
}
func GetUserById(id uint) (*model.User, error) {
var u model.User
if err := db.First(&u, id).Error; err != nil {
return nil, errors.Wrapf(err, "failed get old user")
}
return &u, nil
}
func CreateUser(u *model.User) error {
return errors.WithStack(db.Create(u).Error)
}
func UpdateUser(u *model.User) error {
old, err := GetUserById(u.ID)
if err != nil {
return err
}
2022-06-25 13:34:44 +00:00
userCache.Del(old.Username)
2022-06-27 06:29:36 +00:00
if u.IsGuest() {
guest = nil
}
2022-06-28 06:18:10 +00:00
if u.IsAdmin() {
admin = nil
}
2022-06-16 08:06:10 +00:00
return errors.WithStack(db.Save(u).Error)
}
2022-08-07 03:59:33 +00:00
func Cancel2FAByUser(u *model.User) error {
u.OtpSecret = ""
return errors.WithStack(UpdateUser(u))
}
func Cancel2FAById(id uint) error {
user, err := GetUserById(id)
if err != nil {
return err
}
return Cancel2FAByUser(user)
}
2022-06-16 08:06:10 +00:00
func GetUsers(pageIndex, pageSize int) ([]model.User, int64, error) {
2022-06-16 13:59:17 +00:00
userDB := db.Model(&model.User{})
2022-06-16 08:06:10 +00:00
var count int64
2022-06-16 13:59:17 +00:00
if err := userDB.Count(&count).Error; err != nil {
2022-06-16 08:06:10 +00:00
return nil, 0, errors.Wrapf(err, "failed get users count")
}
var users []model.User
2022-06-16 13:59:17 +00:00
if err := userDB.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&users).Error; err != nil {
2022-06-16 08:06:10 +00:00
return nil, 0, errors.Wrapf(err, "failed get find users")
}
return users, count, nil
}
func DeleteUserById(id uint) error {
old, err := GetUserById(id)
if err != nil {
return err
}
2022-06-26 11:36:27 +00:00
if old.IsAdmin() || old.IsGuest() {
return errors.WithStack(errs.DeleteAdminOrGuest)
}
2022-06-25 13:34:44 +00:00
userCache.Del(old.Username)
2022-06-16 08:06:10 +00:00
return errors.WithStack(db.Delete(&model.User{}, id).Error)
}