2024-02-24 17:06:13 +00:00
|
|
|
package adminauthorizationservice
|
|
|
|
|
|
|
|
import (
|
2024-08-01 10:20:18 +00:00
|
|
|
"context"
|
|
|
|
|
2024-02-24 17:06:13 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
|
|
|
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Repository interface {
|
2024-08-01 10:20:18 +00:00
|
|
|
GetAdminPermissions(ctx context.Context, adminID uint, role entity.AdminRole) ([]entity.AdminPermission, error)
|
2024-02-24 17:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Service struct {
|
|
|
|
repo Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(repo Repository) Service {
|
|
|
|
return Service{repo: repo}
|
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (s Service) CheckAccess(ctx context.Context, adminID uint, role entity.AdminRole, permissions ...entity.AdminPermission) (bool, error) {
|
2024-02-24 17:06:13 +00:00
|
|
|
const op = "adminauthorizationservice.CheckAccess"
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
AdminPermissions, err := s.repo.GetAdminPermissions(ctx, adminID, role)
|
2024-02-24 17:06:13 +00:00
|
|
|
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
|
|
|
|
}
|