-
Notifications
You must be signed in to change notification settings - Fork 180
/
domainShareController.go
464 lines (418 loc) · 11.8 KB
/
domainShareController.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// Copyright 2017 The hzwy23 . All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// this package provide method which handle domain share action
package controllers
import (
"encoding/json"
"html/template"
"github.com/astaxie/beego/context"
"github.com/hzwy23/hauth/core/hrpc"
"github.com/hzwy23/hauth/core/models"
"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 DomainShareController struct {
models *models.DomainShareModel
}
var DomainShareCtl = DomainShareController{
models: new(models.DomainShareModel),
}
// swagger:operation GET /v1/auth/domain/share/page StaticFiles DomainShareController
//
// 返回共享域管理页面
//
// 用户需要首先登录系统, 系统会对用户权限进行校验,校验成功,将会返回共享域管理页面.
//
// 用户需要在request中传入domain_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
// '403':
// description: Insufficient permissions
// '419':
// description: get domain information failed
// '404':
// description: page not found
func (DomainShareController) Page(ctx *context.Context) {
ctx.Request.ParseForm()
// Check the user permissions
if !hrpc.BasicAuth(ctx.Request) {
hret.Error(ctx.ResponseWriter, 403, i18n.NoAuth(ctx.Request))
return
}
// check the domain details
// config this domain to others
var domain_id = ctx.Request.FormValue("domain_id")
if !hrpc.DomainAuth(ctx.Request, domain_id, "r") {
hret.Error(ctx.ResponseWriter, 403, i18n.ReadDomain(ctx.Request, domain_id))
return
}
// get the domain details info
rst, err := DomainCtl.models.GetRow(domain_id)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 419, i18n.Get(ctx.Request, "as_of_date_domain_get_info_failed"), err)
return
}
hz, _ := template.ParseFiles("./views/hauth/domain_share_info.tpl")
hz.Execute(ctx.ResponseWriter, rst)
}
// swagger:operation GET /v1/auth/domain/share/get domainShareController getdomainShareControll
//
// 返回某个域的共享对象
//
// 客户端在请求中传入域,系统将会查询这个域的共享独享列表.
//
// ---
// 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
// '403':
// description: Insufficient permissions
// '419':
// description: get domain share information failed.
func (this DomainShareController) 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 the request argument domain_id is empty,
// so set domain_id yourself.
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.ReadDomain(ctx.Request, domain_id))
return
}
// get domain_id share info
rst, err := this.models.Get(domain_id)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, "as_of_date_domain_get_info_failed"), err)
return
}
hret.Json(ctx.ResponseWriter, rst)
}
// swagger:operation GET /v1/auth/domain/share/unauth domainShareController getdomainShareControll
//
// 返回某个指定的域没有共享的对象
//
// 根据传入的domain_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 DomainShareController) UnAuth(ctx *context.Context) {
ctx.Request.ParseForm()
domain_id := ctx.Request.FormValue("domain_id")
if validator.IsEmpty(domain_id) {
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, "as_of_date_domain_isempty"))
return
}
rst, err := this.models.UnAuth(domain_id)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 419, i18n.Get(ctx.Request, "as_of_date_check_unshare"))
return
}
hret.Json(ctx.ResponseWriter, rst)
}
// swagger:operation POST /v1/auth/domain/share/post domainShareController postomainShareControll
//
// 新增共享域信息
//
// 首先,系统会校验用户的权限信息,如果用户被授权,则进行字段校验,如果新增的域字段信息格式正确,将会写入数据库.
//
// ---
// produces:
// - application/json
// - application/xml
// - text/xml
// - text/html
// parameters:
// - name: domain_id
// in: query
// description: domain code number
// required: true
// type: string
// format:
// - name: target_domain_id
// in: query
// description: domain code number
// required: true
// type: string
// format:
// - name: auth_level
// in: query
// description: domain code number
// required: true
// type: string
// format:
// responses:
// '200':
// description: success
func (this DomainShareController) 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
domain_id := form.Get("domain_id")
if !hrpc.DomainAuth(ctx.Request, domain_id, "w") {
hret.Error(ctx.ResponseWriter, 420, 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, 421, i18n.Disconnect(ctx.Request))
return
}
msg, err := this.models.Post(form, jclaim.UserId)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 419, i18n.Get(ctx.Request, msg))
return
}
hret.Success(ctx.ResponseWriter, i18n.Success(ctx.Request))
}
// swagger:operation POST /v1/auth/domain/share/delete domainShareController postomainShareControll
//
// 删除某个域指定域的共享对象
//
// 在用户请求时,需要传入用户账号
//
// ---
// produces:
// - application/json
// - application/xml
// - text/xml
// - text/html
// parameters:
// - name: domain_id
// in: query
// description: domain code number
// required: true
// type: string
// format:
// - name: JSON
// in: query
// description: json格式信息,例如[{Uuid\:value}]
// required: true
// type: string
// format:
// responses:
// '200':
// description: success
func (this DomainShareController) Delete(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 !hrpc.DomainAuth(ctx.Request, domain_id, "w") {
hret.Error(ctx.ResponseWriter, 420, i18n.Get(ctx.Request, "as_of_date_domain_permission_denied_modify"))
return
}
var rst []models.DomainShareData
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"))
return
}
// delete share domain info
msg, err := this.models.Delete(rst, domain_id)
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 PUT /v1/auth/domain/share/put domainShareController postomainShareControll
//
// 更新指定域的共享对象
//
// 在请求更新时,需要传入域id,域的共享对象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:
// - name: target_domain_id
// in: query
// description: domain code number
// required: true
// type: string
// format:
// - name: auth_level
// in: query
// description: domain code number
// required: true
// type: string
// format:
// responses:
// '200':
// description: success
func (this DomainShareController) Put(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
domain_id := form.Get("domain_id")
if !hrpc.DomainAuth(ctx.Request, domain_id, "w") {
hret.Error(ctx.ResponseWriter, 420, i18n.Get(ctx.Request, "as_of_date_domain_permission_denied_modify"))
return
}
// get user session from cookies
cookie, _ := ctx.Request.Cookie("Authorization")
jclaim, err := jwt.ParseJwt(cookie.Value)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 421, i18n.Disconnect(ctx.Request))
return
}
msg, err := this.models.Update(form, 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.Get(ctx.Request, "success"))
}
// swagger:operation GET /v1/auth/domain/owner domainShareController postomainShareControll
//
// 获取用户能够访问到的域信息.
//
// 当一个域A被共享给域B,则B将可以访问到A中的信息,这个API,将会放回某个指定域所有能够被访问到的对象.
//
// ---
// produces:
// - application/json
// - application/xml
// - text/xml
// - text/html
// responses:
// '200':
// description: success
// '403':
// description: Insufficient permissions
// '421':
// description: get domain that user is able to access failed.
func (this *DomainShareController) GetAccessDomain(ctx *context.Context) {
ctx.Request.ParseForm()
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
}
rst, err := this.models.GetList(jclaim.DomainId)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, "as_of_date_domain_getowner"))
return
}
hret.Json(ctx.ResponseWriter, rst)
}
// swagger:operation GET /v1/auth/domain/self/owner domainShareController postomainShareControll
//
// 获取用户能够访问到的域信息.
//
// 当一个域A被共享给域B,则B将可以访问到A中的信息,这个API,将会放回某个指定域所有能够被访问到的对象.
//
// ---
// produces:
// - application/json
// - application/xml
// - text/xml
// - text/html
// responses:
// '200':
// description: success
func (this *DomainShareController) GetDomainOwner(ctx *context.Context) {
ctx.Request.ParseForm()
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
}
rst, err := this.models.GetOwner(jclaim.DomainId)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, "as_of_date_domains_of_user"))
return
}
hret.Json(ctx.ResponseWriter, rst)
}