]> source.dussan.org Git - gitea.git/commitdiff
[API] Add Restricted Field to User (#14630)
author6543 <6543@obermui.de>
Thu, 18 Feb 2021 08:25:35 +0000 (09:25 +0100)
committerGitHub <noreply@github.com>
Thu, 18 Feb 2021 08:25:35 +0000 (16:25 +0800)
* 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 <art27@cantab.net>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
integrations/api_admin_test.go
modules/convert/user.go
modules/structs/admin_user.go
modules/structs/user.go
routers/api/v1/admin/user.go
templates/swagger/v1_json.tmpl

index 79fdc4a9f701c7024a2a0af8380c9e43db327f0f..fdcfc407892b80596e45bfdc6358a27173b439e8 100644 (file)
@@ -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)
 }
index 010c92b969bb4f38a37b24166d720e30668a04d9..f5d853fd4d10a7041abc1f657df440851d0d4ae8 100644 (file)
@@ -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) {
index 8cd4dc59e861fb49554a79b888fc2f60ec1af29d..ee1738579ab71a45d923447b8d786e9760145f68 100644 (file)
@@ -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"`
 }
index bf52cc9ed65391858c4a1b9cd414ea3f80170590..511e4c56ce24d049eaf420536dc7c347fc160427 100644 (file)
@@ -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
index f148710c797156914a75434e22c5056890010630..116c622048d65f86a5ad90bde017788c9d5278dc 100644 (file)
@@ -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) {
index 2dedb56d1ec2594bb290b19be3be74d1655880ef..71caee9c75e1801e5199b040ee394ccb99358144 100644 (file)
           "type": "boolean",
           "x-go-name": "ProhibitLogin"
         },
+        "restricted": {
+          "type": "boolean",
+          "x-go-name": "Restricted"
+        },
         "source_id": {
           "type": "integer",
           "format": "int64",
           "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"