chore: init users
This commit is contained in:
@ -2,6 +2,7 @@ package db
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/go-cache"
|
||||
"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/pkg/errors"
|
||||
@ -10,15 +11,26 @@ import (
|
||||
var userCache = cache.NewMemCache(cache.WithShards[*model.User](2))
|
||||
var userG singleflight.Group[*model.User]
|
||||
|
||||
func ExistAdmin() bool {
|
||||
return db.Take(&model.User{Role: model.ADMIN}).Error != nil
|
||||
func GetAdmin() (*model.User, error) {
|
||||
user := model.User{Role: model.ADMIN}
|
||||
if err := db.Where(user).Take(&user).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func ExistGuest() bool {
|
||||
return db.Take(&model.User{Role: model.GUEST}).Error != nil
|
||||
func GetGuest() (*model.User, error) {
|
||||
user := model.User{Role: model.GUEST}
|
||||
if err := db.Where(user).Take(&user).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func GetUserByName(username string) (*model.User, error) {
|
||||
if username == "" {
|
||||
return nil, errors.WithStack(errs.EmptyUsername)
|
||||
}
|
||||
user, ok := userCache.Get(username)
|
||||
if ok {
|
||||
return user, nil
|
||||
|
9
internal/errs/user.go
Normal file
9
internal/errs/user.go
Normal file
@ -0,0 +1,9 @@
|
||||
package errs
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
EmptyUsername = errors.New("username is empty")
|
||||
EmptyPassword = errors.New("password is empty")
|
||||
WrongPassword = errors.New("password is incorrect")
|
||||
)
|
@ -1,6 +1,9 @@
|
||||
package model
|
||||
|
||||
import "github.com/pkg/errors"
|
||||
import (
|
||||
"github.com/alist-org/alist/v3/internal/errs"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
GENERAL = iota
|
||||
@ -27,10 +30,10 @@ func (u User) IsAdmin() bool {
|
||||
|
||||
func (u User) ValidatePassword(password string) error {
|
||||
if password == "" {
|
||||
return errors.New("password is empty")
|
||||
return errors.WithStack(errs.EmptyPassword)
|
||||
}
|
||||
if u.Password != password {
|
||||
return errors.New("password is incorrect")
|
||||
return errors.WithStack(errs.WrongPassword)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -11,8 +11,10 @@ type Resp struct {
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func ErrorResp(c *gin.Context, err error, code int) {
|
||||
log.Error(err.Error())
|
||||
func ErrorResp(c *gin.Context, err error, code int, noLog ...bool) {
|
||||
if len(noLog) != 0 && noLog[0] {
|
||||
log.Errorf("%+v", err)
|
||||
}
|
||||
c.JSON(200, Resp{
|
||||
Code: code,
|
||||
Message: err.Error(),
|
||||
|
@ -36,12 +36,12 @@ func Login(c *gin.Context) {
|
||||
}
|
||||
user, err := db.GetUserByName(req.Username)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
common.ErrorResp(c, err, 400, true)
|
||||
return
|
||||
}
|
||||
// validate password
|
||||
if err := user.ValidatePassword(req.Password); err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
common.ErrorResp(c, err, 400, true)
|
||||
loginCache.Set(ip, count+1)
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user