/
roles.go
219 lines (193 loc) · 5.5 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package controllers
import (
"time"
"github.com/go-playground/validator/v10"
"github.com/kataras/iris/v12"
"github.com/snowlyg/IrisAdminApi/backend/libs"
"github.com/snowlyg/IrisAdminApi/backend/models"
"github.com/snowlyg/IrisAdminApi/backend/transformer"
"github.com/snowlyg/IrisAdminApi/backend/validates"
gf "github.com/snowlyg/gotransformer"
)
/**
* @api {get} /admin/roles/:id 根据id获取角色信息
* @apiName 根据id获取角色信息
* @apiGroup Roles
* @apiVersion 1.0.0
* @apiDescription 根据id获取角色信息
* @apiSampleRequest /admin/roles/:id
* @apiSuccess {String} msg 消息
* @apiSuccess {bool} state 状态
* @apiSuccess {String} data 返回数据
* @apiPermission
*/
func GetRole(ctx iris.Context) {
id, _ := ctx.Params().GetUint("id")
role := models.NewRole(id, "")
role.GetRoleById()
ctx.StatusCode(iris.StatusOK)
rr := roleTransform(role)
rr.Perms = permsTransform(role.RolePermisions())
_, _ = ctx.JSON(ApiResource(true, rr, "操作成功"))
}
/**
* @api {post} /admin/roles/ 新建角色
* @apiName 新建角色
* @apiGroup Roles
* @apiVersion 1.0.0
* @apiDescription 新建角色
* @apiSampleRequest /admin/roles/
* @apiParam {string} name 角色名
* @apiParam {string} display_name
* @apiParam {string} description
* @apiParam {string} level
* @apiSuccess {String} msg 消息
* @apiSuccess {bool} state 状态
* @apiSuccess {String} data 返回数据
* @apiPermission null
*/
func CreateRole(ctx iris.Context) {
roleJson := new(validates.RoleRequest)
if err := ctx.ReadJSON(roleJson); err != nil {
ctx.StatusCode(iris.StatusOK)
_, _ = ctx.JSON(ApiResource(false, nil, err.Error()))
return
}
err := validates.Validate.Struct(*roleJson)
if err != nil {
errs := err.(validator.ValidationErrors)
for _, e := range errs.Translate(validates.ValidateTrans) {
if len(e) > 0 {
ctx.StatusCode(iris.StatusOK)
_, _ = ctx.JSON(ApiResource(false, nil, e))
return
}
}
}
role := models.NewRoleByStruct(roleJson)
role.CreateRole(roleJson.PermissionsIds)
ctx.StatusCode(iris.StatusOK)
if role.ID == 0 {
_, _ = ctx.JSON(ApiResource(false, role, "操作失败"))
return
} else {
_, _ = ctx.JSON(ApiResource(true, nil, "操作成功"))
return
}
}
/**
* @api {post} /admin/roles/:id/update 更新角色
* @apiName 更新角色
* @apiGroup Roles
* @apiVersion 1.0.0
* @apiDescription 更新角色
* @apiSampleRequest /admin/roles/:id/update
* @apiParam {string} name 角色名
* @apiParam {string} display_name
* @apiParam {string} description
* @apiParam {string} level
* @apiSuccess {String} msg 消息
* @apiSuccess {bool} state 状态
* @apiSuccess {String} data 返回数据
* @apiPermission null
*/
func UpdateRole(ctx iris.Context) {
roleForm := new(validates.RoleRequest)
if err := ctx.ReadJSON(roleForm); err != nil {
ctx.StatusCode(iris.StatusOK)
_, _ = ctx.JSON(ApiResource(false, nil, err.Error()))
return
}
err := validates.Validate.Struct(*roleForm)
if err != nil {
errs := err.(validator.ValidationErrors)
for _, e := range errs.Translate(validates.ValidateTrans) {
if len(e) > 0 {
ctx.StatusCode(iris.StatusOK)
_, _ = ctx.JSON(ApiResource(false, nil, e))
return
}
}
}
id, _ := ctx.Params().GetUint("id")
role := models.NewRole(id, "")
role.GetRoleById()
if role.Name == "admin" {
ctx.StatusCode(iris.StatusOK)
_, _ = ctx.JSON(ApiResource(false, nil, "不能编辑管理员角色"))
return
}
roleJson := new(validates.RoleRequest)
roleJson.Name = roleForm.Name
roleJson.Description = roleForm.Description
roleJson.DisplayName = roleForm.DisplayName
role.UpdateRole(roleJson, roleForm.PermissionsIds)
ctx.StatusCode(iris.StatusOK)
if role.ID == 0 {
_, _ = ctx.JSON(ApiResource(false, role, "操作失败"))
return
} else {
_, _ = ctx.JSON(ApiResource(true, nil, "操作成功"))
return
}
}
/**
* @api {delete} /admin/roles/:id/delete 删除角色
* @apiName 删除角色
* @apiGroup Roles
* @apiVersion 1.0.0
* @apiDescription 删除角色
* @apiSampleRequest /admin/roles/:id/delete
* @apiSuccess {String} msg 消息
* @apiSuccess {bool} state 状态
* @apiSuccess {String} data 返回数据
* @apiPermission null
*/
func DeleteRole(ctx iris.Context) {
id, _ := ctx.Params().GetUint("id")
role := models.NewRole(id, "")
role.GetRoleById()
if role.Name == "admin" {
ctx.StatusCode(iris.StatusOK)
_, _ = ctx.JSON(ApiResource(true, nil, "不能删除管理员角色"))
return
}
role.DeleteRoleById()
ctx.StatusCode(iris.StatusOK)
_, _ = ctx.JSON(ApiResource(true, nil, "删除成功"))
}
/**
* @api {get} /roles 获取所有的角色
* @apiName 获取所有的角色
* @apiGroup Roles
* @apiVersion 1.0.0
* @apiDescription 获取所有的角色
* @apiSampleRequest /roles
* @apiSuccess {String} msg 消息
* @apiSuccess {bool} state 状态
* @apiSuccess {String} data 返回数据
* @apiPermission null
*/
func GetAllRoles(ctx iris.Context) {
offset := libs.ParseInt(ctx.FormValue("offset"), 1)
limit := libs.ParseInt(ctx.FormValue("limit"), 20)
name := ctx.FormValue("name")
orderBy := ctx.FormValue("orderBy")
roles := models.GetAllRoles(name, orderBy, offset, limit)
ctx.StatusCode(iris.StatusOK)
_, _ = ctx.JSON(ApiResource(true, rolesTransform(roles), "操作成功"))
}
func rolesTransform(roles []*models.Role) []*transformer.Role {
var rs []*transformer.Role
for _, role := range roles {
r := roleTransform(role)
rs = append(rs, r)
}
return rs
}
func roleTransform(role *models.Role) *transformer.Role {
r := &transformer.Role{}
g := gf.NewTransform(r, role, time.RFC3339)
_ = g.Transformer()
return r
}