forked from mikespook/gorbac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
permission_layer.go
45 lines (40 loc) · 915 Bytes
/
permission_layer.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
36
37
38
39
40
41
42
43
44
45
package gorbac
import (
"strings"
)
// NewLayerPermission returns an instance of layered permission with `id`
func NewLayerPermission(id, sep string) LayerPermission {
return LayerPermission{id, sep}
}
// LayerPermission uses string as a layered ID.
// Each layer splits by "/".
type LayerPermission struct {
SID string `json:"id"`
Sep string `json:"sep"`
}
// ID returns id
func (p LayerPermission) ID() string {
return p.SID
}
// Match another permission
func (p LayerPermission) Match(parent Permission[string]) bool {
if p.SID == parent.ID() {
return true
}
q, ok := parent.(LayerPermission)
if !ok {
return false
}
players := strings.Split(p.SID, p.Sep)
qlayers := strings.Split(q.SID, q.Sep)
// layer counts of q should be less than that of p
if len(players) > len(qlayers) {
return false
}
for k, pv := range players {
if pv != qlayers[k] {
return false
}
}
return true
}