234 lines
7.1 KiB
Go
234 lines
7.1 KiB
Go
package auth
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
"github.com/gogf/gf/v2/os/gtime"
|
||
"github.com/gogf/gf/v2/util/gconv"
|
||
"nl-video-api/internal/dao"
|
||
"nl-video-api/internal/model/entity"
|
||
)
|
||
|
||
// PermissionService 权限服务
|
||
type PermissionService struct{}
|
||
|
||
var Permission = &PermissionService{}
|
||
|
||
// PermissionCreateReq 创建权限请求
|
||
type PermissionCreateReq struct {
|
||
Name string `json:"name" v:"required|length:2,50#权限名称不能为空|权限名称长度为2-50位"`
|
||
Slug string `json:"slug" v:"required|length:2,100#权限标识不能为空|权限标识长度为2-100位"`
|
||
Type string `json:"type" v:"required|in:1,2,3#权限类型不能为空|权限类型只能是1,2,3"`
|
||
ParentId int `json:"parent_id" v:"min:0#父级ID不能小于0"`
|
||
Path string `json:"path"` // 路由路径
|
||
Component string `json:"component"` // 组件路径
|
||
Icon string `json:"icon"` // 图标
|
||
ApiPath string `json:"api_path"` // API路径
|
||
Method string `json:"method"` // 请求方法
|
||
Description string `json:"description"` // 权限描述
|
||
Sort int `json:"sort"` // 排序
|
||
Status int `json:"status" v:"in:0,1#状态只能为0或1"`
|
||
}
|
||
|
||
// PermissionUpdateReq 更新权限请求
|
||
type PermissionUpdateReq 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,100#权限标识不能为空|权限标识长度为2-100位"`
|
||
Type string `json:"type" v:"required|in:1,2,3#权限类型不能为空|权限类型只能是1,2,3"`
|
||
ParentId int `json:"parent_id" v:"min:0#父级ID不能小于0"`
|
||
Path string `json:"path"` // 路由路径
|
||
Component string `json:"component"` // 组件路径
|
||
Icon string `json:"icon"` // 图标
|
||
ApiPath string `json:"api_path"` // API路径
|
||
Method string `json:"method"` // 请求方法
|
||
Description string `json:"description"` // 权限描述
|
||
Sort int `json:"sort"` // 排序
|
||
Status int `json:"status" v:"in:0,1#状态只能为0或1"`
|
||
}
|
||
|
||
// Create 创建权限
|
||
func (s *PermissionService) Create(ctx context.Context, req *PermissionCreateReq) (int, error) {
|
||
// 检查权限标识是否存在
|
||
exists, err := dao.Permission.CheckCodeExists(ctx, req.Slug, 0)
|
||
if err != nil {
|
||
return 0, err
|
||
}
|
||
if exists {
|
||
return 0, errors.New("权限标识已存在")
|
||
}
|
||
|
||
// 如果有父级ID,检查父级是否存在
|
||
if req.ParentId > 0 {
|
||
parent, err := dao.Permission.GetById(ctx, req.ParentId)
|
||
if err != nil {
|
||
return 0, err
|
||
}
|
||
if parent == nil {
|
||
return 0, errors.New("父级权限不存在")
|
||
}
|
||
}
|
||
|
||
// 创建权限数据
|
||
permission := &entity.Permission{
|
||
Name: req.Name,
|
||
Slug: req.Slug,
|
||
Type: gconv.Int(req.Type),
|
||
ParentId: req.ParentId,
|
||
Path: req.Path,
|
||
Component: req.Component,
|
||
Icon: req.Icon,
|
||
ApiPath: req.ApiPath,
|
||
Method: req.Method,
|
||
Description: req.Description,
|
||
Sort: req.Sort,
|
||
Status: req.Status,
|
||
CreatedAt: gtime.Now().String(),
|
||
UpdatedAt: gtime.Now().String(),
|
||
}
|
||
|
||
return dao.Permission.Create(ctx, permission)
|
||
}
|
||
|
||
// Update 更新权限
|
||
func (s *PermissionService) Update(ctx context.Context, req *PermissionUpdateReq) error {
|
||
// 检查权限是否存在
|
||
permission, err := dao.Permission.GetById(ctx, req.Id)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if permission == nil {
|
||
return errors.New("权限不存在")
|
||
}
|
||
|
||
// 检查权限标识是否存在(排除自己)
|
||
exists, err := dao.Permission.CheckCodeExists(ctx, req.Slug, req.Id)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if exists {
|
||
return errors.New("权限标识已存在")
|
||
}
|
||
|
||
// 如果有父级ID,检查父级是否存在
|
||
if req.ParentId > 0 {
|
||
parent, err := dao.Permission.GetById(ctx, req.ParentId)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if parent == nil {
|
||
return errors.New("父级权限不存在")
|
||
}
|
||
|
||
// 不能将自己设为父级
|
||
if req.ParentId == req.Id {
|
||
return errors.New("不能将自己设为父级")
|
||
}
|
||
}
|
||
|
||
// 更新数据
|
||
updateData := g.Map{
|
||
"name": req.Name,
|
||
"slug": req.Slug,
|
||
"type": gconv.Int(req.Type),
|
||
"parent_id": req.ParentId,
|
||
"path": req.Path,
|
||
"component": req.Component,
|
||
"icon": req.Icon,
|
||
"api_path": req.ApiPath,
|
||
"method": req.Method,
|
||
"description": req.Description,
|
||
"sort": req.Sort,
|
||
"status": req.Status,
|
||
"updated_at": gtime.Now().String(),
|
||
}
|
||
|
||
return dao.Permission.Update(ctx, req.Id, updateData)
|
||
}
|
||
|
||
// GetById 获取权限详情
|
||
func (s *PermissionService) GetById(ctx context.Context, id int) (*entity.Permission, error) {
|
||
return dao.Permission.GetById(ctx, id)
|
||
}
|
||
|
||
// GetList 获取权限列表
|
||
func (s *PermissionService) GetList(ctx context.Context, req *dao.PermissionListReq) ([]*entity.Permission, int, error) {
|
||
return dao.Permission.GetList(ctx, req)
|
||
}
|
||
|
||
// GetTree 获取权限树形结构
|
||
func (s *PermissionService) GetTree(ctx context.Context) ([]*entity.Permission, error) {
|
||
return dao.Permission.GetTree(ctx)
|
||
}
|
||
|
||
// Delete 删除权限
|
||
func (s *PermissionService) Delete(ctx context.Context, id int) error {
|
||
// 检查权限是否存在
|
||
permission, err := dao.Permission.GetById(ctx, id)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if permission == nil {
|
||
return errors.New("权限不存在")
|
||
}
|
||
|
||
// 检查是否有子权限
|
||
children, err := dao.Permission.GetByParentId(ctx, id)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if len(children) > 0 {
|
||
return errors.New("存在子权限,无法删除")
|
||
}
|
||
|
||
return dao.Permission.Delete(ctx, id)
|
||
}
|
||
|
||
// GetMenuPermissions 获取菜单权限
|
||
func (s *PermissionService) GetMenuPermissions(ctx context.Context) ([]*entity.Permission, error) {
|
||
return dao.Permission.GetMenuPermissions(ctx)
|
||
}
|
||
|
||
// GetApiPermissions 获取API权限
|
||
func (s *PermissionService) GetApiPermissions(ctx context.Context) ([]*entity.Permission, error) {
|
||
return dao.Permission.GetApiPermissions(ctx)
|
||
}
|
||
|
||
// GetUserPermissions 获取用户权限
|
||
func (s *PermissionService) GetUserPermissions(ctx context.Context, userId int) ([]*entity.Permission, error) {
|
||
return dao.Permission.GetUserPermissions(ctx, userId)
|
||
}
|
||
|
||
// CheckUserPermission 检查用户是否有指定权限
|
||
func (s *PermissionService) CheckUserPermission(ctx context.Context, userId int, permissionSlug string) (bool, error) {
|
||
permissions, err := s.GetUserPermissions(ctx, userId)
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
|
||
for _, permission := range permissions {
|
||
if permission.Slug == permissionSlug {
|
||
return true, nil
|
||
}
|
||
}
|
||
|
||
return false, nil
|
||
}
|
||
|
||
// CheckApiPermission 检查API权限
|
||
func (s *PermissionService) CheckApiPermission(ctx context.Context, userId int, apiPath, method string) (bool, error) {
|
||
permissions, err := s.GetUserPermissions(ctx, userId)
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
|
||
for _, permission := range permissions {
|
||
if permission.Type == 3 && permission.ApiPath == apiPath && permission.Method == method {
|
||
return true, nil
|
||
}
|
||
}
|
||
|
||
return false, nil
|
||
} |