@@ -7,6 +7,7 @@ package admin
7
7
8
8
import (
9
9
"errors"
10
+ "net/http"
10
11
11
12
"code.gitea.io/gitea/models"
12
13
"code.gitea.io/gitea/modules/context"
@@ -26,9 +27,9 @@ func parseLoginSource(ctx *context.APIContext, u *models.User, sourceID int64, l
26
27
source , err := models .GetLoginSourceByID (sourceID )
27
28
if err != nil {
28
29
if models .IsErrLoginSourceNotExist (err ) {
29
- ctx .Error (422 , "" , err )
30
+ ctx .Error (http . StatusUnprocessableEntity , "" , err )
30
31
} else {
31
- ctx .Error (500 , "GetLoginSourceByID" , err )
32
+ ctx .Error (http . StatusInternalServerError , "GetLoginSourceByID" , err )
32
33
}
33
34
return
34
35
}
@@ -57,8 +58,11 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
57
58
// "$ref": "#/responses/User"
58
59
// "403":
59
60
// "$ref": "#/responses/forbidden"
61
+ // "400":
62
+ // "$ref": "#/responses/error"
60
63
// "422":
61
64
// "$ref": "#/responses/validationError"
65
+
62
66
u := & models.User {
63
67
Name : form .Username ,
64
68
FullName : form .FullName ,
@@ -78,17 +82,17 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
78
82
}
79
83
if ! password .IsComplexEnough (form .Password ) {
80
84
err := errors .New ("PasswordComplexity" )
81
- ctx .Error (400 , "PasswordComplexity" , err )
85
+ ctx .Error (http . StatusBadRequest , "PasswordComplexity" , err )
82
86
return
83
87
}
84
88
if err := models .CreateUser (u ); err != nil {
85
89
if models .IsErrUserAlreadyExist (err ) ||
86
90
models .IsErrEmailAlreadyUsed (err ) ||
87
91
models .IsErrNameReserved (err ) ||
88
92
models .IsErrNamePatternNotAllowed (err ) {
89
- ctx .Error (422 , "" , err )
93
+ ctx .Error (http . StatusUnprocessableEntity , "" , err )
90
94
} else {
91
- ctx .Error (500 , "CreateUser" , err )
95
+ ctx .Error (http . StatusInternalServerError , "CreateUser" , err )
92
96
}
93
97
return
94
98
}
@@ -98,7 +102,7 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
98
102
if form .SendNotify {
99
103
mailer .SendRegisterNotifyMail (ctx .Locale , u )
100
104
}
101
- ctx .JSON (201 , convert .ToUser (u , ctx .IsSigned , ctx .User .IsAdmin ))
105
+ ctx .JSON (http . StatusCreated , convert .ToUser (u , ctx .IsSigned , ctx .User .IsAdmin ))
102
106
}
103
107
104
108
// EditUser api for modifying a user's information
@@ -127,6 +131,7 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
127
131
// "$ref": "#/responses/forbidden"
128
132
// "422":
129
133
// "$ref": "#/responses/validationError"
134
+
130
135
u := user .GetUserByParams (ctx )
131
136
if ctx .Written () {
132
137
return
@@ -140,12 +145,12 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
140
145
if len (form .Password ) > 0 {
141
146
if ! password .IsComplexEnough (form .Password ) {
142
147
err := errors .New ("PasswordComplexity" )
143
- ctx .Error (400 , "PasswordComplexity" , err )
148
+ ctx .Error (http . StatusBadRequest , "PasswordComplexity" , err )
144
149
return
145
150
}
146
151
var err error
147
152
if u .Salt , err = models .GetUserSalt (); err != nil {
148
- ctx .Error (500 , "UpdateUser" , err )
153
+ ctx .Error (http . StatusInternalServerError , "UpdateUser" , err )
149
154
return
150
155
}
151
156
u .HashPassword (form .Password )
@@ -184,15 +189,15 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
184
189
185
190
if err := models .UpdateUser (u ); err != nil {
186
191
if models .IsErrEmailAlreadyUsed (err ) {
187
- ctx .Error (422 , "" , err )
192
+ ctx .Error (http . StatusUnprocessableEntity , "" , err )
188
193
} else {
189
- ctx .Error (500 , "UpdateUser" , err )
194
+ ctx .Error (http . StatusInternalServerError , "UpdateUser" , err )
190
195
}
191
196
return
192
197
}
193
198
log .Trace ("Account profile updated by admin (%s): %s" , ctx .User .Name , u .Name )
194
199
195
- ctx .JSON (200 , convert .ToUser (u , ctx .IsSigned , ctx .User .IsAdmin ))
200
+ ctx .JSON (http . StatusOK , convert .ToUser (u , ctx .IsSigned , ctx .User .IsAdmin ))
196
201
}
197
202
198
203
// DeleteUser api for deleting a user
@@ -215,6 +220,7 @@ func DeleteUser(ctx *context.APIContext) {
215
220
// "$ref": "#/responses/forbidden"
216
221
// "422":
217
222
// "$ref": "#/responses/validationError"
223
+
218
224
u := user .GetUserByParams (ctx )
219
225
if ctx .Written () {
220
226
return
@@ -223,15 +229,15 @@ func DeleteUser(ctx *context.APIContext) {
223
229
if err := models .DeleteUser (u ); err != nil {
224
230
if models .IsErrUserOwnRepos (err ) ||
225
231
models .IsErrUserHasOrgs (err ) {
226
- ctx .Error (422 , "" , err )
232
+ ctx .Error (http . StatusUnprocessableEntity , "" , err )
227
233
} else {
228
- ctx .Error (500 , "DeleteUser" , err )
234
+ ctx .Error (http . StatusInternalServerError , "DeleteUser" , err )
229
235
}
230
236
return
231
237
}
232
238
log .Trace ("Account deleted by admin(%s): %s" , ctx .User .Name , u .Name )
233
239
234
- ctx .Status (204 )
240
+ ctx .Status (http . StatusNoContent )
235
241
}
236
242
237
243
// CreatePublicKey api for creating a public key to a user
@@ -260,6 +266,7 @@ func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
260
266
// "$ref": "#/responses/forbidden"
261
267
// "422":
262
268
// "$ref": "#/responses/validationError"
269
+
263
270
u := user .GetUserByParams (ctx )
264
271
if ctx .Written () {
265
272
return
@@ -293,6 +300,7 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
293
300
// "$ref": "#/responses/forbidden"
294
301
// "404":
295
302
// "$ref": "#/responses/notFound"
303
+
296
304
u := user .GetUserByParams (ctx )
297
305
if ctx .Written () {
298
306
return
@@ -302,15 +310,15 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
302
310
if models .IsErrKeyNotExist (err ) {
303
311
ctx .NotFound ()
304
312
} else if models .IsErrKeyAccessDenied (err ) {
305
- ctx .Error (403 , "" , "You do not have access to this key" )
313
+ ctx .Error (http . StatusForbidden , "" , "You do not have access to this key" )
306
314
} else {
307
- ctx .Error (500 , "DeleteUserPublicKey" , err )
315
+ ctx .Error (http . StatusInternalServerError , "DeleteUserPublicKey" , err )
308
316
}
309
317
return
310
318
}
311
319
log .Trace ("Key deleted by admin(%s): %s" , ctx .User .Name , u .Name )
312
320
313
- ctx .Status (204 )
321
+ ctx .Status (http . StatusNoContent )
314
322
}
315
323
316
324
//GetAllUsers API for getting information of all the users
@@ -325,13 +333,14 @@ func GetAllUsers(ctx *context.APIContext) {
325
333
// "$ref": "#/responses/UserList"
326
334
// "403":
327
335
// "$ref": "#/responses/forbidden"
336
+
328
337
users , _ , err := models .SearchUsers (& models.SearchUserOptions {
329
338
Type : models .UserTypeIndividual ,
330
339
OrderBy : models .SearchOrderByAlphabetically ,
331
340
PageSize : - 1 ,
332
341
})
333
342
if err != nil {
334
- ctx .Error (500 , "GetAllUsers" , err )
343
+ ctx .Error (http . StatusInternalServerError , "GetAllUsers" , err )
335
344
return
336
345
}
337
346
@@ -340,5 +349,5 @@ func GetAllUsers(ctx *context.APIContext) {
340
349
results [i ] = convert .ToUser (users [i ], ctx .IsSigned , ctx .User .IsAdmin )
341
350
}
342
351
343
- ctx .JSON (200 , & results )
352
+ ctx .JSON (http . StatusOK , & results )
344
353
}
0 commit comments