Files
portal-mini-go/internal/service/admin/rbac/permission_service.go
T
2026-10-25 22:47:13 +08:00

136 lines
3.6 KiB
Go

package rbac
import (
"context"
"portal-mini-go/internal/model/admin"
"portal-mini-go/internal/pkg/errors"
adminrepo "portal-mini-go/internal/repository/admin"
)
// PermissionService 权限(接口 / 菜单)业务:CRUD。
type PermissionService struct {
repo PermissionRepository
invalidator CacheInvalidator
}
func NewPermissionService(repo PermissionRepository, invalidator CacheInvalidator) *PermissionService {
return &PermissionService{repo: repo, invalidator: invalidator}
}
// CreatePermReq 新建权限请求。
// Type 取 admin.PermTypeAPI / admin.PermTypeMenu;菜单需指定 ParentID 与 Sort。
type CreatePermReq struct {
Code string `json:"code" binding:"required"`
Name string `json:"name" binding:"required"`
Type string `json:"type" binding:"required"`
Method string `json:"method"`
Path string `json:"path"`
ParentID uint64 `json:"parent_id"`
Sort int `json:"sort"`
}
// Create 新建权限,code 唯一。
func (s *PermissionService) Create(ctx context.Context, req CreatePermReq) (*admin.Permission, error) {
if req.Type != admin.PermTypeAPI && req.Type != admin.PermTypeMenu {
return nil, errors.New(40000, "非法的权限类型")
}
p := &admin.Permission{
Code: req.Code,
Name: req.Name,
Type: req.Type,
Method: req.Method,
Path: req.Path,
ParentID: req.ParentID,
Sort: req.Sort,
}
if err := s.repo.Create(ctx, p); err != nil {
return nil, err
}
s.invalidator.InvalidateAll(ctx)
return p, nil
}
// PermListReq 权限列表过滤条件。
type PermListReq struct {
Type string
Keyword string
Page int
Size int
}
// List 分页查询权限,Type 为空表示全部类型。
func (s *PermissionService) List(ctx context.Context, req PermListReq) ([]admin.Permission, int64, error) {
page, size := req.Page, req.Size
if page <= 0 {
page = 1
}
if size <= 0 {
size = 20
}
return s.repo.List(ctx, adminrepo.PermListFilter{
Type: req.Type,
Keyword: req.Keyword,
Page: page,
Size: size,
})
}
// GetByID 按 ID 查询权限。
func (s *PermissionService) GetByID(ctx context.Context, id uint64) (*admin.Permission, error) {
return s.repo.GetByID(ctx, id)
}
// UpdatePermReq 编辑权限请求;指针区分“未提供”与“置空”。
type UpdatePermReq struct {
Name *string `json:"name"`
Type *string `json:"type"`
Method *string `json:"method"`
Path *string `json:"path"`
ParentID *uint64 `json:"parent_id"`
Sort *int `json:"sort"`
}
// Update 局部更新权限。
func (s *PermissionService) Update(ctx context.Context, id uint64, req UpdatePermReq) (*admin.Permission, error) {
cols := map[string]interface{}{}
if req.Name != nil {
cols["name"] = *req.Name
}
if req.Type != nil {
if *req.Type != admin.PermTypeAPI && *req.Type != admin.PermTypeMenu {
return nil, errors.New(40000, "非法的权限类型")
}
cols["type"] = *req.Type
}
if req.Method != nil {
cols["method"] = *req.Method
}
if req.Path != nil {
cols["path"] = *req.Path
}
if req.ParentID != nil {
cols["parent_id"] = *req.ParentID
}
if req.Sort != nil {
cols["sort"] = *req.Sort
}
if len(cols) == 0 {
return s.repo.GetByID(ctx, id)
}
if err := s.repo.Update(ctx, id, cols); err != nil {
return nil, err
}
s.invalidator.InvalidateAll(ctx)
return s.repo.GetByID(ctx, id)
}
// Delete 删除权限(清理关联并失效全部缓存)。
func (s *PermissionService) Delete(ctx context.Context, id uint64) error {
if err := s.repo.Delete(ctx, id); err != nil {
return err
}
s.invalidator.InvalidateAll(ctx)
return nil
}