-
Notifications
You must be signed in to change notification settings - Fork 180
/
roleControllers.go
289 lines (261 loc) · 6.79 KB
/
roleControllers.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package controllers
import (
"encoding/json"
"github.com/astaxie/beego/context"
"github.com/hzwy23/hauth/core/groupcache"
"github.com/hzwy23/hauth/core/hrpc"
"github.com/hzwy23/hauth/core/models"
"github.com/hzwy23/hauth/utils"
"github.com/hzwy23/hauth/utils/hret"
"github.com/hzwy23/hauth/utils/i18n"
"github.com/hzwy23/hauth/utils/jwt"
"github.com/hzwy23/hauth/utils/logs"
"github.com/hzwy23/hauth/utils/validator"
)
type roleController struct {
models models.RoleModel
}
var RoleCtl = &roleController{
models.RoleModel{},
}
// swagger:operation GET /v1/auth/role/page StaticFiles roleController
//
// 角色管理页面
//
// 如果用户被授权访问角色管理页面,则系统返回角色管理页面内容,否则返回404错误
//
// ---
// produces:
// - application/json
// - application/xml
// - text/xml
// - text/html
// parameters:
// - name: domain_id
// in: query
// description: domain code number
// required: true
// type: string
// format:
// responses:
// '200':
// description: success
func (roleController) Page(ctx *context.Context) {
ctx.Request.ParseForm()
if !hrpc.BasicAuth(ctx.Request) {
hret.Error(ctx.ResponseWriter, 403, i18n.NoAuth(ctx.Request))
return
}
rst, err := groupcache.GetStaticFile("AsofdateRolePage")
if err != nil {
hret.Error(ctx.ResponseWriter, 404, i18n.PageNotFound(ctx.Request))
return
}
ctx.ResponseWriter.Write(rst)
}
// swagger:operation GET /v1/auth/role/get roleController roleController
//
// 查询角色信息
//
// 查询指定域中的角色信息
//
// ---
// produces:
// - application/json
// - application/xml
// - text/xml
// - text/html
// parameters:
// - name: domain_id
// in: query
// description: domain code number
// required: true
// type: string
// format:
// responses:
// '200':
// description: success
func (this roleController) Get(ctx *context.Context) {
ctx.Request.ParseForm()
if !hrpc.BasicAuth(ctx.Request) {
hret.Error(ctx.ResponseWriter, 403, i18n.NoAuth(ctx.Request))
return
}
domain_id := ctx.Request.FormValue("domain_id")
if validator.IsEmpty(domain_id) {
cookie, _ := ctx.Request.Cookie("Authorization")
jclaim, err := jwt.ParseJwt(cookie.Value)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 403, i18n.Disconnect(ctx.Request))
return
}
domain_id = jclaim.DomainId
}
if !hrpc.DomainAuth(ctx.Request, domain_id, "r") {
hret.Error(ctx.ResponseWriter, 403, i18n.Get(ctx.Request, "as_of_date_domain_permission_denied"))
return
}
rst, err := this.models.Get(domain_id)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, "error_role_query"), err)
return
}
hret.Json(ctx.ResponseWriter, rst)
}
// swagger:operation POST /v1/auth/role/post roleController roleController
//
// 新增角色信息
//
// 在某个指定的域中,新增角色信息
//
// ---
// produces:
// - application/json
// - application/xml
// - text/xml
// - text/html
// parameters:
// - name: domain_id
// in: query
// description: domain code number
// required: true
// type: string
// format:
// responses:
// '200':
// description: success
func (this roleController) Post(ctx *context.Context) {
ctx.Request.ParseForm()
if !hrpc.BasicAuth(ctx.Request) {
hret.Error(ctx.ResponseWriter, 403, i18n.NoAuth(ctx.Request))
return
}
form := ctx.Request.Form
domainid := form.Get("domain_id")
if !hrpc.DomainAuth(ctx.Request, domainid, "w") {
logs.Error("没有权限在这个域中新增角色信息")
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, "as_of_date_domain_permission_denied"))
return
}
cok, _ := ctx.Request.Cookie("Authorization")
jclaim, err := jwt.ParseJwt(cok.Value)
if err != nil {
hret.Error(ctx.ResponseWriter, 403, i18n.Disconnect(ctx.Request))
return
}
msg, err := this.models.Post(form, jclaim.UserId)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, msg), err)
return
}
hret.Success(ctx.ResponseWriter, i18n.Success(ctx.Request))
}
// swagger:operation POST /v1/auth/role/delete roleController roleController
//
// 删除角色信息
//
// 删除某个指定域中的角色信息
//
// ---
// produces:
// - application/json
// - application/xml
// - text/xml
// - text/html
// parameters:
// - name: domain_id
// in: query
// description: domain code number
// required: true
// type: string
// format:
// responses:
// '200':
// description: success
func (this roleController) Delete(ctx *context.Context) {
ctx.Request.ParseForm()
if !hrpc.BasicAuth(ctx.Request) {
hret.Error(ctx.ResponseWriter, 403, i18n.NoAuth(ctx.Request))
return
}
var allrole []models.RoleInfo
err := json.Unmarshal([]byte(ctx.Request.FormValue("JSON")), &allrole)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, "error_role_json_failed"), err)
return
}
for _, val := range allrole {
if !hrpc.DomainAuth(ctx.Request, val.Domain_id, "w") {
hret.Error(ctx.ResponseWriter, 403, i18n.WriteDomain(ctx.Request, val.Domain_id))
return
}
}
msg, err := this.models.Delete(allrole)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 418, i18n.Get(ctx.Request, msg))
return
}
hret.Success(ctx.ResponseWriter, i18n.Success(ctx.Request))
}
// swagger:operation PUT /v1/auth/role/put roleController roleController
//
// 更新角色信息
//
// 更新某个域中的角色信息,角色编码不能更新
//
// ---
// produces:
// - application/json
// - application/xml
// - text/xml
// - text/html
// parameters:
// - name: domain_id
// in: query
// description: domain code number
// required: true
// type: string
// format:
// responses:
// '200':
// description: success
func (this roleController) Update(ctx *context.Context) {
ctx.Request.ParseForm()
if !hrpc.BasicAuth(ctx.Request) {
hret.Error(ctx.ResponseWriter, 403, i18n.NoAuth(ctx.Request))
return
}
form := ctx.Request.Form
Role_id := form.Get("Role_id")
did, err := utils.SplitDomain(Role_id)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 423, i18n.NoSeparator(ctx.Request, Role_id))
}
if !hrpc.DomainAuth(ctx.Request, did, "w") {
hret.Error(ctx.ResponseWriter, 403, i18n.Get(ctx.Request, "as_of_date_domain_permission_denied_modify"))
return
}
cookie, _ := ctx.Request.Cookie("Authorization")
jclaim, err := jwt.ParseJwt(cookie.Value)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 403, i18n.Disconnect(ctx.Request))
return
}
msg, err := this.models.Update(form, jclaim.UserId)
if err != nil {
logs.Error(err.Error())
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, msg), err)
return
}
hret.Success(ctx.ResponseWriter, i18n.Success(ctx.Request))
}
func init() {
groupcache.RegisterStaticFile("AsofdateRolePage", "./views/hauth/role_info_page.tpl")
}