From 6e66be32a152355b4654e8a60f331d75b1215751 Mon Sep 17 00:00:00 2001 From: Vyacheslav1557 Date: Sun, 13 Oct 2024 19:03:43 +0500 Subject: [PATCH] feat: --- .../usecase/policy_agent.go} | 2 +- internal/problems/usecase/permission.go | 39 ------------------- 2 files changed, 1 insertion(+), 40 deletions(-) rename internal/{languages/usecase/permission.go => contests/usecase/policy_agent.go} (93%) delete mode 100644 internal/problems/usecase/permission.go diff --git a/internal/languages/usecase/permission.go b/internal/contests/usecase/policy_agent.go similarity index 93% rename from internal/languages/usecase/permission.go rename to internal/contests/usecase/policy_agent.go index ae3855f..e2c0f82 100644 --- a/internal/languages/usecase/permission.go +++ b/internal/contests/usecase/policy_agent.go @@ -13,7 +13,7 @@ type PermissionService struct { func NewPermissionService() *PermissionService { query, err := rego.New( rego.Query("allow = data.problem.rbac.allow"), - rego.Load([]string{"./opa/all.rego"}, nil), + rego.Load([]string{"./opa/problem.rego"}, nil), ).PrepareForEval(context.TODO()) if err != nil { diff --git a/internal/problems/usecase/permission.go b/internal/problems/usecase/permission.go deleted file mode 100644 index ae3855f..0000000 --- a/internal/problems/usecase/permission.go +++ /dev/null @@ -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) -}