-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
109 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |