mirror of https://github.com/Xhofe/alist
feat(register-and-statistics): enhance role management logic
- Refactor CreateRole and UpdateRole to handle default roles. - Add dynamic role assignment using conf settings in 'role.go'. - Improve request handling with structured data in 'handles/role.go'. - Implement default role logic in 'db/role.go' for non-default roles. - Modify 'model/role.go' to include 'Default' field for role management.pull/9277/head
parent
b80c7f8f8d
commit
b105bb02bf
|
@ -52,6 +52,23 @@ func GetRoleByName(name string) (*model.Role, error) {
|
|||
return r, err
|
||||
}
|
||||
|
||||
func GetDefaultRoleID() int {
|
||||
item, err := GetSettingItemByKey(conf.DefaultRole)
|
||||
if err == nil && item != nil && item.Value != "" {
|
||||
if id, err := strconv.Atoi(item.Value); err == nil && id != 0 {
|
||||
return id
|
||||
}
|
||||
if r, err := db.GetRoleByName(item.Value); err == nil {
|
||||
return int(r.ID)
|
||||
}
|
||||
}
|
||||
var r model.Role
|
||||
if err := db.GetDb().Where("`default` = ?", true).First(&r).Error; err == nil {
|
||||
return int(r.ID)
|
||||
}
|
||||
return int(model.GUEST)
|
||||
}
|
||||
|
||||
func GetRolesByUserID(userID uint) ([]model.Role, error) {
|
||||
user, err := GetUserById(userID)
|
||||
if err != nil {
|
||||
|
|
|
@ -109,7 +109,7 @@ func Register(c *gin.Context) {
|
|||
}
|
||||
user := &model.User{
|
||||
Username: req.Username,
|
||||
Role: model.Roles{setting.GetInt(conf.DefaultRole, int(model.GUEST))},
|
||||
Role: model.Roles{op.GetDefaultRoleID()},
|
||||
Authn: "[]",
|
||||
}
|
||||
user.SetPassword(req.Password)
|
||||
|
|
|
@ -4,10 +4,8 @@ import (
|
|||
"github.com/alist-org/alist/v3/pkg/utils"
|
||||
"strconv"
|
||||
|
||||
"github.com/alist-org/alist/v3/internal/conf"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/alist-org/alist/v3/internal/op"
|
||||
"github.com/alist-org/alist/v3/internal/setting"
|
||||
"github.com/alist-org/alist/v3/server/common"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
@ -39,7 +37,7 @@ func CreateUser(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
if len(req.Role) == 0 {
|
||||
req.Role = model.Roles{setting.GetInt(conf.DefaultRole, int(model.GUEST))}
|
||||
req.Role = model.Roles{op.GetDefaultRoleID()}
|
||||
}
|
||||
if req.IsAdmin() || req.IsGuest() {
|
||||
common.ErrorStrResp(c, "admin or guest user can not be created", 400, true)
|
||||
|
|
Loading…
Reference in New Issue