From 8d5c795cc4cc6b254a897461a860f25b0708ee14 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 18 Feb 2021 09:25:35 +0100 Subject: [PATCH] [API] Add Restricted Field to User (#14630) * Expose Restricted field for User * Add Option to Change Restricted on User via adminEditUser API * Add test who change restricted & test if it changed it ... * make generate-swagger Co-authored-by: zeripath Co-authored-by: Lunny Xiao --- integrations/api_admin_test.go | 14 ++++++++++++++ modules/convert/user.go | 13 +++++++------ modules/structs/admin_user.go | 1 + modules/structs/user.go | 2 ++ routers/api/v1/admin/user.go | 3 +++ templates/swagger/v1_json.tmpl | 9 +++++++++ 6 files changed, 36 insertions(+), 6 deletions(-) diff --git a/integrations/api_admin_test.go b/integrations/api_admin_test.go index 79fdc4a9f70..fdcfc407892 100644 --- a/integrations/api_admin_test.go +++ b/integrations/api_admin_test.go @@ -192,4 +192,18 @@ func TestAPIEditUser(t *testing.T) { errMap := make(map[string]interface{}) json.Unmarshal(resp.Body.Bytes(), &errMap) assert.EqualValues(t, "email is not allowed to be empty string", errMap["message"].(string)) + + user2 := models.AssertExistsAndLoadBean(t, &models.User{LoginName: "user2"}).(*models.User) + assert.Equal(t, false, user2.IsRestricted) + bTrue := true + req = NewRequestWithJSON(t, "PATCH", urlStr, api.EditUserOption{ + // required + LoginName: "user2", + SourceID: 0, + // to change + Restricted: &bTrue, + }) + session.MakeRequest(t, req, http.StatusOK) + user2 = models.AssertExistsAndLoadBean(t, &models.User{LoginName: "user2"}).(*models.User) + assert.Equal(t, true, user2.IsRestricted) } diff --git a/modules/convert/user.go b/modules/convert/user.go index 010c92b969b..f5d853fd4d1 100644 --- a/modules/convert/user.go +++ b/modules/convert/user.go @@ -17,12 +17,13 @@ func ToUser(user *models.User, signed, authed bool) *api.User { return nil } result := &api.User{ - ID: user.ID, - UserName: user.Name, - FullName: markup.Sanitize(user.FullName), - Email: user.GetEmail(), - AvatarURL: user.AvatarLink(), - Created: user.CreatedUnix.AsTime(), + ID: user.ID, + UserName: user.Name, + FullName: markup.Sanitize(user.FullName), + Email: user.GetEmail(), + AvatarURL: user.AvatarLink(), + Created: user.CreatedUnix.AsTime(), + Restricted: user.IsRestricted, } // hide primary email if API caller is anonymous or user keep email private if signed && (!user.KeepEmailPrivate || authed) { diff --git a/modules/structs/admin_user.go b/modules/structs/admin_user.go index 8cd4dc59e86..ee1738579ab 100644 --- a/modules/structs/admin_user.go +++ b/modules/structs/admin_user.go @@ -41,4 +41,5 @@ type EditUserOption struct { MaxRepoCreation *int `json:"max_repo_creation"` ProhibitLogin *bool `json:"prohibit_login"` AllowCreateOrganization *bool `json:"allow_create_organization"` + Restricted *bool `json:"restricted"` } diff --git a/modules/structs/user.go b/modules/structs/user.go index bf52cc9ed65..511e4c56ce2 100644 --- a/modules/structs/user.go +++ b/modules/structs/user.go @@ -30,6 +30,8 @@ type User struct { LastLogin time.Time `json:"last_login,omitempty"` // swagger:strfmt date-time Created time.Time `json:"created,omitempty"` + // Is user restricted + Restricted bool `json:"restricted"` } // MarshalJSON implements the json.Marshaler interface for User, adding field(s) for backward compatibility diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go index f148710c797..116c622048d 100644 --- a/routers/api/v1/admin/user.go +++ b/routers/api/v1/admin/user.go @@ -224,6 +224,9 @@ func EditUser(ctx *context.APIContext) { if form.ProhibitLogin != nil { u.ProhibitLogin = *form.ProhibitLogin } + if form.Restricted != nil { + u.IsRestricted = *form.Restricted + } if err := models.UpdateUser(u); err != nil { if models.IsErrEmailAlreadyUsed(err) || models.IsErrEmailInvalid(err) { diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 2dedb56d1ec..71caee9c75e 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -13796,6 +13796,10 @@ "type": "boolean", "x-go-name": "ProhibitLogin" }, + "restricted": { + "type": "boolean", + "x-go-name": "Restricted" + }, "source_id": { "type": "integer", "format": "int64", @@ -16209,6 +16213,11 @@ "description": "the user's username", "type": "string", "x-go-name": "UserName" + }, + "restricted": { + "description": "Is user restricted", + "type": "boolean", + "x-go-name": "Restricted" } }, "x-go-package": "code.gitea.io/gitea/modules/structs"