145 lines
5.2 KiB
Go
145 lines
5.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"kra/internal/biz"
|
|
"kra/internal/conf"
|
|
"kra/internal/service/dto"
|
|
)
|
|
|
|
type AccessService struct {
|
|
uc *biz.AccessUsecase
|
|
runtime *conf.Runtime
|
|
}
|
|
|
|
func NewAccessService(uc *biz.AccessUsecase, runtime *conf.Runtime) *AccessService {
|
|
return &AccessService{uc: uc, runtime: runtime}
|
|
}
|
|
|
|
func (s *AccessService) NormalizeRoutePath(path string) string {
|
|
config := s.runtime.Admin()
|
|
if config == nil || config.RouterPrefix == "" {
|
|
return path
|
|
}
|
|
prefix := strings.TrimSuffix(config.RouterPrefix, "/")
|
|
normalized := strings.TrimPrefix(path, prefix)
|
|
if normalized == "" {
|
|
return "/"
|
|
}
|
|
return normalized
|
|
}
|
|
func (s *AccessService) Authorize(ctx context.Context, aid uint, path, method string) (bool, error) {
|
|
return s.uc.Authorize(ctx, aid, path, method)
|
|
}
|
|
|
|
func authorityDTO(v *biz.Authority) map[string]any {
|
|
out := convertAuthority(*v)
|
|
children := make([]map[string]any, 0, len(v.Children))
|
|
for _, child := range v.Children {
|
|
children = append(children, authorityDTO(child))
|
|
}
|
|
out["children"] = children
|
|
return out
|
|
}
|
|
func (s *AccessService) Authorities(ctx context.Context) ([]map[string]any, error) {
|
|
items, err := s.uc.AuthorityTree(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
roots := make([]map[string]any, 0, len(items))
|
|
for _, v := range items {
|
|
roots = append(roots, authorityDTO(v))
|
|
}
|
|
return roots, nil
|
|
}
|
|
func (s *AccessService) CreateAuthority(ctx context.Context, v *biz.Authority) error {
|
|
return s.uc.CreateAuthority(ctx, v)
|
|
}
|
|
func (s *AccessService) CopyAuthority(ctx context.Context, sourceID uint, v *biz.Authority) error {
|
|
return s.uc.CopyAuthority(ctx, sourceID, v)
|
|
}
|
|
func (s *AccessService) UpdateAuthority(ctx context.Context, v *biz.Authority) error {
|
|
return s.uc.UpdateAuthority(ctx, v)
|
|
}
|
|
func (s *AccessService) DeleteAuthority(ctx context.Context, id uint) error {
|
|
return s.uc.DeleteAuthority(ctx, id)
|
|
}
|
|
func authorityDomain(req *dto.AuthorityRequest) *biz.Authority {
|
|
return &biz.Authority{AuthorityID: req.AuthorityID, AuthorityName: req.AuthorityName, ParentID: req.ParentID, DataScope: req.DataScope, DefaultRouter: req.DefaultRouter}
|
|
}
|
|
func authorityResponse(value *biz.Authority) *dto.AuthorityResponse {
|
|
return &dto.AuthorityResponse{AuthorityID: value.AuthorityID, AuthorityName: value.AuthorityName, ParentID: value.ParentID, DataScope: value.DataScope, DefaultRouter: value.DefaultRouter}
|
|
}
|
|
func (s *AccessService) CreateAuthorityRequest(ctx context.Context, req *dto.AuthorityRequest) (*dto.AuthorityResponse, error) {
|
|
value := authorityDomain(req)
|
|
if err := s.CreateAuthority(ctx, value); err != nil {
|
|
return nil, err
|
|
}
|
|
return authorityResponse(value), nil
|
|
}
|
|
func (s *AccessService) CopyAuthorityRequest(ctx context.Context, req *dto.CopyAuthorityRequest) (*dto.AuthorityResponse, error) {
|
|
value := authorityDomain(&req.Authority)
|
|
if err := s.CopyAuthority(ctx, req.OldAuthorityID, value); err != nil {
|
|
return nil, err
|
|
}
|
|
return authorityResponse(value), nil
|
|
}
|
|
func (s *AccessService) UpdateAuthorityRequest(ctx context.Context, req *dto.AuthorityRequest) (*dto.AuthorityResponse, error) {
|
|
value := authorityDomain(req)
|
|
if err := s.UpdateAuthority(ctx, value); err != nil {
|
|
return nil, err
|
|
}
|
|
return authorityResponse(value), nil
|
|
}
|
|
func (s *AccessService) SetAuthorityUsers(ctx context.Context, id uint, ids []uint) error {
|
|
return s.uc.SetAuthorityUsers(ctx, id, ids)
|
|
}
|
|
func (s *AccessService) AuthorityUserIDs(ctx context.Context, id uint) ([]uint, error) {
|
|
return s.uc.AuthorityUserIDs(ctx, id)
|
|
}
|
|
func (s *AccessService) SetDataScope(ctx context.Context, id uint, scope int, ids []uint) error {
|
|
return s.uc.SetDataScope(ctx, id, scope, ids)
|
|
}
|
|
func (s *AccessService) DataScopeDepartmentIDs(ctx context.Context, id uint) ([]uint, error) {
|
|
return s.uc.DataScopeDepartmentIDs(ctx, id)
|
|
}
|
|
func (s *AccessService) ResolveDataScope(ctx context.Context, authorityID, userID uint) (biz.DataScope, error) {
|
|
return s.uc.ResolveDataScope(ctx, authorityID, userID)
|
|
}
|
|
func (s *AccessService) ContextWithDataScope(ctx context.Context, authorityID, userID uint) (context.Context, error) {
|
|
scope, err := s.ResolveDataScope(ctx, authorityID, userID)
|
|
if err != nil {
|
|
return ctx, err
|
|
}
|
|
return biz.NewDataScopeContext(ctx, scope), nil
|
|
}
|
|
|
|
func (s *AccessService) Buttons(ctx context.Context, id uint) ([]map[string]any, error) {
|
|
items, err := s.uc.Buttons(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]map[string]any, 0, len(items))
|
|
for _, v := range items {
|
|
out = append(out, map[string]any{"ID": v.ID, "name": v.Name, "desc": v.Description, "sysBaseMenuID": v.MenuID})
|
|
}
|
|
return out, nil
|
|
}
|
|
func (s *AccessService) SetAuthorityButtons(ctx context.Context, id uint, v map[uint][]uint) error {
|
|
return s.uc.SetAuthorityButtons(ctx, id, v)
|
|
}
|
|
func (s *AccessService) AuthorityButtonIDs(ctx context.Context, id uint) ([]uint, error) {
|
|
return s.uc.AuthorityButtonIDs(ctx, id)
|
|
}
|
|
func (s *AccessService) SelectedButtons(ctx context.Context, aid, menuID uint) ([]uint, error) {
|
|
return s.uc.SelectedButtons(ctx, aid, menuID)
|
|
}
|
|
func (s *AccessService) SetSelectedButtons(ctx context.Context, aid, menuID uint, ids []uint) error {
|
|
return s.uc.SetSelectedButtons(ctx, aid, menuID, ids)
|
|
}
|
|
func (s *AccessService) CanRemoveButton(ctx context.Context, id uint) (bool, error) {
|
|
return s.uc.CanRemoveButton(ctx, id)
|
|
}
|