wip: jwt
This commit is contained in:
@ -2,14 +2,60 @@ package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"loveuer/utodo/internal/model/enum"
|
||||
"loveuer/utodo/pkg/logger"
|
||||
"loveuer/utodo/pkg/sqlType"
|
||||
"loveuer/utodo/pkg/tool"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
func Init(ctx context.Context, tx *gorm.DB) error {
|
||||
return tx.AutoMigrate(
|
||||
func Init(ctx context.Context, tx *gorm.DB) (err error) {
|
||||
if err = tx.AutoMigrate(
|
||||
&Todo{},
|
||||
&User{},
|
||||
&Object{},
|
||||
)
|
||||
&Privilege{},
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = tx.Model(&Privilege{}).
|
||||
Clauses(clause.OnConflict{
|
||||
DoNothing: true,
|
||||
}).
|
||||
Create(&Privilege{
|
||||
Role: enum.UserRoleAdmin,
|
||||
Privileges: sqlType.SliceStr[enum.Privilege]{enum.PrivilegeAll},
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var uc int64
|
||||
if err = tx.Model(&User{}).
|
||||
Select("COUNT(id)").
|
||||
Where("deleted_at", 0).
|
||||
Find(&uc).
|
||||
Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if uc == 0 {
|
||||
password := tool.RandomString(16)
|
||||
if err = tx.Model(&User{}).
|
||||
Create(&User{
|
||||
Username: "admin",
|
||||
Nickname: "admin",
|
||||
Password: tool.NewPassword(password),
|
||||
Roles: sqlType.SliceStr[enum.UserRole]{enum.UserRoleAdmin},
|
||||
ResetPassword: 1,
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logger.InfoCtx(ctx, "inited admin password: %s", password)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user