-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth_grant_policy.go
35 lines (31 loc) · 1.06 KB
/
auth_grant_policy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package gcloudcx
import (
"fmt"
"strings"
)
type AuthorizationGrantPolicy struct {
EntityName string `json:"entityName"`
Domain string `json:"domain"`
Condition string `json:"condition"`
Actions []string `json:"actions"`
}
// CheckScope checks if the grant allows or denies the given scope
func (policy AuthorizationGrantPolicy) CheckScope(scope AuthorizationScope) bool {
if policy.Domain == "*" || (policy.Domain == scope.Domain && (policy.EntityName == "*" || policy.EntityName == scope.Entity)) {
for _, action := range policy.Actions {
if action == "*" || action == scope.Action {
return true
}
}
}
return false
}
// String returns a string representation of the AuthorizationDivision
//
// implements fmt.Stringer
func (policy AuthorizationGrantPolicy) String() string {
if len(policy.Condition) == 0 {
return fmt.Sprintf("%s:%s:[%s]", policy.Domain, policy.EntityName, strings.Join(policy.Actions, ","))
}
return fmt.Sprintf("%s:%s:[%s]{? %s}", policy.Domain, policy.EntityName, strings.Join(policy.Actions, ","), policy.Condition)
}