378 lines
13 KiB
Go
378 lines
13 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"kra/internal/biz"
|
|
)
|
|
|
|
type AccessService struct{ uc *biz.AccessUsecase }
|
|
|
|
func NewAccessService(uc *biz.AccessUsecase) *AccessService { return &AccessService{uc: uc} }
|
|
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 { return convertAuthority(*v) }
|
|
func apiDTO(v *biz.API) map[string]any {
|
|
return map[string]any{"ID": v.ID, "CreatedAt": v.CreatedAt, "UpdatedAt": v.UpdatedAt, "DeletedAt": nil, "path": v.Path, "description": v.Description, "apiGroup": v.APIGroup, "method": v.Method}
|
|
}
|
|
func departmentDTO(v *biz.Department) map[string]any {
|
|
children := make([]map[string]any, 0, len(v.Children))
|
|
for _, c := range v.Children {
|
|
children = append(children, departmentDTO(c))
|
|
}
|
|
return map[string]any{"ID": v.ID, "CreatedAt": v.CreatedAt, "UpdatedAt": v.UpdatedAt, "DeletedAt": nil, "name": v.Name, "parentId": v.ParentID, "ancestors": v.Ancestors, "sort": v.Sort, "leaderId": v.LeaderID, "leader": nil, "status": v.Status, "children": children, "namePath": ""}
|
|
}
|
|
func positionDTO(v *biz.Position) map[string]any {
|
|
return map[string]any{"ID": v.ID, "CreatedAt": v.CreatedAt, "UpdatedAt": v.UpdatedAt, "DeletedAt": nil, "name": v.Name, "code": v.Code, "sort": v.Sort, "status": v.Status, "remark": v.Remark}
|
|
}
|
|
|
|
func (s *AccessService) Authorities(ctx context.Context) ([]map[string]any, error) {
|
|
items, err := s.uc.Repo().ListAuthorities(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
byID := make(map[uint]map[string]any, len(items))
|
|
for _, v := range items {
|
|
byID[v.AuthorityID] = authorityDTO(v)
|
|
}
|
|
roots := make([]map[string]any, 0)
|
|
for _, v := range items {
|
|
item := byID[v.AuthorityID]
|
|
if v.ParentID != nil && *v.ParentID != 0 && byID[*v.ParentID] != nil {
|
|
parent := byID[*v.ParentID]
|
|
children, _ := parent["children"].([]map[string]any)
|
|
parent["children"] = append(children, item)
|
|
} else {
|
|
roots = append(roots, item)
|
|
}
|
|
}
|
|
return roots, nil
|
|
}
|
|
func (s *AccessService) CreateAuthority(ctx context.Context, v *biz.Authority) error {
|
|
return s.uc.Repo().CreateAuthority(ctx, v)
|
|
}
|
|
func (s *AccessService) CopyAuthority(ctx context.Context, sourceID uint, v *biz.Authority) error {
|
|
return s.uc.Repo().CopyAuthority(ctx, sourceID, v)
|
|
}
|
|
func (s *AccessService) UpdateAuthority(ctx context.Context, v *biz.Authority) error {
|
|
return s.uc.Repo().UpdateAuthority(ctx, v)
|
|
}
|
|
func (s *AccessService) DeleteAuthority(ctx context.Context, id uint) error {
|
|
return s.uc.Repo().DeleteAuthority(ctx, id)
|
|
}
|
|
func (s *AccessService) SetAuthorityMenus(ctx context.Context, id uint, ids []uint) error {
|
|
return s.uc.Repo().SetAuthorityMenus(ctx, id, ids)
|
|
}
|
|
func (s *AccessService) AuthorityMenuIDs(ctx context.Context, id uint) ([]uint, error) {
|
|
return s.uc.Repo().AuthorityMenuIDs(ctx, id)
|
|
}
|
|
func (s *AccessService) AuthorityMenus(ctx context.Context, id uint) ([]map[string]any, error) {
|
|
ids, err := s.uc.Repo().AuthorityMenuIDs(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
selected := make(map[uint]bool, len(ids))
|
|
for _, value := range ids {
|
|
selected[value] = true
|
|
}
|
|
menus, err := s.uc.Repo().ListMenus(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]map[string]any, 0, len(ids))
|
|
for _, menu := range menus {
|
|
if selected[menu.ID] {
|
|
out = append(out, map[string]any{"menuId": menu.ID, "authorityId": id, "sysBaseMenu": convertMenu(menu), "parentId": menu.ParentID})
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
func (s *AccessService) SetAuthorityUsers(ctx context.Context, id uint, ids []uint) error {
|
|
return s.uc.Repo().SetAuthorityUsers(ctx, id, ids)
|
|
}
|
|
func (s *AccessService) AuthorityUserIDs(ctx context.Context, id uint) ([]uint, error) {
|
|
return s.uc.Repo().AuthorityUserIDs(ctx, id)
|
|
}
|
|
func (s *AccessService) SetDataScope(ctx context.Context, id uint, scope int, ids []uint) error {
|
|
return s.uc.Repo().SetDataScope(ctx, id, scope, ids)
|
|
}
|
|
func (s *AccessService) DataScopeDepartmentIDs(ctx context.Context, id uint) ([]uint, error) {
|
|
return s.uc.Repo().DataScopeDepartmentIDs(ctx, id)
|
|
}
|
|
func (s *AccessService) ResolveDataScope(ctx context.Context, authorityID, userID uint) (biz.DataScope, error) {
|
|
return s.uc.Repo().ResolveDataScope(ctx, authorityID, userID)
|
|
}
|
|
|
|
func (s *AccessService) Menus(ctx context.Context) ([]map[string]any, error) {
|
|
items, err := s.uc.Repo().ListMenus(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]map[string]any, 0, len(items))
|
|
for _, v := range items {
|
|
v.Buttons, err = s.uc.Repo().Buttons(ctx, v.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, convertMenu(v))
|
|
}
|
|
return out, nil
|
|
}
|
|
func (s *AccessService) MenuTree(ctx context.Context) ([]map[string]any, error) {
|
|
items, err := s.uc.Repo().ListMenus(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
byID := make(map[uint]*biz.Menu, len(items))
|
|
for _, item := range items {
|
|
item.Buttons, err = s.uc.Repo().Buttons(ctx, item.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
item.Children = []*biz.Menu{}
|
|
byID[item.ID] = item
|
|
}
|
|
roots := make([]*biz.Menu, 0)
|
|
for _, item := range items {
|
|
if parent := byID[item.ParentID]; parent != nil {
|
|
parent.Children = append(parent.Children, item)
|
|
} else {
|
|
roots = append(roots, item)
|
|
}
|
|
}
|
|
out := make([]map[string]any, 0, len(roots))
|
|
for _, item := range roots {
|
|
out = append(out, convertMenu(item))
|
|
}
|
|
return out, nil
|
|
}
|
|
func (s *AccessService) CreateMenu(ctx context.Context, v *biz.Menu) error {
|
|
return s.uc.Repo().CreateMenu(ctx, v)
|
|
}
|
|
func (s *AccessService) UpdateMenu(ctx context.Context, v *biz.Menu) error {
|
|
return s.uc.Repo().UpdateMenu(ctx, v)
|
|
}
|
|
func (s *AccessService) DeleteMenu(ctx context.Context, id uint) error {
|
|
return s.uc.Repo().DeleteMenu(ctx, id)
|
|
}
|
|
func (s *AccessService) Menu(ctx context.Context, id uint) (map[string]any, error) {
|
|
v, err := s.uc.Repo().FindMenu(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
v.Buttons, err = s.uc.Repo().Buttons(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return convertMenu(v), nil
|
|
}
|
|
func (s *AccessService) MenuRoleIDs(ctx context.Context, id uint) ([]uint, error) {
|
|
return s.uc.Repo().MenuRoleIDs(ctx, id)
|
|
}
|
|
func (s *AccessService) SetMenuRoles(ctx context.Context, id uint, ids []uint) error {
|
|
return s.uc.Repo().SetMenuRoles(ctx, id, ids)
|
|
}
|
|
func (s *AccessService) ReplaceMenuButtons(ctx context.Context, menuID uint, buttons []*biz.MenuButton) error {
|
|
return s.uc.Repo().ReplaceMenuButtons(ctx, menuID, buttons)
|
|
}
|
|
|
|
func (s *AccessService) APIs(ctx context.Context, page, size int, q *biz.API) ([]map[string]any, int64, error) {
|
|
items, total, err := s.uc.Repo().ListAPIs(ctx, page, size, q)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out := make([]map[string]any, 0, len(items))
|
|
for _, v := range items {
|
|
out = append(out, apiDTO(v))
|
|
}
|
|
return out, total, nil
|
|
}
|
|
func (s *AccessService) CreateAPI(ctx context.Context, v *biz.API) error {
|
|
return s.uc.Repo().CreateAPI(ctx, v)
|
|
}
|
|
func (s *AccessService) UpdateAPI(ctx context.Context, v *biz.API) error {
|
|
return s.uc.Repo().UpdateAPI(ctx, v)
|
|
}
|
|
func (s *AccessService) DeleteAPIs(ctx context.Context, ids []uint) error {
|
|
return s.uc.Repo().DeleteAPIs(ctx, ids)
|
|
}
|
|
func (s *AccessService) API(ctx context.Context, id uint) (map[string]any, error) {
|
|
v, err := s.uc.Repo().FindAPI(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return apiDTO(v), nil
|
|
}
|
|
func (s *AccessService) APIRoleIDs(ctx context.Context, id uint) ([]uint, error) {
|
|
return s.uc.Repo().APIRoleIDs(ctx, id)
|
|
}
|
|
func (s *AccessService) SetAPIRoles(ctx context.Context, id uint, ids []uint) error {
|
|
return s.uc.Repo().SetAPIRoles(ctx, id, ids)
|
|
}
|
|
func (s *AccessService) PolicyPaths(ctx context.Context, id uint) ([]map[string]any, error) {
|
|
items, err := s.uc.Repo().PolicyPaths(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{"path": v.Path, "method": v.Method})
|
|
}
|
|
return out, nil
|
|
}
|
|
func (s *AccessService) SetPolicyPaths(ctx context.Context, id uint, paths []*biz.API) error {
|
|
return s.uc.Repo().SetPolicyPaths(ctx, id, paths)
|
|
}
|
|
func (s *AccessService) SyncAPIs(ctx context.Context, routes []*biz.API) (map[string]any, error) {
|
|
stored, _, err := s.uc.Repo().ListAPIs(ctx, 1, 100000, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ignored, err := s.uc.Repo().IgnoredAPIs(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
key := func(v *biz.API) string { return strings.ToUpper(v.Method) + " " + v.Path }
|
|
ignoreSet := make(map[string]bool, len(ignored))
|
|
for _, item := range ignored {
|
|
ignoreSet[key(item)] = true
|
|
}
|
|
routeSet := make(map[string]*biz.API, len(routes))
|
|
for _, item := range routes {
|
|
if !ignoreSet[key(item)] {
|
|
routeSet[key(item)] = item
|
|
}
|
|
}
|
|
storedSet := make(map[string]*biz.API, len(stored))
|
|
for _, item := range stored {
|
|
storedSet[key(item)] = item
|
|
}
|
|
added := make([]map[string]any, 0)
|
|
deleted := make([]map[string]any, 0)
|
|
ignoredDTO := make([]map[string]any, 0, len(ignored))
|
|
for k, item := range routeSet {
|
|
if storedSet[k] == nil {
|
|
added = append(added, apiDTO(item))
|
|
}
|
|
}
|
|
for k, item := range storedSet {
|
|
if routeSet[k] == nil && !ignoreSet[k] {
|
|
deleted = append(deleted, apiDTO(item))
|
|
}
|
|
}
|
|
for _, item := range ignored {
|
|
ignoredDTO = append(ignoredDTO, apiDTO(item))
|
|
}
|
|
return map[string]any{"newApis": added, "deleteApis": deleted, "ignoreApis": ignoredDTO}, nil
|
|
}
|
|
func (s *AccessService) SetAPIIgnored(ctx context.Context, path, method string, ignored bool) error {
|
|
return s.uc.Repo().SetAPIIgnored(ctx, path, method, ignored)
|
|
}
|
|
func (s *AccessService) ApplyAPISync(ctx context.Context, added, deleted []*biz.API) error {
|
|
return s.uc.Repo().ApplyAPISync(ctx, added, deleted)
|
|
}
|
|
|
|
func (s *AccessService) Buttons(ctx context.Context, id uint) ([]map[string]any, error) {
|
|
items, err := s.uc.Repo().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.Repo().SetAuthorityButtons(ctx, id, v)
|
|
}
|
|
func (s *AccessService) AuthorityButtonIDs(ctx context.Context, id uint) ([]uint, error) {
|
|
return s.uc.Repo().AuthorityButtonIDs(ctx, id)
|
|
}
|
|
func (s *AccessService) SelectedButtons(ctx context.Context, aid, menuID uint) ([]uint, error) {
|
|
return s.uc.Repo().SelectedButtons(ctx, aid, menuID)
|
|
}
|
|
func (s *AccessService) SetSelectedButtons(ctx context.Context, aid, menuID uint, ids []uint) error {
|
|
return s.uc.Repo().SetSelectedButtons(ctx, aid, menuID, ids)
|
|
}
|
|
func (s *AccessService) CanRemoveButton(ctx context.Context, id uint) (bool, error) {
|
|
return s.uc.Repo().CanRemoveButton(ctx, id)
|
|
}
|
|
|
|
func (s *AccessService) Departments(ctx context.Context, name string) ([]map[string]any, error) {
|
|
items, err := s.uc.Repo().ListDepartments(ctx, name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]map[string]any, 0, len(items))
|
|
for _, v := range items {
|
|
out = append(out, departmentDTO(v))
|
|
}
|
|
return out, nil
|
|
}
|
|
func (s *AccessService) CreateDepartment(ctx context.Context, v *biz.Department) error {
|
|
return s.uc.Repo().CreateDepartment(ctx, v)
|
|
}
|
|
func (s *AccessService) UpdateDepartment(ctx context.Context, v *biz.Department) error {
|
|
return s.uc.Repo().UpdateDepartment(ctx, v)
|
|
}
|
|
func (s *AccessService) DeleteDepartment(ctx context.Context, id uint) error {
|
|
return s.uc.Repo().DeleteDepartment(ctx, id)
|
|
}
|
|
func (s *AccessService) Department(ctx context.Context, id uint) (map[string]any, error) {
|
|
v, err := s.uc.Repo().FindDepartment(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return departmentDTO(v), nil
|
|
}
|
|
func (s *AccessService) DepartmentUserIDs(ctx context.Context, id uint) ([]uint, error) {
|
|
return s.uc.Repo().DepartmentUserIDs(ctx, id)
|
|
}
|
|
func (s *AccessService) SetDepartmentUsers(ctx context.Context, id uint, ids []uint) error {
|
|
return s.uc.Repo().SetDepartmentUsers(ctx, id, ids)
|
|
}
|
|
func (s *AccessService) SetUserDepartments(ctx context.Context, id uint, ids []uint, primary uint) error {
|
|
return s.uc.Repo().SetUserDepartments(ctx, id, ids, primary)
|
|
}
|
|
|
|
func (s *AccessService) Positions(ctx context.Context, page, size int, q *biz.Position) ([]map[string]any, int64, error) {
|
|
items, total, err := s.uc.Repo().ListPositions(ctx, page, size, q)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out := make([]map[string]any, 0, len(items))
|
|
for _, v := range items {
|
|
out = append(out, positionDTO(v))
|
|
}
|
|
return out, total, nil
|
|
}
|
|
func (s *AccessService) CreatePosition(ctx context.Context, v *biz.Position) error {
|
|
return s.uc.Repo().CreatePosition(ctx, v)
|
|
}
|
|
func (s *AccessService) UpdatePosition(ctx context.Context, v *biz.Position) error {
|
|
return s.uc.Repo().UpdatePosition(ctx, v)
|
|
}
|
|
func (s *AccessService) DeletePosition(ctx context.Context, id uint) error {
|
|
return s.uc.Repo().DeletePosition(ctx, id)
|
|
}
|
|
func (s *AccessService) Position(ctx context.Context, id uint) (map[string]any, error) {
|
|
v, err := s.uc.Repo().FindPosition(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return positionDTO(v), nil
|
|
}
|
|
func (s *AccessService) PositionUserIDs(ctx context.Context, id uint) ([]uint, error) {
|
|
return s.uc.Repo().PositionUserIDs(ctx, id)
|
|
}
|
|
func (s *AccessService) SetPositionUsers(ctx context.Context, id uint, ids []uint) error {
|
|
return s.uc.Repo().SetPositionUsers(ctx, id, ids)
|
|
}
|
|
func (s *AccessService) SetUserPositions(ctx context.Context, id uint, ids []uint) error {
|
|
return s.uc.Repo().SetUserPositions(ctx, id, ids)
|
|
}
|