mirror of
https://github.com/AlistGo/alist.git
synced 2025-04-21 20:18:47 +08:00
feat: add user model
This commit is contained in:
parent
1df5472855
commit
56c95eadea
24
internal/model/user.go
Normal file
24
internal/model/user.go
Normal file
@ -0,0 +1,24 @@
|
||||
package model
|
||||
|
||||
const (
|
||||
GENERAL = iota
|
||||
GUEST // only one exists
|
||||
ADMIN
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID uint `json:"id" gorm:"primaryKey"` // unique key
|
||||
Name string `json:"name" gorm:"unique"` // username
|
||||
Password string `json:"password"` // password
|
||||
BasePath string `json:"base_path"` // base path
|
||||
AllowUpload bool `json:"allow_upload"` // allow upload
|
||||
Role int `json:"role"` // user's role
|
||||
}
|
||||
|
||||
func (u User) IsGuest() bool {
|
||||
return u.Role == GUEST
|
||||
}
|
||||
|
||||
func (u User) IsAdmin() bool {
|
||||
return u.Role == ADMIN
|
||||
}
|
@ -3,11 +3,15 @@ package store
|
||||
import (
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"gorm.io/gorm"
|
||||
"log"
|
||||
)
|
||||
|
||||
var db gorm.DB
|
||||
|
||||
func Init(dbgorm *gorm.DB) {
|
||||
db = *dbgorm
|
||||
db.AutoMigrate(&model.Account{})
|
||||
func Init(d *gorm.DB) {
|
||||
db = *d
|
||||
err := db.AutoMigrate(&model.Account{})
|
||||
if err != nil {
|
||||
log.Fatalf("failed migrate database: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
78
internal/store/user.go
Normal file
78
internal/store/user.go
Normal file
@ -0,0 +1,78 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/go-cache"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/alist-org/alist/v3/pkg/singleflight"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var userCache = cache.NewMemCache(cache.WithShards[*model.User](4))
|
||||
var userG singleflight.Group[*model.User]
|
||||
|
||||
func ExistAdmin() bool {
|
||||
return db.Take(&model.User{Role: model.ADMIN}).Error != nil
|
||||
}
|
||||
|
||||
func ExistGuest() bool {
|
||||
return db.Take(&model.User{Role: model.GUEST}).Error != nil
|
||||
}
|
||||
|
||||
func GetUserByName(name string) (*model.User, error) {
|
||||
user, ok := userCache.Get(name)
|
||||
if ok {
|
||||
return user, nil
|
||||
}
|
||||
user, err, _ := userG.Do(name, func() (*model.User, error) {
|
||||
user := model.User{Name: name}
|
||||
if err := db.First(&user).Error; err != nil {
|
||||
return nil, errors.Wrapf(err, "failed select user")
|
||||
}
|
||||
userCache.Set(name, &user)
|
||||
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
|
||||
}
|
||||
userCache.Del(old.Name)
|
||||
return errors.WithStack(db.Save(u).Error)
|
||||
}
|
||||
|
||||
func GetUsers(pageIndex, pageSize int) ([]model.User, int64, error) {
|
||||
userDb := db.Model(&model.User{})
|
||||
var count int64
|
||||
if err := userDb.Count(&count).Error; err != nil {
|
||||
return nil, 0, errors.Wrapf(err, "failed get users count")
|
||||
}
|
||||
var users []model.User
|
||||
if err := userDb.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&users).Error; err != nil {
|
||||
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
|
||||
}
|
||||
userCache.Del(old.Name)
|
||||
return errors.WithStack(db.Delete(&model.User{}, id).Error)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user