mirror of https://github.com/Xhofe/alist
feat(register-and-statistics): enhance role management logic (#register-and-statistics)
- Refactored CreateRole and UpdateRole functions to handle default role. - Added dynamic role assignment logic in 'role.go' using conf settings. - Improved request handling in 'handles/role.go' with structured data. - Implemented default role logic in 'db/role.go' to update non-default roles. - Modified 'model/role.go' to include a 'Default' field for role management.pull/9277/head
parent
17b287972f
commit
b80c7f8f8d
|
@ -35,11 +35,27 @@ func GetRoles(pageIndex, pageSize int) (roles []model.Role, count int64, err err
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateRole(r *model.Role) error {
|
func CreateRole(r *model.Role) error {
|
||||||
return errors.WithStack(db.Create(r).Error)
|
if err := db.Create(r).Error; err != nil {
|
||||||
|
return errors.WithStack(err)
|
||||||
|
}
|
||||||
|
if r.Default {
|
||||||
|
if err := db.Model(&model.Role{}).Where("id <> ?", r.ID).Update("default", false).Error; err != nil {
|
||||||
|
return errors.WithStack(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateRole(r *model.Role) error {
|
func UpdateRole(r *model.Role) error {
|
||||||
return errors.WithStack(db.Save(r).Error)
|
if err := db.Save(r).Error; err != nil {
|
||||||
|
return errors.WithStack(err)
|
||||||
|
}
|
||||||
|
if r.Default {
|
||||||
|
if err := db.Model(&model.Role{}).Where("id <> ?", r.ID).Update("default", false).Error; err != nil {
|
||||||
|
return errors.WithStack(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteRole(id uint) error {
|
func DeleteRole(id uint) error {
|
||||||
|
|
|
@ -17,6 +17,7 @@ type Role struct {
|
||||||
ID uint `json:"id" gorm:"primaryKey"`
|
ID uint `json:"id" gorm:"primaryKey"`
|
||||||
Name string `json:"name" gorm:"unique" binding:"required"`
|
Name string `json:"name" gorm:"unique" binding:"required"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
|
Default bool `json:"default" gorm:"default:false"`
|
||||||
// PermissionScopes stores structured permission list and is ignored by gorm.
|
// PermissionScopes stores structured permission list and is ignored by gorm.
|
||||||
PermissionScopes []PermissionEntry `json:"permission_scopes" gorm:"-"`
|
PermissionScopes []PermissionEntry `json:"permission_scopes" gorm:"-"`
|
||||||
// RawPermission is the JSON representation of PermissionScopes stored in DB.
|
// RawPermission is the JSON representation of PermissionScopes stored in DB.
|
||||||
|
|
|
@ -2,9 +2,11 @@ package op
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Xhofe/go-cache"
|
"github.com/Xhofe/go-cache"
|
||||||
|
"github.com/alist-org/alist/v3/internal/conf"
|
||||||
"github.com/alist-org/alist/v3/internal/db"
|
"github.com/alist-org/alist/v3/internal/db"
|
||||||
"github.com/alist-org/alist/v3/internal/errs"
|
"github.com/alist-org/alist/v3/internal/errs"
|
||||||
"github.com/alist-org/alist/v3/internal/model"
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
|
@ -92,7 +94,21 @@ func CreateRole(r *model.Role) error {
|
||||||
}
|
}
|
||||||
roleCache.Del(fmt.Sprint(r.ID))
|
roleCache.Del(fmt.Sprint(r.ID))
|
||||||
roleCache.Del(r.Name)
|
roleCache.Del(r.Name)
|
||||||
return db.CreateRole(r)
|
if err := db.CreateRole(r); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if r.Default {
|
||||||
|
roleCache.Clear()
|
||||||
|
item, err := GetSettingItemByKey(conf.DefaultRole)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
item.Value = strconv.Itoa(int(r.ID))
|
||||||
|
if err := SaveSettingItem(item); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateRole(r *model.Role) error {
|
func UpdateRole(r *model.Role) error {
|
||||||
|
@ -131,7 +147,21 @@ func UpdateRole(r *model.Role) error {
|
||||||
//}
|
//}
|
||||||
roleCache.Del(fmt.Sprint(r.ID))
|
roleCache.Del(fmt.Sprint(r.ID))
|
||||||
roleCache.Del(r.Name)
|
roleCache.Del(r.Name)
|
||||||
return db.UpdateRole(r)
|
if err := db.UpdateRole(r); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if r.Default {
|
||||||
|
roleCache.Clear()
|
||||||
|
item, err := GetSettingItemByKey(conf.DefaultRole)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
item.Value = strconv.Itoa(int(r.ID))
|
||||||
|
if err := SaveSettingItem(item); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteRole(id uint) error {
|
func DeleteRole(id uint) error {
|
||||||
|
|
|
@ -44,7 +44,7 @@ func GetRole(c *gin.Context) {
|
||||||
|
|
||||||
func CreateRole(c *gin.Context) {
|
func CreateRole(c *gin.Context) {
|
||||||
var req model.Role
|
var req model.Role
|
||||||
if err := c.ShouldBind(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
common.ErrorResp(c, err, 400)
|
common.ErrorResp(c, err, 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -56,8 +56,14 @@ func CreateRole(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateRole(c *gin.Context) {
|
func UpdateRole(c *gin.Context) {
|
||||||
var req model.Role
|
var req struct {
|
||||||
if err := c.ShouldBind(&req); err != nil {
|
ID uint `json:"id"`
|
||||||
|
Name string `json:"name" binding:"required"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
PermissionScopes []model.PermissionEntry `json:"permission_scopes"`
|
||||||
|
Default *bool `json:"default"`
|
||||||
|
}
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
common.ErrorResp(c, err, 400)
|
common.ErrorResp(c, err, 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -74,7 +80,13 @@ func UpdateRole(c *gin.Context) {
|
||||||
case "guest":
|
case "guest":
|
||||||
req.Name = "guest"
|
req.Name = "guest"
|
||||||
}
|
}
|
||||||
if err := op.UpdateRole(&req); err != nil {
|
role.Name = req.Name
|
||||||
|
role.Description = req.Description
|
||||||
|
role.PermissionScopes = req.PermissionScopes
|
||||||
|
if req.Default != nil {
|
||||||
|
role.Default = *req.Default
|
||||||
|
}
|
||||||
|
if err := op.UpdateRole(role); err != nil {
|
||||||
common.ErrorResp(c, err, 500, true)
|
common.ErrorResp(c, err, 500, true)
|
||||||
} else {
|
} else {
|
||||||
common.SuccessResp(c)
|
common.SuccessResp(c)
|
||||||
|
|
Loading…
Reference in New Issue