chore: init users

refactor/fs
Noah Hsu 2022-06-25 22:05:02 +08:00
parent b474eefd87
commit 54ca68e4b3
7 changed files with 88 additions and 12 deletions

49
bootstrap/data.go Normal file
View File

@ -0,0 +1,49 @@
package bootstrap
import (
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils/random"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
func InitData() {
initUser()
}
func initUser() {
admin, err := db.GetAdmin()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
admin = &model.User{
Username: "admin",
Password: random.RandomStr(8),
Role: model.ADMIN,
BasePath: "/",
}
if err := db.CreateUser(admin); err != nil {
panic(err)
}
} else {
panic(err)
}
}
guest, err := db.GetGuest()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
guest = &model.User{
Username: "guest",
Role: model.GUEST,
BasePath: "/",
}
if err := db.CreateUser(guest); err != nil {
panic(err)
}
} else {
panic(err)
}
}
log.Infof("admin password: %+v", admin.Password)
}

View File

@ -33,10 +33,11 @@ func Init() {
bootstrap.InitConfig() bootstrap.InitConfig()
bootstrap.Log() bootstrap.Log()
bootstrap.InitDB() bootstrap.InitDB()
bootstrap.InitData()
} }
func main() { func main() {
Init() Init()
if !args.Debug { if !args.Debug && !args.Dev {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
} }
r := gin.New() r := gin.New()

View File

@ -2,6 +2,7 @@ package db
import ( import (
"github.com/Xhofe/go-cache" "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/internal/model"
"github.com/alist-org/alist/v3/pkg/singleflight" "github.com/alist-org/alist/v3/pkg/singleflight"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -10,15 +11,26 @@ import (
var userCache = cache.NewMemCache(cache.WithShards[*model.User](2)) var userCache = cache.NewMemCache(cache.WithShards[*model.User](2))
var userG singleflight.Group[*model.User] var userG singleflight.Group[*model.User]
func ExistAdmin() bool { func GetAdmin() (*model.User, error) {
return db.Take(&model.User{Role: model.ADMIN}).Error != nil 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 { func GetGuest() (*model.User, error) {
return db.Take(&model.User{Role: model.GUEST}).Error != nil 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) { func GetUserByName(username string) (*model.User, error) {
if username == "" {
return nil, errors.WithStack(errs.EmptyUsername)
}
user, ok := userCache.Get(username) user, ok := userCache.Get(username)
if ok { if ok {
return user, nil return user, nil

9
internal/errs/user.go Normal file
View 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")
)

View File

@ -1,6 +1,9 @@
package model package model
import "github.com/pkg/errors" import (
"github.com/alist-org/alist/v3/internal/errs"
"github.com/pkg/errors"
)
const ( const (
GENERAL = iota GENERAL = iota
@ -27,10 +30,10 @@ func (u User) IsAdmin() bool {
func (u User) ValidatePassword(password string) error { func (u User) ValidatePassword(password string) error {
if password == "" { if password == "" {
return errors.New("password is empty") return errors.WithStack(errs.EmptyPassword)
} }
if u.Password != password { if u.Password != password {
return errors.New("password is incorrect") return errors.WithStack(errs.WrongPassword)
} }
return nil return nil
} }

View File

@ -11,8 +11,10 @@ type Resp struct {
Data interface{} `json:"data"` Data interface{} `json:"data"`
} }
func ErrorResp(c *gin.Context, err error, code int) { func ErrorResp(c *gin.Context, err error, code int, noLog ...bool) {
log.Error(err.Error()) if len(noLog) != 0 && noLog[0] {
log.Errorf("%+v", err)
}
c.JSON(200, Resp{ c.JSON(200, Resp{
Code: code, Code: code,
Message: err.Error(), Message: err.Error(),

View File

@ -36,12 +36,12 @@ func Login(c *gin.Context) {
} }
user, err := db.GetUserByName(req.Username) user, err := db.GetUserByName(req.Username)
if err != nil { if err != nil {
common.ErrorResp(c, err, 400) common.ErrorResp(c, err, 400, true)
return return
} }
// validate password // validate password
if err := user.ValidatePassword(req.Password); err != nil { if err := user.ValidatePassword(req.Password); err != nil {
common.ErrorResp(c, err, 400) common.ErrorResp(c, err, 400, true)
loginCache.Set(ip, count+1) loginCache.Set(ip, count+1)
return return
} }