328 lines
8.8 KiB
Go
328 lines
8.8 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
"nl-video-api/internal/dao"
|
|
"nl-video-api/internal/model/entity"
|
|
)
|
|
|
|
// RoleService 角色服务
|
|
type RoleService struct{}
|
|
|
|
var Role = &RoleService{}
|
|
|
|
// RoleCreateReq 创建角色请求
|
|
type RoleCreateReq struct {
|
|
Name string `json:"name" v:"required|length:2,50#角色名称不能为空|角色名称长度为2-50位"`
|
|
Slug string `json:"slug" v:"required|length:2,50#角色标识不能为空|角色标识长度为2-50位"`
|
|
Level int `json:"level" v:"required|min:1,max:10#角色等级不能为空|角色等级范围1-10"`
|
|
Description string `json:"description"` // 角色描述
|
|
Sort int `json:"sort"` // 排序
|
|
Status int `json:"status" v:"in:0,1#状态只能为0或1"`
|
|
IsSystem int `json:"is_system" v:"in:0,1#系统角色标识只能为0或1"`
|
|
}
|
|
|
|
// RoleUpdateReq 更新角色请求
|
|
type RoleUpdateReq struct {
|
|
Id int `json:"id" v:"required|min:1#角色ID不能为空"`
|
|
Name string `json:"name" v:"required|length:2,50#角色名称不能为空|角色名称长度为2-50位"`
|
|
Slug string `json:"slug" v:"required|length:2,50#角色标识不能为空|角色标识长度为2-50位"`
|
|
Level int `json:"level" v:"required|min:1,max:10#角色等级不能为空|角色等级范围1-10"`
|
|
Description string `json:"description"` // 角色描述
|
|
Sort int `json:"sort"` // 排序
|
|
Status int `json:"status" v:"in:0,1#状态只能为0或1"`
|
|
IsSystem int `json:"is_system" v:"in:0,1#系统角色标识只能为0或1"`
|
|
}
|
|
|
|
// RolePermissionReq 角色权限分配请求
|
|
type RolePermissionReq struct {
|
|
RoleId int `json:"role_id" v:"required|min:1#角色ID不能为空"`
|
|
PermissionIds []int `json:"permission_ids"` // 权限ID列表
|
|
}
|
|
|
|
// Create 创建角色
|
|
func (s *RoleService) Create(ctx context.Context, req *RoleCreateReq) (int, error) {
|
|
// 检查角色标识是否存在
|
|
exists, err := dao.Role.CheckCodeExists(ctx, req.Slug, 0)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if exists {
|
|
return 0, errors.New("角色标识已存在")
|
|
}
|
|
|
|
// 创建角色数据
|
|
role := &entity.Role{
|
|
Name: req.Name,
|
|
Slug: req.Slug,
|
|
Level: req.Level,
|
|
Description: req.Description,
|
|
Sort: req.Sort,
|
|
Status: req.Status,
|
|
IsSystem: req.IsSystem,
|
|
CreatedAt: gtime.Now().String(),
|
|
UpdatedAt: gtime.Now().String(),
|
|
}
|
|
|
|
return dao.Role.Create(ctx, role)
|
|
}
|
|
|
|
// Update 更新角色
|
|
func (s *RoleService) Update(ctx context.Context, req *RoleUpdateReq) error {
|
|
// 检查角色是否存在
|
|
role, err := dao.Role.GetById(ctx, req.Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if role == nil {
|
|
return errors.New("角色不存在")
|
|
}
|
|
|
|
// 系统角色不允许修改标识和系统标识
|
|
if role.IsSystem == 1 {
|
|
if req.Slug != role.Slug {
|
|
return errors.New("系统角色不允许修改标识")
|
|
}
|
|
if req.IsSystem != role.IsSystem {
|
|
return errors.New("系统角色不允许修改系统标识")
|
|
}
|
|
}
|
|
|
|
// 检查角色标识是否存在(排除自己)
|
|
exists, err := dao.Role.CheckCodeExists(ctx, req.Slug, req.Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if exists {
|
|
return errors.New("角色标识已存在")
|
|
}
|
|
|
|
// 更新数据
|
|
updateData := g.Map{
|
|
"name": req.Name,
|
|
"slug": req.Slug,
|
|
"level": req.Level,
|
|
"description": req.Description,
|
|
"sort": req.Sort,
|
|
"status": req.Status,
|
|
"is_system": req.IsSystem,
|
|
"updated_at": gtime.Now().String(),
|
|
}
|
|
|
|
return dao.Role.Update(ctx, req.Id, updateData)
|
|
}
|
|
|
|
// GetById 获取角色详情
|
|
func (s *RoleService) GetById(ctx context.Context, id int) (*entity.Role, error) {
|
|
return dao.Role.GetById(ctx, id)
|
|
}
|
|
|
|
// GetList 获取角色列表
|
|
func (s *RoleService) GetList(ctx context.Context, req *dao.RoleListReq) ([]*entity.Role, int, error) {
|
|
return dao.Role.GetList(ctx, req)
|
|
}
|
|
|
|
// GetAll 获取所有角色
|
|
func (s *RoleService) GetAll(ctx context.Context) ([]*entity.Role, error) {
|
|
return dao.Role.GetAll(ctx)
|
|
}
|
|
|
|
// Delete 删除角色
|
|
func (s *RoleService) Delete(ctx context.Context, id int) error {
|
|
// 检查角色是否存在
|
|
role, err := dao.Role.GetById(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if role == nil {
|
|
return errors.New("角色不存在")
|
|
}
|
|
|
|
// 系统角色不允许删除
|
|
if role.IsSystem == 1 {
|
|
return errors.New("系统角色不允许删除")
|
|
}
|
|
|
|
// 检查是否有管理员使用该角色
|
|
// 这里需要查询管理员表,暂时跳过
|
|
|
|
return dao.Role.Delete(ctx, id)
|
|
}
|
|
|
|
// AssignPermissions 为角色分配权限
|
|
func (s *RoleService) AssignPermissions(ctx context.Context, req *RolePermissionReq) error {
|
|
// 检查角色是否存在
|
|
role, err := dao.Role.GetById(ctx, req.RoleId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if role == nil {
|
|
return errors.New("角色不存在")
|
|
}
|
|
|
|
// 验证权限ID是否有效
|
|
if len(req.PermissionIds) > 0 {
|
|
permissions, err := dao.Permission.GetPermissionsByIds(ctx, req.PermissionIds)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(permissions) != len(req.PermissionIds) {
|
|
return errors.New("存在无效的权限ID")
|
|
}
|
|
}
|
|
|
|
return dao.Role.AssignPermissions(ctx, req.RoleId, req.PermissionIds)
|
|
}
|
|
|
|
// GetRolePermissions 获取角色权限
|
|
func (s *RoleService) GetRolePermissions(ctx context.Context, roleId int) ([]*entity.Permission, error) {
|
|
// 检查角色是否存在
|
|
role, err := dao.Role.GetById(ctx, roleId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if role == nil {
|
|
return nil, errors.New("角色不存在")
|
|
}
|
|
|
|
return dao.Role.GetRolePermissions(ctx, roleId)
|
|
}
|
|
|
|
// GetRolePermissionIds 获取角色权限ID列表
|
|
func (s *RoleService) GetRolePermissionIds(ctx context.Context, roleId int) ([]int, error) {
|
|
permissions, err := s.GetRolePermissions(ctx, roleId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var permissionIds []int
|
|
for _, permission := range permissions {
|
|
permissionIds = append(permissionIds, permission.Id)
|
|
}
|
|
|
|
return permissionIds, nil
|
|
}
|
|
|
|
// BatchUpdateStatus 批量更新角色状态
|
|
func (s *RoleService) BatchUpdateStatus(ctx context.Context, ids []int, status int) error {
|
|
if len(ids) == 0 {
|
|
return errors.New("请选择要操作的角色")
|
|
}
|
|
|
|
// 检查是否包含系统角色
|
|
roles, err := dao.Role.GetRolesByIds(ctx, ids)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, role := range roles {
|
|
if role.IsSystem == 1 {
|
|
return errors.New("不能修改系统角色状态")
|
|
}
|
|
}
|
|
|
|
// 批量更新
|
|
successCount := 0
|
|
for _, id := range ids {
|
|
updateData := g.Map{
|
|
"status": status,
|
|
"updated_at": gtime.Now().String(),
|
|
}
|
|
if err := dao.Role.Update(ctx, id, updateData); err != nil {
|
|
g.Log().Errorf(ctx, "批量更新角色状态失败: ID=%d, 错误=%v", id, err)
|
|
} else {
|
|
successCount++
|
|
}
|
|
}
|
|
|
|
if successCount == 0 {
|
|
return errors.New("批量更新失败")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// CopyRole 复制角色
|
|
func (s *RoleService) CopyRole(ctx context.Context, sourceId int, newName, newSlug string) (int, error) {
|
|
// 检查源角色是否存在
|
|
sourceRole, err := dao.Role.GetById(ctx, sourceId)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if sourceRole == nil {
|
|
return 0, errors.New("源角色不存在")
|
|
}
|
|
|
|
// 检查新角色标识是否存在
|
|
exists, err := dao.Role.CheckCodeExists(ctx, newSlug, 0)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if exists {
|
|
return 0, errors.New("角色标识已存在")
|
|
}
|
|
|
|
// 创建新角色
|
|
newRole := &entity.Role{
|
|
Name: newName,
|
|
Slug: newSlug,
|
|
Level: sourceRole.Level,
|
|
Description: sourceRole.Description + " (复制)",
|
|
Sort: sourceRole.Sort,
|
|
Status: 1, // 默认启用
|
|
IsSystem: 0, // 复制的角色不是系统角色
|
|
CreatedAt: gtime.Now().String(),
|
|
UpdatedAt: gtime.Now().String(),
|
|
}
|
|
|
|
newRoleId, err := dao.Role.Create(ctx, newRole)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
// 复制权限
|
|
sourcePermissions, err := dao.Role.GetRolePermissions(ctx, sourceId)
|
|
if err != nil {
|
|
return newRoleId, err // 角色创建成功,但权限复制失败
|
|
}
|
|
|
|
if len(sourcePermissions) > 0 {
|
|
var permissionIds []int
|
|
for _, permission := range sourcePermissions {
|
|
permissionIds = append(permissionIds, permission.Id)
|
|
}
|
|
|
|
err = dao.Role.AssignPermissions(ctx, newRoleId, permissionIds)
|
|
if err != nil {
|
|
g.Log().Errorf(ctx, "复制角色权限失败: 新角色ID=%d, 错误=%v", newRoleId, err)
|
|
}
|
|
}
|
|
|
|
return newRoleId, nil
|
|
}
|
|
|
|
// GetRolesByLevel 根据等级获取角色
|
|
func (s *RoleService) GetRolesByLevel(ctx context.Context, level int) ([]*entity.Role, error) {
|
|
req := &dao.RoleListReq{
|
|
Level: level,
|
|
Status: 1, // 只获取启用的角色
|
|
Page: 0, // 不分页
|
|
PageSize: 0,
|
|
}
|
|
|
|
roles, _, err := dao.Role.GetList(ctx, req)
|
|
return roles, err
|
|
}
|
|
|
|
// CheckRoleExists 检查角色是否存在
|
|
func (s *RoleService) CheckRoleExists(ctx context.Context, id int) (bool, error) {
|
|
role, err := dao.Role.GetById(ctx, id)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return role != nil, nil
|
|
} |