56 lines
1.8 KiB
Go
56 lines
1.8 KiB
Go
package biz
|
|
|
|
import "context"
|
|
|
|
type AuthorityAccessRepo interface {
|
|
CreateAuthority(context.Context, *Authority) error
|
|
CopyAuthority(context.Context, uint, *Authority) error
|
|
UpdateAuthority(context.Context, *Authority) error
|
|
DeleteAuthority(context.Context, uint) error
|
|
ListAuthorities(context.Context) ([]*Authority, error)
|
|
SetAuthorityUsers(context.Context, uint, []uint) error
|
|
AuthorityUserIDs(context.Context, uint) ([]uint, error)
|
|
SetDataScope(context.Context, uint, int, []uint) error
|
|
DataScopeDepartmentIDs(context.Context, uint) ([]uint, error)
|
|
ResolveDataScope(context.Context, uint, uint) (DataScope, error)
|
|
}
|
|
|
|
type AuthorityUsecase struct{ AuthorityAccessRepo }
|
|
|
|
func NewAuthorityUsecase(repo AuthorityAccessRepo) *AuthorityUsecase {
|
|
return &AuthorityUsecase{AuthorityAccessRepo: repo}
|
|
}
|
|
|
|
func (uc *AuthorityUsecase) AuthorityTree(ctx context.Context) ([]*Authority, error) {
|
|
items, err := uc.ListAuthorities(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
byID := make(map[uint]*Authority, len(items))
|
|
for _, item := range items {
|
|
item.Children = nil
|
|
byID[item.AuthorityID] = item
|
|
}
|
|
roots := make([]*Authority, 0)
|
|
for _, item := range items {
|
|
if item.ParentID != nil && *item.ParentID != 0 && byID[*item.ParentID] != nil {
|
|
parent := byID[*item.ParentID]
|
|
parent.Children = append(parent.Children, item)
|
|
} else if item.ParentID == nil || *item.ParentID == 0 {
|
|
roots = append(roots, item)
|
|
}
|
|
}
|
|
// In GVA strict-authority mode a non-root actor receives its direct
|
|
// children as the top-level result even though their parent is omitted.
|
|
if len(roots) == 0 {
|
|
if actor, ok := ActorFromContext(ctx); ok {
|
|
for _, item := range items {
|
|
if item.ParentID != nil && *item.ParentID == actor.AuthorityID {
|
|
roots = append(roots, item)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return roots, nil
|
|
}
|