refactor:
This commit is contained in:
parent
81e75e5a9c
commit
d62ae666d5
57 changed files with 656 additions and 310 deletions
1
internal/tester/delivery.go
Normal file
1
internal/tester/delivery.go
Normal file
|
@ -0,0 +1 @@
|
|||
package tester
|
1
internal/tester/delivery/grpc/handlers.go
Normal file
1
internal/tester/delivery/grpc/handlers.go
Normal file
|
@ -0,0 +1 @@
|
|||
package grpc
|
1
internal/tester/pg_repository.go
Normal file
1
internal/tester/pg_repository.go
Normal file
|
@ -0,0 +1 @@
|
|||
package tester
|
1
internal/tester/repository/pg_repository.go
Normal file
1
internal/tester/repository/pg_repository.go
Normal file
|
@ -0,0 +1 @@
|
|||
package repository
|
1
internal/tester/usecase.go
Normal file
1
internal/tester/usecase.go
Normal file
|
@ -0,0 +1 @@
|
|||
package tester
|
44
internal/tester/usecase/all.rego
Normal file
44
internal/tester/usecase/all.rego
Normal file
|
@ -0,0 +1,44 @@
|
|||
package problem.rbac
|
||||
|
||||
import rego.v1
|
||||
|
||||
spectator := 0
|
||||
participant := 1
|
||||
moderator := 2
|
||||
admin := 3
|
||||
|
||||
permissions := {
|
||||
"read": is_spectator,
|
||||
"participate": is_participant,
|
||||
"update": is_moderator,
|
||||
"create": is_moderator,
|
||||
"delete": is_moderator,
|
||||
}
|
||||
|
||||
default allow := false
|
||||
|
||||
allow if is_admin
|
||||
|
||||
allow if {
|
||||
permissions[input.action]
|
||||
}
|
||||
|
||||
default is_admin := false
|
||||
is_admin if {
|
||||
input.user.role == admin
|
||||
}
|
||||
|
||||
default is_moderator := false
|
||||
is_moderator if {
|
||||
input.user.role >= moderator
|
||||
}
|
||||
|
||||
default is_participant := false
|
||||
is_participant if {
|
||||
input.user.role >= participant
|
||||
}
|
||||
|
||||
default is_spectator := true
|
||||
is_spectator if {
|
||||
input.user.role >= spectator
|
||||
}
|
39
internal/tester/usecase/permission.go
Normal file
39
internal/tester/usecase/permission.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package services
|
||||
|
||||
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)
|
||||
}
|
1
internal/tester/usecase/usecase.go
Normal file
1
internal/tester/usecase/usecase.go
Normal file
|
@ -0,0 +1 @@
|
|||
package usecase
|
Loading…
Add table
Add a link
Reference in a new issue