-
Notifications
You must be signed in to change notification settings - Fork 180
/
userRolesController.go
277 lines (253 loc) · 6.96 KB
/
userRolesController.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
package controllers
import (
"encoding/json"
"github.com/hzwy23/hauth/core/groupcache"
"github.com/hzwy23/hauth/core/hrpc"
"github.com/hzwy23/hauth/core/models"
"github.com/astaxie/beego/context"
"github.com/hzwy23/hauth/utils/hret"
"github.com/hzwy23/hauth/utils/i18n"
"github.com/hzwy23/hauth/utils/jwt"
"github.com/hzwy23/hauth/utils/logs"
)
type userRolesController struct {
models *models.UserRolesModel
}
var UserRolesCtl = &userRolesController{
models: new(models.UserRolesModel),
}
// swagger:operation GET /v1/auth/batch/page StaticFiles domainShareControll
//
// If the request is successful,
// will return authorization page information to the client
//
// The system will check user permissions.
// So,you must first login system,and then you can send the request.
//
// If the user is authorized to visit, the return authorization information page
//
// ---
// produces:
// - application/json
// - application/xml
// - text/xml
// - text/html
// responses:
// '200':
// description: request success.
// '404':
// description: page not found.
func (this *userRolesController) Page(ctx *context.Context) {
if !hrpc.BasicAuth(ctx.Request) {
hret.Error(ctx.ResponseWriter, 403, i18n.NoAuth(ctx.Request))
return
}
// According to the key get the value from the groupCache system
rst, err := groupcache.GetStaticFile("AuthorityPage")
if err != nil {
hret.Error(ctx.ResponseWriter, 404, i18n.Get(ctx.Request, "as_of_date_page_not_exist"))
return
}
ctx.ResponseWriter.Write(rst)
}
// swagger:operation GET /v1/auth/user/roles/get userRolesController userRolesController
//
// 通过user_id用户账号,来查询这个用户拥有的角色信息
//
// 查询角色信息
//
// ---
// 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 userRolesController) GetRolesByUserId(ctx *context.Context) {
ctx.Request.ParseForm()
user_id := ctx.Request.FormValue("user_id")
rst, err := this.models.GetRolesByUser(user_id)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 419, i18n.Get(ctx.Request, "error_user_role_query"), err)
return
}
hret.Json(ctx.ResponseWriter, rst)
}
// swagger:operation GET /v1/auth/user/roles/other userRolesController userRolesController
//
// 通过user_id账号,查询这个用户能够访问,但是又没有获取到的角色信息
//
// 查询用户没有获取的角色
//
// ---
// 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: all domain information
func (this userRolesController) GetOtherRoles(ctx *context.Context) {
ctx.Request.ParseForm()
user_id := ctx.Request.FormValue("user_id")
if user_id == "" {
hret.Error(ctx.ResponseWriter, 419, i18n.Get(ctx.Request, "error_user_role_no_user"))
return
}
rst, err := this.models.GetOtherRoles(user_id)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 419, i18n.Get(ctx.Request, "error_user_role_un_auth"), err)
return
}
hret.Json(ctx.ResponseWriter, rst)
}
// swagger:operation POST /v1/auth/user/roles/auth userRolesController userRolesController
//
// 给指定的用户授予角色
//
// 给指定的用户授予角色
//
// ---
// 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 userRolesController) Auth(ctx *context.Context) {
ctx.Request.ParseForm()
if !hrpc.BasicAuth(ctx.Request) {
hret.Error(ctx.ResponseWriter, 403, i18n.NoAuth(ctx.Request))
return
}
var rst []models.UserRolesModel
err := json.Unmarshal([]byte(ctx.Request.FormValue("JSON")), &rst)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, "error_unmarsh_json"), err)
return
}
for _, val := range rst {
domain_id, err := hrpc.GetDomainId(val.User_id)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 403, i18n.Get(ctx.Request, "error_user_role_no_auth"))
return
}
if !hrpc.DomainAuth(ctx.Request, domain_id, "w") {
hret.Error(ctx.ResponseWriter, 403, i18n.WriteDomain(ctx.Request, domain_id))
return
}
}
cok, _ := ctx.Request.Cookie("Authorization")
jclaim, err := jwt.ParseJwt(cok.Value)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 403, i18n.Disconnect(ctx.Request))
return
}
msg, err := this.models.Auth(rst, jclaim.UserId)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 419, i18n.Get(ctx.Request, msg), err)
return
}
hret.Success(ctx.ResponseWriter, i18n.Success(ctx.Request))
}
// swagger:operation POST /v1/auth/user/roles/revoke userRolesController userRolesController
//
// Delete user has been granted the roles
//
// The system will check user permissions.
// So,you must first login system,and then you can send the request.
//
// If the user is authorized to visit, the system will delete the roles that client request specified.
//
// ---
// produces:
// - application/json
// - application/xml
// - text/xml
// - text/html
// parameters:
// - name: user_id
// in: query
// description: Removed the role of the user
// required: true
// type: string
// format:
// - name: role_id
// in: query
// description: The role of ready to delete
// required: true
// type: string
// format:
// responses:
// '200':
// description: success
func (this userRolesController) Revoke(ctx *context.Context) {
ctx.Request.ParseForm()
if !hrpc.BasicAuth(ctx.Request) {
hret.Error(ctx.ResponseWriter, 403, i18n.NoAuth(ctx.Request))
return
}
form := ctx.Request.FormValue("JSON")
var rst []models.UserRolesModel
err := json.Unmarshal([]byte(form), &rst)
if err != nil {
logs.Error("解析json格式数据失败,请联系管理员")
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, "error_unmarsh_json"))
return
}
for _, val := range rst {
domain_id, err := hrpc.GetDomainId(val.User_id)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 403, i18n.Get(ctx.Request, "error_user_role_no_auth"))
return
}
if !hrpc.DomainAuth(ctx.Request, domain_id, "w") {
hret.Error(ctx.ResponseWriter, 403, i18n.WriteDomain(ctx.Request, domain_id))
return
}
}
msg, err := this.models.Revoke(rst)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 419, i18n.Get(ctx.Request, msg), err)
return
}
hret.Success(ctx.ResponseWriter, i18n.Success(ctx.Request))
}
func init() {
// Registered in the static page to the groupCache system
groupcache.RegisterStaticFile("AuthorityPage", "./views/hauth/sys_batch_page.tpl")
}