Skip to content

Instantly share code, notes, and snippets.

@eminetto
Created September 28, 2022 11:55
Show Gist options
  • Save eminetto/c80cbb31ab8a42c4dd0b3511c54fe59c to your computer and use it in GitHub Desktop.
Save eminetto/c80cbb31ab8a42c4dd0b3511c54fe59c to your computer and use it in GitHub Desktop.
service.go
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