-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpermission.go
137 lines (113 loc) · 2.48 KB
/
permission.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
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
// Permission ...
type Permission struct {
Unit string `json:"category" binding:"required"`
Name string `json:"cn" binding:"required"`
Description string
MType []string
UType []string
}
func (p *Permission) rbacTypes() []string {
return append(p.MType, p.UType...)
}
func fetchPermissionByRoleUPID(c *gin.Context) {
fetchPermissionByRole(true, c)
}
func fetchPermissionByRolePPID(c *gin.Context) {
fetchPermissionByRole(false, c)
}
func fetchPermissionByRole(isUnit bool, c *gin.Context) {
rid := c.Query(`role_id`) // 有可能是查找某个角色包含的所有类型
r, e := org.RoleByID(rid)
if e != nil {
sendError(c, e)
return
}
ids := r[`ppid`].([]string)
if isUnit {
ids = r[`upid`].([]string)
}
sr, e := org.PermissionByIDs(ids)
if e != nil {
sendError(c, e)
return
}
sendResult(c, sr)
}
func fetchPermissions(c *gin.Context) {
isUnit := c.Query(`isUnit`) == `true`
_, pageSize, cookie := findPageControl(c)
r, e := org.Permissions(isUnit, pageSize, cookie)
if e != nil {
c.JSON(http.StatusInternalServerError, map[string]string{
`err`: e.Error(),
})
}
sendResult(c, r)
}
func createPermission(c *gin.Context) {
var p Permission
err := c.BindJSON(&p) // 会发送错误信息
if err != nil {
return
}
id, err := org.AddPermission(p.Name, p.Description, p.rbacTypes(), p.Unit == department)
if err != nil {
sendError(c, err)
} else {
c.JSON(http.StatusOK, map[string]string{
`id`: id,
})
}
}
func permissionByID(c *gin.Context) {
p, e := org.PermissionByID(c.Param(`id`))
if e != nil {
sendError(c, e)
return
}
isUnit := p[`isUnit`].(bool)
if isUnit {
p[`category`] = `部门权限`
p[`uType`] = p[`rbacType`]
} else {
p[`category`] = `员工权限`
p[`mType`] = p[`rbacType`]
}
delete(p, `dn`)
delete(p, `rbacType`)
c.JSON(http.StatusOK, p)
}
func editPermission(c *gin.Context) {
id := c.Param(`id`)
var p Permission
err := c.BindJSON(&p) // 会发送错误信息
if err != nil {
return
}
err = org.ModifyPermission(id, p.Name, p.Description, p.rbacTypes())
if err != nil {
sendError(c, err)
} else {
c.JSON(http.StatusOK, map[string]string{
`id`: id,
})
}
}
// 删除逻辑
// 保证没人引用这个Permission
func delPermission(c *gin.Context) {
id := c.Param(`id`)
err := org.DelPermission(id)
if err != nil {
sendError(c, err)
return
}
c.JSON(http.StatusOK, map[string]string{
`id`: id,
})
}