This commit is contained in:
Vyacheslav1557 2024-10-13 19:03:43 +05:00
parent be25404852
commit 6e66be32a1
2 changed files with 1 additions and 40 deletions

View file

@ -13,7 +13,7 @@ type PermissionService struct {
func NewPermissionService() *PermissionService { func NewPermissionService() *PermissionService {
query, err := rego.New( query, err := rego.New(
rego.Query("allow = data.problem.rbac.allow"), rego.Query("allow = data.problem.rbac.allow"),
rego.Load([]string{"./opa/all.rego"}, nil), rego.Load([]string{"./opa/problem.rego"}, nil),
).PrepareForEval(context.TODO()) ).PrepareForEval(context.TODO())
if err != nil { if err != nil {

View file

@ -1,39 +0,0 @@
package usecase
import (
"context"
"git.sch9.ru/new_gate/models"
"github.com/open-policy-agent/opa/rego"
)
type PermissionService struct {
query *rego.PreparedEvalQuery
}
func NewPermissionService() *PermissionService {
query, err := rego.New(
rego.Query("allow = data.problem.rbac.allow"),
rego.Load([]string{"./opa/all.rego"}, nil),
).PrepareForEval(context.TODO())
if err != nil {
panic(err)
}
return &PermissionService{
query: &query,
}
}
func (s *PermissionService) Allowed(ctx context.Context, user *models.User, action string) bool {
input := map[string]interface{}{
"user": user,
"action": action,
}
result, err := s.query.Eval(ctx, rego.EvalInput(input))
if err != nil {
panic(err)
}
return result[0].Bindings["allow"].(bool)
}