-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathroles.go
56 lines (49 loc) · 1.38 KB
/
roles.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
package main
import (
"fmt"
"github.com/zpatrick/rbac"
)
// NewAdminRole returns a role with admin-level permissions
func NewAdminRole() rbac.Role {
return rbac.Role{
RoleID: "Admin",
Permissions: []rbac.Permission{
rbac.NewGlobPermission("*", "*"),
},
}
}
// NewGuestRole returns a role with guest-level permissions
func NewGuestRole() rbac.Role {
return rbac.Role{
RoleID: "Guest",
Permissions: []rbac.Permission{
rbac.NewGlobPermission("ReadArticle", "*"),
rbac.NewGlobPermission("RateArticle", "*"),
},
}
}
// NewMemberRole returns a role with member-level permissions
func NewMemberRole(userID string) rbac.Role {
return rbac.Role{
RoleID: fmt.Sprintf("Member(%s)", userID),
Permissions: []rbac.Permission{
rbac.NewGlobPermission("CreateArticle", "*"),
rbac.NewGlobPermission("ReadArticle", "*"),
rbac.NewGlobPermission("RateArticle", "*"),
rbac.NewPermission(rbac.GlobMatch("EditArticle"), ifArticleAuthor(userID)),
rbac.NewPermission(rbac.GlobMatch("DeleteArticle"), ifArticleAuthor(userID)),
},
}
}
// ifArticleAuthor returns a matcher that will only return true if
// the article's author matches userID.
func ifArticleAuthor(userID string) rbac.Matcher {
return func(target string) (bool, error) {
for _, article := range Articles() {
if article.ArticleID == target {
return article.AuthorID == userID, nil
}
}
return false, nil
}
}