From de34275bb64efe8baccdd54ee1a337202d4364ce Mon Sep 17 00:00:00 2001 From: lafriks Date: Wed, 20 Jul 2022 10:57:09 +0800 Subject: [PATCH] Add method to get user organization permissions (#594) Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/594 Reviewed-by: Lunny Xiao Reviewed-by: 6543 <6543@obermui.de> Co-authored-by: lafriks Co-committed-by: lafriks --- gitea/org_member.go | 23 +++++++++++++++++++++++ gitea/org_member_test.go | 5 +++++ 2 files changed, 28 insertions(+) diff --git a/gitea/org_member.go b/gitea/org_member.go index 1eed90f..79dad4e 100644 --- a/gitea/org_member.go +++ b/gitea/org_member.go @@ -117,3 +117,26 @@ func (c *Client) SetPublicOrgMembership(org, user string, visible bool) (*Respon return resp, fmt.Errorf("unexpected Status: %d", status) } } + +// OrgPermissions represents the permissions for an user in an organization +type OrgPermissions struct { + CanCreateRepository bool `json:"can_create_repository"` + CanRead bool `json:"can_read"` + CanWrite bool `json:"can_write"` + IsAdmin bool `json:"is_admin"` + IsOwner bool `json:"is_owner"` +} + +// GetOrgPermissions returns user permissions for specific organization. +func (c *Client) GetOrgPermissions(org, user string) (*OrgPermissions, *Response, error) { + if err := escapeValidatePathSegments(&org, &user); err != nil { + return nil, nil, err + } + + perm := &OrgPermissions{} + resp, err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s/orgs/%s/permissions", user, org), jsonHeader, nil, &perm) + if err != nil { + return nil, resp, err + } + return perm, resp, nil +} diff --git a/gitea/org_member_test.go b/gitea/org_member_test.go index 8a101ab..e017e1d 100644 --- a/gitea/org_member_test.go +++ b/gitea/org_member_test.go @@ -35,6 +35,11 @@ func TestOrgMembership(t *testing.T) { assert.NoError(t, err) assert.True(t, check) + perm, _, err := c.GetOrgPermissions(newOrg.UserName, user.UserName) + assert.NoError(t, err) + assert.NotNil(t, perm) + assert.True(t, perm.IsOwner) + _, err = c.SetPublicOrgMembership(newOrg.UserName, user.UserName, true) assert.NoError(t, err) check, _, err = c.CheckPublicOrgMembership(newOrg.UserName, user.UserName)