2018-10-01 12:15:00 +00:00
|
|
|
package api
|
2018-07-07 20:10:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"git.xdrm.io/go/aicra/middleware"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CheckScope returns whether a given scope matches the
|
|
|
|
// method configuration
|
|
|
|
//
|
|
|
|
// format is: [ [a,b], [c], [d,e] ]
|
|
|
|
// > level 1 is OR
|
|
|
|
// > level 2 is AND
|
|
|
|
func (m *Method) CheckScope(scope middleware.Scope) bool {
|
2018-07-07 21:07:00 +00:00
|
|
|
|
|
|
|
for _, OR := range m.Permission {
|
|
|
|
|
|
|
|
granted := true
|
|
|
|
|
|
|
|
for _, AND := range OR {
|
|
|
|
|
2018-10-07 09:14:04 +00:00
|
|
|
if !scopeHasPermission(AND, scope) {
|
2018-07-07 21:07:00 +00:00
|
|
|
granted = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// if one is valid -> grant
|
|
|
|
if granted {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-10-07 09:14:04 +00:00
|
|
|
// scopeHasPermission returns whether @perm is present in a given @scope
|
|
|
|
func scopeHasPermission(perm string, scope []string) bool {
|
2018-07-07 21:07:00 +00:00
|
|
|
for _, s := range scope {
|
|
|
|
if perm == s {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2018-07-07 20:10:56 +00:00
|
|
|
return false
|
|
|
|
}
|