-
Notifications
You must be signed in to change notification settings - Fork 1
/
role_enums.go
69 lines (58 loc) · 1.83 KB
/
role_enums.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
package internal
import (
"github.com/lvyahui8/goenum"
)
// Role 参考 https://docs.gitlab.com/ee/user/permissions.html
type Role struct {
goenum.Enum
perms []Permission
}
// UnmarshalJSON 枚举类需要自行实现json.Unmarshaler接口
func (r *Role) UnmarshalJSON(data []byte) (err error) {
role, err := goenum.Unmarshal[Role](data)
if err == nil {
*r = role
}
return
}
func (r *Role) HasPerm(p Permission) bool {
for _, perm := range r.perms {
if p.Equals(perm) {
return true
}
}
return false
}
type Module struct {
goenum.Enum
perms []Permission
basePath string
}
func (m Module) GetPerms() []Permission {
return m.perms
}
func (m Module) BasePath() string {
return m.basePath
}
type Permission struct {
goenum.Enum
}
// 定义权限
var (
AddLabels = goenum.NewEnum[Permission]("AddLabels")
AddTopic = goenum.NewEnum[Permission]("AddTopic")
ViewMergeRequest = goenum.NewEnum[Permission]("ViewMergeRequest")
ApproveMergeRequest = goenum.NewEnum[Permission]("ApproveMergeRequest")
DeleteMergeRequest = goenum.NewEnum[Permission]("DeleteMergeRequest")
)
// 定义模块
var (
Issues = goenum.NewEnum[Module]("Issues", Module{perms: []Permission{AddLabels, AddTopic}, basePath: "/issues/"})
MergeRequests = goenum.NewEnum[Module]("MergeRequests", Module{perms: []Permission{ViewMergeRequest, ApproveMergeRequest, DeleteMergeRequest}, basePath: "/merge/"})
)
// 定义角色
var (
Reporter = goenum.NewEnum[Role]("Reporter", Role{perms: []Permission{ViewMergeRequest}})
Developer = goenum.NewEnum[Role]("Developer", Role{perms: []Permission{AddLabels, AddTopic, ViewMergeRequest}})
Owner = goenum.NewEnum[Role]("Owner", Role{perms: []Permission{AddLabels, AddTopic, ViewMergeRequest, ApproveMergeRequest, DeleteMergeRequest}}) // 可以考虑给Owner单独定义一个All的权限
)