-
Notifications
You must be signed in to change notification settings - Fork 0
/
perms_test.go
191 lines (179 loc) · 4.65 KB
/
perms_test.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package perms
import (
"fmt"
"testing"
)
type User struct {
Name string
IsSuperuser bool
}
type Group struct {
Name string
Members []string
}
type Video struct {
Name string
Duration float64
Public bool
User string
Group string
}
type Playlist struct {
ID string
videos []Video
Public bool
User string
Group string
}
type Archive struct {
Name string
User string
Group string
}
const (
ALLOW = "allow"
DENY = "deny"
)
func TestAddRule(t *testing.T) {
rs := NewRuleSet(DENY)
rs.AddRule(&User{}, "view", &Playlist{},
func (subj interface{}, act interface{}, res interface{}) (matches bool, effect string, quick bool) {
user := subj.(*User)
// if !ok {
// return false, "", false
// }
action := act.(string)
playlist := res.(*Playlist)
// if !ok {
// return false, "", false
// }
fmt.Printf("[view] user:%v action:%v playlist:%v\n", user, action, playlist)
if user.IsSuperuser {
return true, ALLOW, true
}
if playlist.Public {
return true, ALLOW, false
}
if playlist.User == user.Name {
return true, ALLOW, false
}
return true, DENY, false
})
rs.AddRule(&User{}, "modify", &Playlist{},
func (subj interface{}, act interface{}, res interface{}) (matches bool, effect string, quick bool) {
user := subj.(*User)
action := act.(string)
playlist := res.(*Playlist)
fmt.Printf("[modify] user:%v action:%v playlist:%v\n", user, action, playlist)
if playlist.User == user.Name {
return true, ALLOW, false
}
return true, DENY, false
})
rs.AddRule(&User{}, "view", &Video{},
func (subj interface{}, act interface{}, res interface{}) (matches bool, effect string, quick bool) {
user := subj.(*User)
action := act.(string)
video := res.(*Video)
fmt.Printf("[view] user:%v action:%v video:%v\n", user, action, video)
if user.IsSuperuser {
return true, ALLOW, true
}
if video.Public {
return true, ALLOW, false
}
if video.User == user.Name {
return true, ALLOW, false
}
return true, DENY, false
})
rs.AddRule(&User{}, "modify", &Video{},
func (subj interface{}, act interface{}, res interface{}) (matches bool, effect string, quick bool) {
user := subj.(*User)
action := act.(string)
video := res.(*Video)
fmt.Printf("[%v] user:%v action:%v video:%v\n", action, user, action, video)
if user.IsSuperuser {
return true, ALLOW, true
}
if video.User == user.Name {
return true, ALLOW, false
}
return true, DENY, false
})
rs.AddRule(&Group{}, "modify", &Playlist{},
func (subj interface{}, act interface{}, res interface{}) (matches bool, effect string, quick bool) {
group := subj.(*Group)
action := act.(string)
playlist := res.(*Playlist)
fmt.Printf("[%v] group:%v action:%v playlist:%v\n", action, group, action, playlist)
if playlist.Group == group.Name {
return true, ALLOW, false
}
return true, DENY, false
})
rs.AddRule(&User{}, "view", nil,
func (subj interface{}, act interface{}, res interface{}) (matches bool, effect string, quick bool) {
user := subj.(*User)
action := act.(string)
fmt.Printf("[view] user:%v action:%v resource:%v\n", user, action, res)
if user.IsSuperuser {
return true, ALLOW, true
}
return true, DENY, false
})
john := &User{
Name: "john",
IsSuperuser: false,
}
jack := &User{
Name: "jack",
IsSuperuser: false,
}
anonymous := &User{
Name: "anonymous",
IsSuperuser: false,
}
overlord := &User{
Name: "overlord",
IsSuperuser: true,
}
john_playlist := &Playlist{
ID: "6563",
User: "john",
Group: "john",
}
john_public_playlist := &Playlist{
ID: "6274",
Public: true,
User: "john",
Group: "john",
}
jack_playlist := &Playlist{
ID: "9374",
User: "jack",
Group: "editors",
}
check := func(got string, want string) {
fmt.Printf("eff: %v\n", got)
if got != want {
t.Errorf("got %q want %q", got, want)
}
}
check(rs.Query(john, "modify", john_playlist), ALLOW)
check(rs.Query(john, "modify", jack_playlist), DENY)
check(rs.Query(jack, "modify", jack_playlist), ALLOW)
check(rs.Query(jack, "view", john_playlist), DENY)
check(rs.Query(jack, "view", john_public_playlist), ALLOW)
check(rs.Query(anonymous, "view", john_public_playlist), ALLOW)
check(rs.Query(overlord, "modify", jack_playlist), DENY)
check(rs.Query(&Group{
Name: "editors",
}, "modify", john_playlist), DENY)
check(rs.Query(&Group{
Name: "editors",
}, "modify", jack_playlist), ALLOW)
check(rs.Query(jack, "view", &Archive{Name: "test"}), DENY)
check(rs.Query(overlord, "modify", &Archive{Name: "test"}), DENY)
check(rs.Query(overlord, "view", &Archive{Name: "test"}), ALLOW)
}