mirror of
https://github.com/AlistGo/alist.git
synced 2025-11-25 19:37:41 +08:00
* feat(setting): add role-based default and registration settings (closed #feat/register-and-statistics) - Added `AllowRegister` and `DefaultRole` settings to site configuration. - Integrated dynamic role options for `DefaultRole` using `op.GetRoles`. - Updated `setting.go` handlers to manage `DefaultRole` options dynamically. - Modified `const.go` to include new site settings constants. - Updated dependencies in `go.mod` and `go.sum` to support new functionality. * feat(register-and-statistics): add user registration endpoint - Added `POST /auth/register` endpoint to support user registration. - Implemented registration logic in `auth.go` with dynamic role assignment. - Integrated settings `AllowRegister` and `DefaultRole` for registration flow. - Updated imports to include new modules: `conf`, `setting`. - Adjusted user creation logic to use `DefaultRole` setting dynamically. * feat(register-and-statistics): add user registration endpoint (#register-and-statistics) - Added `POST /auth/register` endpoint to support user registration. - Implemented registration logic in `auth.go` with dynamic role assignment. - Integrated `AllowRegister` and `DefaultRole` settings for registration flow. - Updated imports to include new modules: `conf`, `setting`. - Adjusted user creation logic to use `DefaultRole` dynamically. * 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. * 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. * feat(register-and-statistics): improve role handling logic - Switch from role names to role IDs for better consistency. - Update logic to prioritize "guest" for default role ID. - Adjust `DefaultRole` setting to use role IDs. - Refactor `getRoleOptions` to return role IDs as a comma-separated string. * feat(register-and-statistics): improve role handling logic
96 lines
2.5 KiB
Go
96 lines
2.5 KiB
Go
package db
|
|
|
|
import (
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
"github.com/pkg/errors"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
func GetRole(id uint) (*model.Role, error) {
|
|
var r model.Role
|
|
if err := db.First(&r, id).Error; err != nil {
|
|
return nil, errors.Wrapf(err, "failed get role")
|
|
}
|
|
return &r, nil
|
|
}
|
|
|
|
func GetRoleByName(name string) (*model.Role, error) {
|
|
r := model.Role{Name: name}
|
|
if err := db.Where(r).First(&r).Error; err != nil {
|
|
return nil, errors.Wrapf(err, "failed get role")
|
|
}
|
|
return &r, nil
|
|
}
|
|
|
|
func GetRoles(pageIndex, pageSize int) (roles []model.Role, count int64, err error) {
|
|
roleDB := db.Model(&model.Role{})
|
|
if err = roleDB.Count(&count).Error; err != nil {
|
|
return nil, 0, errors.Wrapf(err, "failed get roles count")
|
|
}
|
|
if err = roleDB.Order(columnName("id")).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&roles).Error; err != nil {
|
|
return nil, 0, errors.Wrapf(err, "failed get find roles")
|
|
}
|
|
return roles, count, nil
|
|
}
|
|
|
|
func CreateRole(r *model.Role) 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 {
|
|
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 {
|
|
return errors.WithStack(db.Delete(&model.Role{}, id).Error)
|
|
}
|
|
|
|
func UpdateRolePermissionsPathPrefix(oldPath, newPath string) ([]uint, error) {
|
|
var roles []model.Role
|
|
var modifiedRoleIDs []uint
|
|
|
|
if err := db.Find(&roles).Error; err != nil {
|
|
return nil, errors.WithMessage(err, "failed to load roles")
|
|
}
|
|
|
|
for _, role := range roles {
|
|
updated := false
|
|
for i, entry := range role.PermissionScopes {
|
|
entryPath := path.Clean(entry.Path)
|
|
oldPathClean := path.Clean(oldPath)
|
|
|
|
if entryPath == oldPathClean {
|
|
role.PermissionScopes[i].Path = newPath
|
|
updated = true
|
|
} else if strings.HasPrefix(entryPath, oldPathClean+"/") {
|
|
role.PermissionScopes[i].Path = newPath + entryPath[len(oldPathClean):]
|
|
updated = true
|
|
}
|
|
}
|
|
if updated {
|
|
if err := UpdateRole(&role); err != nil {
|
|
return nil, errors.WithMessagef(err, "failed to update role ID %d", role.ID)
|
|
}
|
|
modifiedRoleIDs = append(modifiedRoleIDs, role.ID)
|
|
}
|
|
}
|
|
return modifiedRoleIDs, nil
|
|
}
|