/
user.go
394 lines (347 loc) · 10.6 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
package api
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/saltbo/gopkg/ginutil"
_ "github.com/saltbo/gopkg/httputil"
"github.com/saltbo/gopkg/strutil"
"github.com/saltbo/zpan/internal/app/dao"
"github.com/saltbo/zpan/internal/app/model"
"github.com/saltbo/zpan/internal/app/service"
"github.com/saltbo/zpan/internal/pkg/authed"
"github.com/saltbo/zpan/internal/pkg/bind"
)
type UserResource struct {
dUser *dao.User
sUser *service.User
}
func NewUserResource() *UserResource {
return &UserResource{
dUser: dao.NewUser(),
sUser: service.NewUser(),
}
}
func (rs *UserResource) Register(router *gin.RouterGroup) {
router.POST("/users", rs.create) // 账户注册
router.PATCH("/users/:email", rs.patch) // 账户激活、密码重置
router.GET("/users", rs.findAll) // 查询用户列表,需管理员权限
router.GET("/users/:username", rs.find) // 查询某一个用户的公开信息
router.DELETE("/users/:username", rs.remove) // 删除某一个用户
router.PUT("/users/:username/storage", rs.updateStorage) // 修改某一个用户的存储空间
router.PUT("/users/:username/password", rs.resetPassword) // 修改某一个用户的用户密码
router.PUT("/users/:username/status", rs.updateStatus) // 修改某一个用户的状态
router.GET("/user", rs.userMe) // 获取已登录用户的所有信息
router.PUT("/user/profile", rs.updateProfile) // 更新已登录用户个人信息
router.PUT("/user/password", rs.updatePassword) // 修改已登录用户密码
}
// create godoc
// @Tags Users
// @Summary 用户注册
// @Description 注册一个用户
// @Accept json
// @Produce json
// @Param body body bind.BodyUserCreation true "参数"
// @Success 200 {object} httputil.JSONResponse{data=model.User}
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /users [post]
func (rs *UserResource) create(c *gin.Context) {
p := new(bind.BodyUserCreation)
if err := c.ShouldBindJSON(p); err != nil {
ginutil.JSONBadRequest(c, err)
return
}
if !authed.IsAdmin(c) && rs.sUser.InviteRequired() && p.Ticket == "" {
ginutil.JSONBadRequest(c, fmt.Errorf("ticket required"))
return
}
opt := model.NewUserCreateOption()
opt.Roles = model.RoleMember
opt.Ticket = p.Ticket
if authed.IsAdmin(c) {
opt.Roles = p.Roles
opt.StorageMax = p.StorageMax
}
opt.Origin = ginutil.GetOrigin(c)
if _, err := rs.sUser.Signup(p.Email, p.Password, opt); err != nil {
ginutil.JSONBadRequest(c, err)
return
}
ginutil.JSON(c)
}
// patch godoc
// @Tags Users
// @Summary 更新一项用户信息
// @Description 用于账户激活和密码重置
// @Accept json
// @Produce json
// @Param email path string true "邮箱"
// @Param body body bind.BodyUserPatch true "参数"
// @Success 200 {object} httputil.JSONResponse
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /users/{email} [patch]
func (rs *UserResource) patch(c *gin.Context) {
p := new(bind.BodyUserPatch)
if err := c.ShouldBindJSON(p); err != nil {
ginutil.JSONBadRequest(c, err)
return
}
// account activate
if p.Activated {
if err := rs.sUser.Active(p.Token); err != nil {
ginutil.JSONServerError(c, err)
return
}
}
// password reset
if p.Password != "" {
if err := rs.sUser.PasswordReset(p.Token, p.Password); err != nil {
ginutil.JSONServerError(c, err)
return
}
}
ginutil.JSON(c)
}
// findAll godoc
// @Tags Users
// @Summary 用户列表
// @Description 获取用户列表信息
// @Accept json
// @Produce json
// @Security OAuth2Application[admin]
// @Param query query bind.QueryUser true "参数"
// @Success 200 {object} httputil.JSONResponse{data=gin.H{list=[]model.User,total=int64}}
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /users [get]
func (rs *UserResource) findAll(c *gin.Context) {
p := new(bind.QueryUser)
if err := c.BindQuery(p); err != nil {
ginutil.JSONBadRequest(c, err)
return
}
query := dao.NewQuery()
query.WithPage(p.PageNo, p.PageSize)
if p.Email != "" {
query.WithLike("email", p.Email)
}
list, total, err := rs.dUser.FindAll(query)
if err != nil {
ginutil.JSONServerError(c, err)
return
}
ginutil.JSONList(c, list, total)
}
// find godoc
// @Tags Users
// @Summary 用户查询
// @Description 获取一个用户的公开信息
// @Accept json
// @Produce json
// @Param username path string true "用户名"
// @Success 200 {object} httputil.JSONResponse{data=model.UserProfile}
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /users/{username} [get]
func (rs *UserResource) find(c *gin.Context) {
user, exist := rs.dUser.UsernameExist(c.Param("username"))
if !exist {
ginutil.JSONServerError(c, fmt.Errorf("user not exist"))
return
}
ginutil.JSONData(c, user.Profile)
}
// updateStorage godoc
// @Tags Users
// @Summary 修改某一个用户的存储空间
// @Description 修改某一个用户的存储空间
// @Accept json
// @Produce json
// @Security OAuth2Application[admin]
// @Param username path string true "用户名"
// @Param body body bind.BodyUserPassword true "参数"
// @Success 200 {object} httputil.JSONResponse
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /users/{username}/storage [put]
func (rs *UserResource) updateStorage(c *gin.Context) {
p := new(bind.BodyUserStorage)
if err := c.ShouldBindJSON(p); err != nil {
ginutil.JSONBadRequest(c, err)
return
}
user, err := rs.dUser.FindByUsername(c.Param("username"))
if err != nil {
ginutil.JSONBadRequest(c, err)
return
}
if err := rs.dUser.UpdateStorage(user.Id, p.Max); err != nil {
ginutil.JSONServerError(c, err)
return
}
ginutil.JSON(c)
}
// updateStatus godoc
// @Tags Users
// @Summary 修改某一个用户的状态
// @Description 修改某一个用户的状态
// @Accept json
// @Produce json
// @Security OAuth2Application[admin]
// @Param username path string true "用户名"
// @Param body body bind.BodyUserStatus true "参数"
// @Success 200 {object} httputil.JSONResponse
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /users/{username}/status [put]
func (rs *UserResource) updateStatus(c *gin.Context) {
p := new(bind.BodyUserStatus)
if err := c.ShouldBindJSON(p); err != nil {
ginutil.JSONBadRequest(c, err)
return
}
user, err := rs.dUser.FindByUsername(c.Param("username"))
if err != nil {
ginutil.JSONBadRequest(c, err)
return
}
if err := rs.dUser.UpdateStatus(user.Id, p.Status); err != nil {
ginutil.JSONServerError(c, err)
return
}
ginutil.JSON(c)
}
// resetPassword godoc
// @Tags Users
// @Summary 重置某一个用户的密码
// @Description 重置某一个用户的密码
// @Accept json
// @Produce json
// @Security OAuth2Application[admin]
// @Param username path string true "用户名"
// @Param body body bind.BodyUserStatus true "参数"
// @Success 200 {object} httputil.JSONResponse
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /users/{username}/password [put]
func (rs *UserResource) resetPassword(c *gin.Context) {
p := new(bind.BodyUserPasswordReset)
if err := c.ShouldBindJSON(p); err != nil {
ginutil.JSONBadRequest(c, err)
return
}
user, err := rs.dUser.FindByUsername(c.Param("username"))
if err != nil {
ginutil.JSONBadRequest(c, err)
return
}
user.Password = strutil.Md5Hex(p.Password)
if err := rs.dUser.Update(user); err != nil {
ginutil.JSONServerError(c, err)
return
}
ginutil.JSON(c)
}
// remove godoc
// @Tags Users
// @Summary 删除某一个用户
// @Description 删除某一个用户
// @Accept json
// @Produce json
// @Security OAuth2Application[admin]
// @Param username path string true "用户名"
// @Success 200 {object} httputil.JSONResponse
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /users/{username} [delete]
func (rs *UserResource) remove(c *gin.Context) {
user, err := rs.dUser.FindByUsername(c.Param("username"))
if err != nil {
ginutil.JSONBadRequest(c, err)
return
}
if err := rs.dUser.Delete(user); err != nil {
ginutil.JSONServerError(c, err)
return
}
ginutil.JSON(c)
}
// profile godoc
// @Tags Users
// @Summary 当前登录用户信息
// @Description 获取已登录用户的详细信息
// @Accept json
// @Produce json
// @Success 200 {object} httputil.JSONResponse{data=gin.H{user=model.User,profile=model.UserProfile}}
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /user [get]
func (rs *UserResource) userMe(c *gin.Context) {
user, err := rs.dUser.Find(authed.UidGet(c))
if err != nil {
ginutil.JSONServerError(c, err)
return
}
ginutil.JSONData(c, user)
}
// updatePassword godoc
// @Tags Users
// @Summary 修改登录用户密码
// @Description 修改登录用户密码
// @Accept json
// @Produce json
// @Param body body bind.BodyUserPassword true "参数"
// @Success 200 {object} httputil.JSONResponse
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /user/password [put]
func (rs *UserResource) updatePassword(c *gin.Context) {
p := new(bind.BodyUserPassword)
if err := c.ShouldBindJSON(p); err != nil {
ginutil.JSONBadRequest(c, err)
return
}
uid := authed.UidGet(c)
if err := rs.sUser.PasswordUpdate(uid, p.OldPassword, p.NewPassword); err != nil {
ginutil.JSONServerError(c, err)
return
}
ginutil.JSON(c)
}
// updateProfile godoc
// @Tags Users
// @Summary 修改个人信息
// @Description 更新用户的个人信息
// @Accept json
// @Produce json
// @Param body body bind.BodyUserProfile true "参数"
// @Success 200 {object} httputil.JSONResponse
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /user/profile [put]
func (rs *UserResource) updateProfile(c *gin.Context) {
p := new(bind.BodyUserProfile)
if err := c.ShouldBindJSON(p); err != nil {
ginutil.JSONBadRequest(c, err)
return
}
user, err := rs.dUser.Find(authed.UidGet(c))
if err != nil {
ginutil.JSONServerError(c, err)
return
}
userProfile := new(model.UserProfile)
userProfile.Avatar = p.Avatar
userProfile.Nickname = p.Nickname
userProfile.Bio = p.Bio
userProfile.URL = p.URL
userProfile.Company = p.Company
userProfile.Location = p.Location
userProfile.Locale = p.Locale
if err := rs.dUser.UpdateProfile(user.Id, userProfile); err != nil {
ginutil.JSONServerError(c, err)
return
}
ginutil.JSON(c)
}