niki/service/admin/authorization/service.go

38 lines
831 B
Go
Raw Permalink Normal View History

package adminauthorizationservice
import (
"git.gocasts.ir/ebhomengo/niki/entity"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
type Repository interface {
GetAdminPermissions(adminID uint, role entity.AdminRole) ([]entity.AdminPermission, error)
}
type Service struct {
repo Repository
}
func New(repo Repository) Service {
return Service{repo: repo}
}
func (s Service) CheckAccess(adminID uint, role entity.AdminRole, permissions ...entity.AdminPermission) (bool, error) {
const op = "adminauthorizationservice.CheckAccess"
AdminPermissions, err := s.repo.GetAdminPermissions(adminID, role)
if err != nil {
return false, richerror.New(op).WithErr(err)
}
for _, p := range permissions {
for _, ap := range AdminPermissions {
if p == ap {
return true, nil
}
}
}
return false, nil
}