59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package pnyx
|
|
|
|
type PermissionLevel int
|
|
const (
|
|
PERMISSION_FALLTHROUGH PermissionLevel = iota
|
|
PERMISSION_ALLOW
|
|
PERMISSION_DENY
|
|
PERMISSION_ALLOW_ALL
|
|
PERMISSION_DENY_ALL
|
|
)
|
|
|
|
type Role string
|
|
type Action string
|
|
|
|
type Permissions map[Action]Permission
|
|
type Permission struct {
|
|
Level PermissionLevel
|
|
Next Permissions
|
|
}
|
|
|
|
func(permissions Permissions) Allowed(action []Action, def bool) bool {
|
|
if len(action) == 0 {
|
|
return def
|
|
} else if permissions == nil {
|
|
return def
|
|
}
|
|
|
|
perm, exists := permissions[action[0]]
|
|
if exists == false {
|
|
return def
|
|
} else if len(action) == 1 {
|
|
return perm.Level == PERMISSION_ALLOW || perm.Level == PERMISSION_ALLOW_ALL
|
|
} else {
|
|
switch perm.Level {
|
|
case PERMISSION_ALLOW_ALL:
|
|
return perm.Next.Allowed(action[1:], true)
|
|
case PERMISSION_DENY_ALL:
|
|
return perm.Next.Allowed(action[1:], false)
|
|
default:
|
|
return perm.Next.Allowed(action[1:], def)
|
|
}
|
|
}
|
|
}
|
|
|
|
func(permissions Permissions) Copy() Permissions {
|
|
if permissions == nil {
|
|
return nil
|
|
}
|
|
|
|
new_permissions := Permissions{}
|
|
for action, permission := range(permissions) {
|
|
new_permissions[action] = Permission{
|
|
Level: permission.Level,
|
|
Next: permission.Next.Copy(),
|
|
}
|
|
}
|
|
return new_permissions
|
|
}
|