Created
September 28, 2022 11:55
-
-
Save eminetto/c80cbb31ab8a42c4dd0b3511c54fe59c to your computer and use it in GitHub Desktop.
service.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package service | |
//NewService create new service | |
func NewService() *Service { | |
return &Service{} | |
} | |
//FindAll | |
func (s *Service) FindAll() ([]*entity.Privilege, error) { | |
acl := s.getDefaultPrivileges() | |
return acl, nil | |
} | |
//FindByRole | |
func (s *Service) FindyByRole(r *entity.Role) ([]*entity.Privilege, error) { | |
var ret []*entity.Privilege | |
acl := s.getDefaultPrivileges() | |
for _, p := range acl { | |
if p.Role.Slug == r.Slug { | |
ret = append(ret, p) | |
} | |
pChildren := walkPrivilegeChildren(p) | |
for _, pC := range pChildren { | |
if pC.Role.Slug == r.Slug { | |
ret = append(ret, pC) | |
} | |
} | |
} | |
return ret, nil | |
} | |
func walkPrivilegeChildren(priv *entity.Privilege) []*entity.Privilege { | |
var p []*entity.Privilege | |
for _, c := range priv.Children { | |
p = append(p, walkPrivilegeChildren(c)...) | |
} | |
return p | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment