-
Notifications
You must be signed in to change notification settings - Fork 66
/
user.go
500 lines (452 loc) · 16.9 KB
/
user.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package handler
import (
"github.com/aaronchen2k/deeptest/cmd/server/v1/domain"
integrationService "github.com/aaronchen2k/deeptest/integration/service"
"github.com/aaronchen2k/deeptest/internal/server/core/web/validate"
"github.com/aaronchen2k/deeptest/internal/server/modules/model"
"github.com/aaronchen2k/deeptest/internal/server/modules/repo"
"github.com/aaronchen2k/deeptest/internal/server/modules/service"
"github.com/aaronchen2k/deeptest/pkg/domain"
"github.com/aaronchen2k/deeptest/pkg/lib/log"
"strings"
"github.com/kataras/iris/v12"
"github.com/snowlyg/multi"
"go.uber.org/zap"
)
type UserCtrl struct {
BaseCtrl
UserService *service.UserService `inject:""`
UserRepo *repo.UserRepo `inject:""`
IntegrationProjectService *integrationService.ProjectService `inject:""`
}
// ListAll
// @Tags 用户管理
// @summary 用户列表
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param UserReqPaginate query serverDomain.UserReqPaginate true "查询参数"
// @success 200 {object} _domain.Response{data=_domain.PageData{result=[]serverDomain.UserResp}}
// @Router /api/v1/users [get]
func (c *UserCtrl) ListAll(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
var req serverDomain.UserReqPaginate
if err := ctx.ReadQuery(&req); err != nil {
errs := validate.ValidRequest(err)
if len(errs) > 0 {
_logUtils.Errorf("参数验证失败", zap.String("错误", strings.Join(errs, ";")))
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: strings.Join(errs, ";")})
return
}
}
data, err := c.UserRepo.Paginate(tenantId, req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: data, Msg: _domain.NoErr.Msg})
}
// GetUser 详情
// @Tags 用户管理
// @summary 用户详情
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param id path int true "用户ID"
// @success 200 {object} _domain.Response{data=serverDomain.UserResp}
// @Router /api/v1/users/{id} [get]
func (c *UserCtrl) GetUser(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
var req _domain.ReqId
if err := ctx.ReadParams(&req); err != nil {
_logUtils.Errorf("参数解析失败", zap.String("错误:", err.Error()))
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: _domain.ParamErr.Msg})
return
}
user, err := c.UserRepo.FindDetailById(tenantId, req.Id)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: _domain.SystemErr.Msg})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: user, Msg: _domain.NoErr.Msg})
}
// Invite 邀请用户
// @Tags 用户管理
// @summary 邀请用户
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param InviteUserReq body serverDomain.InviteUserReq true "邀请用户的请求参数"
// @success 200 {object} _domain.Response
// @Router /api/v1/users/invite [post]
func (c *UserCtrl) Invite(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
req := serverDomain.InviteUserReq{}
err := ctx.ReadJSON(&req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: err.Error()})
return
}
//TODO Saas免费版限制使用人数
if tenantId != "" && c.IntegrationProjectService.SaasUserLimit(tenantId, uint(req.ProjectId)) {
ctx.JSON(_domain.Response{Code: _domain.ErrUserProjectLimit.Code, Msg: _domain.ErrUserProjectLimit.Msg})
return
}
projectId, err := ctx.URLParamInt("currProjectId")
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: err.Error()})
return
}
req.ProjectId = projectId
_, bizErr := c.UserService.Invite(tenantId, req)
if bizErr != nil {
ctx.JSON(_domain.Response{Code: bizErr.Code})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Msg: _domain.NoErr.Msg})
}
// UpdateEmail 修改邮箱
// @Tags 用户管理
// @summary 修改邮箱
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param UpdateUserReq body serverDomain.UpdateUserReq true "修改邮箱的请求参数"
// @success 200 {object} _domain.Response
// @Router /api/v1/users/updateEmail [post]
func (c *UserCtrl) UpdateEmail(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
userId := multi.GetUserId(ctx)
req := serverDomain.UpdateUserReq{}
err := ctx.ReadJSON(&req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: err.Error()})
return
}
po, _ := c.UserRepo.FindByEmail(tenantId, req.Email, userId)
if po.Id > 0 {
bizErr := _domain.ErrEmailExist
ctx.JSON(_domain.Response{Code: bizErr.Code})
return
}
err = c.UserRepo.UpdateEmail(tenantId, req.Email, userId)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
user, err := c.UserRepo.FindDetailById(tenantId, userId)
user.Password = ""
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: user, Msg: _domain.NoErr.Msg})
}
// UpdateName 修改名称
// @Tags 用户管理
// @summary 修改名称
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param UpdateUserReq body serverDomain.UpdateUserReq true "修改名称的请求参数"
// @success 200 {object} _domain.Response{data=serverDomain.UserResp}
// @Router /api/v1/users/updateName [post]
func (c *UserCtrl) UpdateName(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
userId := multi.GetUserId(ctx)
req := serverDomain.UpdateUserReq{}
err := ctx.ReadJSON(&req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: err.Error()})
return
}
po, _ := c.UserRepo.FindByUserName(tenantId, req.Username, userId)
if po.Id > 0 {
bizErr := _domain.ErrUsernameExist
ctx.JSON(_domain.Response{Code: bizErr.Code})
return
}
err = c.UserRepo.UpdateName(tenantId, req.Username, userId)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
user, err := c.UserRepo.FindDetailById(tenantId, userId)
user.Password = ""
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: user, Msg: _domain.NoErr.Msg})
}
// UpdatePassword 修改密码
// @Tags 用户管理
// @summary 修改密码
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param UpdateUserReq body serverDomain.UpdateUserReq true "修改密码的请求参数"
// @success 200 {object} _domain.Response{data=serverDomain.UserResp}
// @Router /api/v1/users/updatePassword [post]
func (c *UserCtrl) UpdatePassword(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
userId := multi.GetUserId(ctx)
req := serverDomain.UpdateUserReq{}
err := ctx.ReadJSON(&req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: err.Error()})
return
}
err = c.UserRepo.ChangePassword(tenantId, req, userId)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
user, err := c.UserRepo.FindDetailById(tenantId, userId)
user.Password = ""
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: user, Msg: _domain.NoErr.Msg})
}
// Profile 个人信息
// @Tags 用户管理
// @summary 个人信息
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @success 200 {object} _domain.Response{data=serverDomain.UserResp}
// @Router /api/v1/users/profile [get]
func (c *UserCtrl) Profile(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
id := multi.GetUserId(ctx)
if id == 0 {
ctx.JSON(_domain.Response{Code: _domain.ErrNoUser.Code, Msg: _domain.SystemErr.Msg})
return
}
user, err := c.UserRepo.FindDetailById(tenantId, id)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
user.Password = ""
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: user, Msg: _domain.NoErr.Msg})
}
// Message 消息
func (c *UserCtrl) Message(ctx iris.Context) {
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Msg: _domain.NoErr.Msg})
}
// CreateUser 添加
// @Tags 用户管理
// @summary 新建用户
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param UserReq body serverDomain.UserReq true "新建用户的请求参数"
// @success 200 {object} _domain.Response
// @Router /api/v1/users [post]
func (c *UserCtrl) CreateUser(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
req := serverDomain.UserReq{}
if err := ctx.ReadJSON(&req); err != nil {
errs := validate.ValidRequest(err)
if len(errs) > 0 {
_logUtils.Errorf("参数验证失败", zap.String("错误", strings.Join(errs, ";")))
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: strings.Join(errs, ";")})
return
}
}
//区分手动手动添加的账号和域账号登录,true 为手动创建,非true 为域账号
req.Type = true
id, err := c.UserRepo.Create(tenantId, req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: iris.Map{"id": id}, Msg: _domain.NoErr.Msg})
}
// UpdateUser 更新
// @Tags 用户管理
// @summary 编辑用户
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param id path int true "用户ID"
// @Param UserReq body serverDomain.UserReq true "编辑用户的请求参数"
// @success 200 {object} _domain.Response
// @Router /api/v1/users/{id} [post]
func (c *UserCtrl) UpdateUser(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
userId := multi.GetUserId(ctx)
var reqId _domain.ReqId
if err := ctx.ReadParams(&reqId); err != nil {
_logUtils.Errorf("参数解析失败", zap.String("错误:", err.Error()))
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: _domain.ParamErr.Msg})
return
}
var req serverDomain.UserReq
if err := ctx.ReadJSON(&req); err != nil {
errs := validate.ValidRequest(err)
if len(errs) > 0 {
_logUtils.Errorf("参数验证失败", zap.String("错误", strings.Join(errs, ";")))
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: strings.Join(errs, ";")})
return
}
}
err := c.UserRepo.Update(tenantId, userId, reqId.Id, req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Msg: _domain.NoErr.Msg})
}
// DeleteUser 删除
// @Tags 用户管理
// @summary 删除用户
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param id path int true "用户ID"
// @success 200 {object} _domain.Response
// @Router /api/v1/users/{id} [delete]
func (c *UserCtrl) DeleteUser(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
var req _domain.ReqId
if err := ctx.ReadParams(&req); err != nil {
_logUtils.Errorf("参数解析失败", zap.String("错误:", err.Error()))
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: _domain.ParamErr.Msg})
return
}
err := c.UserRepo.DeleteById(tenantId, req.Id)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Msg: _domain.NoErr.Msg})
}
// Logout 退出
// @Tags 用户管理
// @summary 退出登录
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @success 200 {object} _domain.Response
// @Router /api/v1/users/logout [get]
func (c *UserCtrl) Logout(ctx iris.Context) {
token := multi.GetVerifiedToken(ctx)
if token == nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: "授权凭证为空"})
return
}
err := c.UserRepo.DelToken(string(token))
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Msg: _domain.NoErr.Msg})
}
// Clear 清空 token
// @Tags 用户管理
// @summary 清空 token
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @success 200 {object} _domain.Response
// @Router /api/v1/users/clear [get]
func (c *UserCtrl) Clear(ctx iris.Context) {
token := multi.GetVerifiedToken(ctx)
if token == nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: "授权凭证为空"})
return
}
if err := c.UserRepo.CleanToken(multi.AdminAuthority, string(token)); err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Msg: _domain.NoErr.Msg})
}
// ChangeAvatar 修改头像
// @Tags 用户管理
// @summary 修改头像
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param Avatar body model.Avatar true "头像"
// @success 200 {object} _domain.Response
// @Router /api/v1/users/change_avatar [post]
func (c *UserCtrl) ChangeAvatar(ctx iris.Context) {
avatar := &model.Avatar{}
if err := ctx.ReadJSON(avatar); err != nil {
errs := validate.ValidRequest(err)
if len(errs) > 0 {
_logUtils.Errorf("参数验证失败", zap.String("错误", strings.Join(errs, ";")))
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: strings.Join(errs, ";")})
return
}
}
err := c.UserRepo.UpdateAvatar(multi.GetUserId(ctx), avatar.Avatar)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Msg: _domain.NoErr.Msg})
}
// GetUsersNotExistedInProject
// @Tags 用户管理
// @summary 获取项目中没有的用户列表
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @success 200 {object} _domain.Response{data=object{result=[]serverDomain.UserResp}}
// @Router /api/v1/users/usersNotExistedInProject [get]
func (c *UserCtrl) GetUsersNotExistedInProject(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
projectId, _ := ctx.URLParamInt("currProjectId")
users, err := c.UserService.GetUsersNotExistedInProject(tenantId, uint(projectId))
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
data := iris.Map{"result": users}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: data, Msg: _domain.NoErr.Msg})
}
// ChangeUserSysRole
// @Tags 用户管理
// @summary 更新用户系统角色
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param UpdateUserRoleReq body serverDomain.UpdateUserRoleReq true "更新用户系统角色的请求参数"
// @success 200 {object} _domain.Response
// @Router /api/v1/users/changeUserSysRole [post]
func (c *UserCtrl) ChangeUserSysRole(ctx iris.Context) {
//SAAS
tenantId := c.getTenantId(ctx)
req := serverDomain.UpdateUserRoleReq{}
err := ctx.ReadJSON(&req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: err.Error()})
return
}
if err = c.UserService.UpdateSysRoleForUser(tenantId, req.UserId, req.RoleIds); err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Msg: _domain.NoErr.Msg})
}