From 1afe04e13f149a6bb0f99e9da948979e50ec8285 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 7 Jul 2018 23:07:00 +0200 Subject: [PATCH] add scope management --- cmd/aicra/main.go | 2 +- config/method.go | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/cmd/aicra/main.go b/cmd/aicra/main.go index cf70552..6855455 100644 --- a/cmd/aicra/main.go +++ b/cmd/aicra/main.go @@ -158,7 +158,7 @@ func main() { /* (3) Compile middlewares */ if compileMiddlewares { clifmt.Title("compile middlewares") - err = buildControllers(mPath, filepath.Join(projectPath, ".build/middleware")) + err = buildTypes(mPath, filepath.Join(projectPath, ".build/middleware")) if err != nil { fmt.Printf("%s compilation error: %s\n", clifmt.Warn(), err) } diff --git a/config/method.go b/config/method.go index ba7ce0a..03fb08e 100644 --- a/config/method.go +++ b/config/method.go @@ -1,7 +1,6 @@ package config import ( - "fmt" "git.xdrm.io/go/aicra/middleware" ) @@ -12,6 +11,36 @@ import ( // > level 1 is OR // > level 2 is AND func (m *Method) CheckScope(scope middleware.Scope) bool { - fmt.Printf("Scope: %v\n", m.Permission) + + for _, OR := range m.Permission { + + granted := true + + for _, AND := range OR { + + if !isPermInScope(AND, scope) { + granted = false + break + } + + } + + // if one is valid -> grant + if granted { + return true + } + + } + + return false +} + +// Returns whether @perm is present in @scope +func isPermInScope(perm string, scope []string) bool { + for _, s := range scope { + if perm == s { + return true + } + } return false }