-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity.go
More file actions
141 lines (123 loc) · 3.65 KB
/
Copy pathentity.go
File metadata and controls
141 lines (123 loc) · 3.65 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package permission
import "github.com/gouef/utils"
// Entity represents a user, group, role (or what you want) with specific permissions.
type Entity struct {
ID string
Parents []*Entity
Children []*Entity
Permission map[Permission]map[*Resource]bool
}
// NewEntity creates a new entity with default permission sets.
//
// Example:
//
// user := permission.NewEntity("user1")
// fmt.Println(user.ID) // Output: user1
func NewEntity(id string) *Entity {
perms := make(map[Permission]map[*Resource]bool)
perms[All] = make(map[*Resource]bool)
perms[Create] = make(map[*Resource]bool)
perms[Read] = make(map[*Resource]bool)
perms[Update] = make(map[*Resource]bool)
perms[Delete] = make(map[*Resource]bool)
return &Entity{
ID: id,
Parents: make([]*Entity, 0),
Children: make([]*Entity, 0),
Permission: perms,
}
}
// CreateChild creates a child entity and assigns it as a descendant.
//
// Example:
//
// parent := permission.NewEntity("admin")
// child := parent.CreateChild("user")
// fmt.Println(child.ID) // Output: user
func (e *Entity) CreateChild(id string) *Entity {
child := NewEntity(id)
e.AddChildren(child)
return child
}
// AddChildren associates child entities with the current entity.
//
// Example:
//
// admin := permission.NewEntity("admin")
// user := permission.NewEntity("user")
// admin.AddChildren(user)
func (e *Entity) AddChildren(children ...*Entity) {
e.Children = append(e.Children, children...)
for _, child := range children {
if !child.parentExists(e) {
child.AddParents(e)
}
}
}
// AddParents associates parent entities with the current entity.
//
// Example:
//
// admin := permission.NewEntity("admin")
// user := permission.NewEntity("user")
// user.AddParents(admin)
func (e *Entity) AddParents(parents ...*Entity) {
e.Parents = append(e.Parents, parents...)
for _, parent := range parents {
if !parent.childExists(e) {
parent.AddChildren(e)
}
}
}
// Allow grants specified permissions for a resource to the entity.
//
// Example:
//
// user := permission.NewEntity("user")
// res := permission.NewResource("file")
// user.Allow(res, permission.Read, permission.Write)
func (e *Entity) Allow(resource *Resource, permissions ...Permission) {
for _, permission := range permissions {
e.AddPerm(permission, resource, true)
}
}
// Deny revokes specified permissions for a resource from the entity.
//
// Example:
//
// user := permission.NewEntity("user")
// res := permission.NewResource("file")
// user.Deny(res, permission.Read, permission.Write)
func (e *Entity) Deny(resource *Resource, permissions ...Permission) {
for _, permission := range permissions {
e.AddPerm(permission, resource, false)
}
}
// AddPerm sets or removes a specific permission for a resource.
func (e *Entity) AddPerm(permission Permission, resource *Resource, enabled bool) {
if _, ok := e.Permission[permission]; !ok {
e.Permission[permission] = make(map[*Resource]bool)
}
e.Permission[permission][resource] = enabled
}
func (e *Entity) AddPermAll(resource *Resource, enabled bool) {
e.AddPerm(All, resource, enabled)
}
func (e *Entity) AddPermCreate(resource *Resource, enabled bool) {
e.AddPerm(Create, resource, enabled)
}
func (e *Entity) AddPermRead(resource *Resource, enabled bool) {
e.AddPerm(Read, resource, enabled)
}
func (e *Entity) AddPermUpdate(resource *Resource, enabled bool) {
e.AddPerm(Update, resource, enabled)
}
func (e *Entity) AddPermDelete(resource *Resource, enabled bool) {
e.AddPerm(Delete, resource, enabled)
}
func (e *Entity) parentExists(parent *Entity) bool {
return utils.InArray(parent, e.Parents)
}
func (e *Entity) childExists(child *Entity) bool {
return utils.InArray(child, e.Children)
}