feat(role): 新增平台角色管理功能增强
- 权限表增加 available_for_role_types 字段,支持标记权限可用角色类型 - 权限列表和权限树接口支持按 available_for_role_type 过滤 - 新增角色状态切换接口 PUT /api/admin/roles/:id/status - 角色分配权限时验证权限的可用角色类型 - 完善数据库迁移脚本和单元测试 - 补充数据库迁移相关开发规范文档
This commit is contained in:
@@ -2,6 +2,7 @@ package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
@@ -72,6 +73,10 @@ func (s *PermissionStore) List(ctx context.Context, opts *store.QueryOptions, fi
|
||||
if platform, ok := filters["platform"].(string); ok && platform != "" {
|
||||
query = query.Where("platform = ?", platform)
|
||||
}
|
||||
if availableForRoleType, ok := filters["available_for_role_type"].(int); ok {
|
||||
roleTypeStr := fmt.Sprintf("%d", availableForRoleType)
|
||||
query = query.Where("available_for_role_types LIKE ?", "%"+roleTypeStr+"%")
|
||||
}
|
||||
if parentID, ok := filters["parent_id"].(uint); ok {
|
||||
query = query.Where("parent_id = ?", parentID)
|
||||
}
|
||||
@@ -116,23 +121,33 @@ func (s *PermissionStore) GetByIDs(ctx context.Context, ids []uint) ([]*model.Pe
|
||||
}
|
||||
|
||||
// GetAll 获取所有权限(用于构建权限树)
|
||||
func (s *PermissionStore) GetAll(ctx context.Context) ([]*model.Permission, error) {
|
||||
func (s *PermissionStore) GetAll(ctx context.Context, availableForRoleType *int) ([]*model.Permission, error) {
|
||||
var permissions []*model.Permission
|
||||
if err := s.db.WithContext(ctx).Order("sort ASC, id ASC").Find(&permissions).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return permissions, nil
|
||||
}
|
||||
query := s.db.WithContext(ctx)
|
||||
|
||||
// GetByPlatform 根据端口获取权限列表
|
||||
// platform: 端口类型(all/web/h5),如果为空则返回所有权限
|
||||
func (s *PermissionStore) GetByPlatform(ctx context.Context, platform string) ([]*model.Permission, error) {
|
||||
var permissions []*model.Permission
|
||||
query := s.db.WithContext(ctx).Where("status = ?", 1) // 只获取启用的权限
|
||||
|
||||
if platform != "" {
|
||||
// 获取指定端口的权限或通用权限(platform='all')
|
||||
query = query.Where("platform = ? OR platform = ?", platform, "all")
|
||||
if availableForRoleType != nil {
|
||||
roleTypeStr := fmt.Sprintf("%d", *availableForRoleType)
|
||||
query = query.Where("available_for_role_types LIKE ?", "%"+roleTypeStr+"%")
|
||||
}
|
||||
|
||||
if err := query.Order("sort ASC, id ASC").Find(&permissions).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return permissions, nil
|
||||
}
|
||||
|
||||
// GetByPlatform 根据端口获取权限列表
|
||||
func (s *PermissionStore) GetByPlatform(ctx context.Context, platform string, availableForRoleType *int) ([]*model.Permission, error) {
|
||||
var permissions []*model.Permission
|
||||
query := s.db.WithContext(ctx).Where("status = ?", 1)
|
||||
|
||||
if platform != "" {
|
||||
query = query.Where("platform = ? OR platform = ?", platform, "all")
|
||||
}
|
||||
|
||||
if availableForRoleType != nil {
|
||||
roleTypeStr := fmt.Sprintf("%d", *availableForRoleType)
|
||||
query = query.Where("available_for_role_types LIKE ?", "%"+roleTypeStr+"%")
|
||||
}
|
||||
|
||||
if err := query.Order("sort ASC, id ASC").Find(&permissions).Error; err != nil {
|
||||
|
||||
Reference in New Issue
Block a user