Skip to content

Commit

Permalink
Merge pull request #100 from ctreminiom/feature/confluence-restrictio…
Browse files Browse the repository at this point in the history
…n-operation-user

✨ Added the Confluence Restriction Operation User service
  • Loading branch information
ctreminiom authored Jan 19, 2022
2 parents a8bdca5 + 5f445b6 commit c9288b4
Show file tree
Hide file tree
Showing 4 changed files with 621 additions and 0 deletions.
1 change: 1 addition & 0 deletions confluence/confluence.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func New(httpClient *http.Client, site string) (client *Client, err error) {
Operation: &ContentRestrictionOperationService{
client: client,
Group: &ContentRestrictionOperationGroupService{client: client},
User: &ContentRestrictionOperationUserService{client: client},
}},
}

Expand Down
132 changes: 132 additions & 0 deletions confluence/contentRestrictionOperationUser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package confluence

import (
"context"
"fmt"
"github.com/ctreminiom/go-atlassian/pkg/infra/models"
"net/http"
"net/url"
"strings"
)

type ContentRestrictionOperationUserService struct{ client *Client }

// Get returns whether the specified content restriction applies to a user.
// Docs: https://docs.go-atlassian.io/confluence-cloud/content/restrictions/operations/user#get-content-restriction-status-for-user
func (c *ContentRestrictionOperationUserService) Get(ctx context.Context, contentID, operationKey, accountID string) (
response *ResponseScheme, err error) {

if len(contentID) == 0 {
return nil, models.ErrNoContentIDError
}

if len(operationKey) == 0 {
return nil, models.ErrNoContentRestrictionKeyError
}

if len(accountID) == 0 {
return nil, models.ErrNoAccountIDError
}

var endpoint strings.Builder
endpoint.WriteString(fmt.Sprintf("wiki/rest/api/content/%v/restriction/byOperation/%v/user", contentID, operationKey))

query := url.Values{}
query.Add("accountId", accountID)

if query.Encode() != "" {
endpoint.WriteString(fmt.Sprintf("?%v", query.Encode()))
}

request, err := c.client.newRequest(ctx, http.MethodGet, endpoint.String(), nil)
if err != nil {
return nil, err
}

response, err = c.client.Call(request, nil)
if err != nil {
return response, err
}

return
}

// Add adds a user to a content restriction. That is, grant read or update permission to the user for a piece of content.
// Docs: https://docs.go-atlassian.io/confluence-cloud/content/restrictions/operations/user#add-user-to-content-restriction
func (c *ContentRestrictionOperationUserService) Add(ctx context.Context, contentID, operationKey, accountID string) (
response *ResponseScheme, err error) {

if len(contentID) == 0 {
return nil, models.ErrNoContentIDError
}

if len(operationKey) == 0 {
return nil, models.ErrNoContentRestrictionKeyError
}

if len(accountID) == 0 {
return nil, models.ErrNoAccountIDError
}

var endpoint strings.Builder
endpoint.WriteString(fmt.Sprintf("wiki/rest/api/content/%v/restriction/byOperation/%v/user", contentID, operationKey))

query := url.Values{}
query.Add("accountId", accountID)

if query.Encode() != "" {
endpoint.WriteString(fmt.Sprintf("?%v", query.Encode()))
}

request, err := c.client.newRequest(ctx, http.MethodPut, endpoint.String(), nil)
if err != nil {
return nil, err
}

response, err = c.client.Call(request, nil)
if err != nil {
return response, err
}

return
}

// Remove removes a group from a content restriction. That is, remove read or update permission for the group for a piece of content.
// Docs: https://docs.go-atlassian.io/confluence-cloud/content/restrictions/operations/user#remove-user-from-content-restriction
func (c *ContentRestrictionOperationUserService) Remove(ctx context.Context, contentID, operationKey, accountID string) (
response *ResponseScheme, err error) {

if len(contentID) == 0 {
return nil, models.ErrNoContentIDError
}

if len(operationKey) == 0 {
return nil, models.ErrNoContentRestrictionKeyError
}

if len(accountID) == 0 {
return nil, models.ErrNoAccountIDError
}

var endpoint strings.Builder
endpoint.WriteString(fmt.Sprintf("wiki/rest/api/content/%v/restriction/byOperation/%v/user", contentID, operationKey))

query := url.Values{}
query.Add("accountId", accountID)

if query.Encode() != "" {
endpoint.WriteString(fmt.Sprintf("?%v", query.Encode()))
}

request, err := c.client.newRequest(ctx, http.MethodDelete, endpoint.String(), nil)
if err != nil {
return nil, err
}

response, err = c.client.Call(request, nil)
if err != nil {
return response, err
}

return
}
Loading

0 comments on commit c9288b4

Please sign in to comment.