@@ -7,6 +7,7 @@ package admin
77
88import (
99 "errors"
10+ "net/http"
1011
1112 "code.gitea.io/gitea/models"
1213 "code.gitea.io/gitea/modules/context"
@@ -26,9 +27,9 @@ func parseLoginSource(ctx *context.APIContext, u *models.User, sourceID int64, l
2627 source , err := models .GetLoginSourceByID (sourceID )
2728 if err != nil {
2829 if models .IsErrLoginSourceNotExist (err ) {
29- ctx .Error (422 , "" , err )
30+ ctx .Error (http . StatusUnprocessableEntity , "" , err )
3031 } else {
31- ctx .Error (500 , "GetLoginSourceByID" , err )
32+ ctx .Error (http . StatusInternalServerError , "GetLoginSourceByID" , err )
3233 }
3334 return
3435 }
@@ -57,8 +58,11 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
5758 // "$ref": "#/responses/User"
5859 // "403":
5960 // "$ref": "#/responses/forbidden"
61+ // "400":
62+ // "$ref": "#/responses/error"
6063 // "422":
6164 // "$ref": "#/responses/validationError"
65+
6266 u := & models.User {
6367 Name : form .Username ,
6468 FullName : form .FullName ,
@@ -78,17 +82,17 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
7882 }
7983 if ! password .IsComplexEnough (form .Password ) {
8084 err := errors .New ("PasswordComplexity" )
81- ctx .Error (400 , "PasswordComplexity" , err )
85+ ctx .Error (http . StatusBadRequest , "PasswordComplexity" , err )
8286 return
8387 }
8488 if err := models .CreateUser (u ); err != nil {
8589 if models .IsErrUserAlreadyExist (err ) ||
8690 models .IsErrEmailAlreadyUsed (err ) ||
8791 models .IsErrNameReserved (err ) ||
8892 models .IsErrNamePatternNotAllowed (err ) {
89- ctx .Error (422 , "" , err )
93+ ctx .Error (http . StatusUnprocessableEntity , "" , err )
9094 } else {
91- ctx .Error (500 , "CreateUser" , err )
95+ ctx .Error (http . StatusInternalServerError , "CreateUser" , err )
9296 }
9397 return
9498 }
@@ -98,7 +102,7 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
98102 if form .SendNotify {
99103 mailer .SendRegisterNotifyMail (ctx .Locale , u )
100104 }
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 ))
102106}
103107
104108// EditUser api for modifying a user's information
@@ -127,6 +131,7 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
127131 // "$ref": "#/responses/forbidden"
128132 // "422":
129133 // "$ref": "#/responses/validationError"
134+
130135 u := user .GetUserByParams (ctx )
131136 if ctx .Written () {
132137 return
@@ -140,12 +145,12 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
140145 if len (form .Password ) > 0 {
141146 if ! password .IsComplexEnough (form .Password ) {
142147 err := errors .New ("PasswordComplexity" )
143- ctx .Error (400 , "PasswordComplexity" , err )
148+ ctx .Error (http . StatusBadRequest , "PasswordComplexity" , err )
144149 return
145150 }
146151 var err error
147152 if u .Salt , err = models .GetUserSalt (); err != nil {
148- ctx .Error (500 , "UpdateUser" , err )
153+ ctx .Error (http . StatusInternalServerError , "UpdateUser" , err )
149154 return
150155 }
151156 u .HashPassword (form .Password )
@@ -184,15 +189,15 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
184189
185190 if err := models .UpdateUser (u ); err != nil {
186191 if models .IsErrEmailAlreadyUsed (err ) {
187- ctx .Error (422 , "" , err )
192+ ctx .Error (http . StatusUnprocessableEntity , "" , err )
188193 } else {
189- ctx .Error (500 , "UpdateUser" , err )
194+ ctx .Error (http . StatusInternalServerError , "UpdateUser" , err )
190195 }
191196 return
192197 }
193198 log .Trace ("Account profile updated by admin (%s): %s" , ctx .User .Name , u .Name )
194199
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 ))
196201}
197202
198203// DeleteUser api for deleting a user
@@ -215,6 +220,7 @@ func DeleteUser(ctx *context.APIContext) {
215220 // "$ref": "#/responses/forbidden"
216221 // "422":
217222 // "$ref": "#/responses/validationError"
223+
218224 u := user .GetUserByParams (ctx )
219225 if ctx .Written () {
220226 return
@@ -223,15 +229,15 @@ func DeleteUser(ctx *context.APIContext) {
223229 if err := models .DeleteUser (u ); err != nil {
224230 if models .IsErrUserOwnRepos (err ) ||
225231 models .IsErrUserHasOrgs (err ) {
226- ctx .Error (422 , "" , err )
232+ ctx .Error (http . StatusUnprocessableEntity , "" , err )
227233 } else {
228- ctx .Error (500 , "DeleteUser" , err )
234+ ctx .Error (http . StatusInternalServerError , "DeleteUser" , err )
229235 }
230236 return
231237 }
232238 log .Trace ("Account deleted by admin(%s): %s" , ctx .User .Name , u .Name )
233239
234- ctx .Status (204 )
240+ ctx .Status (http . StatusNoContent )
235241}
236242
237243// CreatePublicKey api for creating a public key to a user
@@ -260,6 +266,7 @@ func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
260266 // "$ref": "#/responses/forbidden"
261267 // "422":
262268 // "$ref": "#/responses/validationError"
269+
263270 u := user .GetUserByParams (ctx )
264271 if ctx .Written () {
265272 return
@@ -293,6 +300,7 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
293300 // "$ref": "#/responses/forbidden"
294301 // "404":
295302 // "$ref": "#/responses/notFound"
303+
296304 u := user .GetUserByParams (ctx )
297305 if ctx .Written () {
298306 return
@@ -302,15 +310,15 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
302310 if models .IsErrKeyNotExist (err ) {
303311 ctx .NotFound ()
304312 } 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" )
306314 } else {
307- ctx .Error (500 , "DeleteUserPublicKey" , err )
315+ ctx .Error (http . StatusInternalServerError , "DeleteUserPublicKey" , err )
308316 }
309317 return
310318 }
311319 log .Trace ("Key deleted by admin(%s): %s" , ctx .User .Name , u .Name )
312320
313- ctx .Status (204 )
321+ ctx .Status (http . StatusNoContent )
314322}
315323
316324//GetAllUsers API for getting information of all the users
@@ -325,13 +333,14 @@ func GetAllUsers(ctx *context.APIContext) {
325333 // "$ref": "#/responses/UserList"
326334 // "403":
327335 // "$ref": "#/responses/forbidden"
336+
328337 users , _ , err := models .SearchUsers (& models.SearchUserOptions {
329338 Type : models .UserTypeIndividual ,
330339 OrderBy : models .SearchOrderByAlphabetically ,
331340 PageSize : - 1 ,
332341 })
333342 if err != nil {
334- ctx .Error (500 , "GetAllUsers" , err )
343+ ctx .Error (http . StatusInternalServerError , "GetAllUsers" , err )
335344 return
336345 }
337346
@@ -340,5 +349,5 @@ func GetAllUsers(ctx *context.APIContext) {
340349 results [i ] = convert .ToUser (users [i ], ctx .IsSigned , ctx .User .IsAdmin )
341350 }
342351
343- ctx .JSON (200 , & results )
352+ ctx .JSON (http . StatusOK , & results )
344353}
0 commit comments