Skip to content

Commit

Permalink
Merge pull request #99 from ctreminiom/feature/content-restrictions
Browse files Browse the repository at this point in the history
✨ Added the Confluence Restriction Service
  • Loading branch information
ctreminiom authored Jan 19, 2022
2 parents 441f021 + 2f787c0 commit aa74abb
Show file tree
Hide file tree
Showing 16 changed files with 2,437 additions and 15 deletions.
8 changes: 8 additions & 0 deletions confluence/confluence.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func New(httpClient *http.Client, site string) (client *Client, err error) {
Permission: &ContentPermissionService{client: client},
Label: &ContentLabelService{client: client},
Property: &ContentPropertyService{client: client},
Restriction: &ContentRestrictionService{
client: client,
Operation: &ContentRestrictionOperationService{
client: client,
Group: &ContentRestrictionOperationGroupService{client: client},
}},
}

client.Space = &SpaceService{client: client}
Expand Down Expand Up @@ -128,6 +134,8 @@ func transformTheHTTPResponse(response *http.Response, structure interface{}) (r
}

responseTransformed.API = &apiError
responseTransformed.Bytes.Write(responseAsBytes)

return responseTransformed, fmt.Errorf(requestFailedError, response.StatusCode)
}
return responseTransformed, fmt.Errorf(requestFailedError, response.StatusCode)
Expand Down
1 change: 1 addition & 0 deletions confluence/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ContentService struct {
Permission *ContentPermissionService
Label *ContentLabelService
Property *ContentPropertyService
Restriction *ContentRestrictionService
}

// Gets returns all content in a Confluence instance.
Expand Down
170 changes: 170 additions & 0 deletions confluence/contentRestriction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package confluence

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

type ContentRestrictionService struct {
client *Client
Operation *ContentRestrictionOperationService
}

// Gets returns the restrictions on a piece of content.
// Docs: https://docs.go-atlassian.io/confluence-cloud/content/restrictions#get-restrictions
func (c *ContentRestrictionService) Gets(ctx context.Context, contentID string, expand []string, startAt, maxResults int) (
result *models.ContentRestrictionPageScheme, response *ResponseScheme, err error) {

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

query := url.Values{}
query.Add("start", strconv.Itoa(startAt))
query.Add("limit", strconv.Itoa(maxResults))

if len(expand) != 0 {
query.Add("expand", strings.Join(expand, ","))
}

endpoint := fmt.Sprintf("wiki/rest/api/content/%v/restriction?%v", contentID, query.Encode())

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

request.Header.Set("Accept", "application/json")

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

return
}

// Add adds restrictions to a piece of content. Note, this does not change any existing restrictions on the content.
// Docs: https://docs.go-atlassian.io/confluence-cloud/content/restrictions#add-restrictions
func (c *ContentRestrictionService) Add(ctx context.Context, contentID string, payload *models.ContentRestrictionUpdatePayloadScheme,
expand []string) (result *models.ContentRestrictionPageScheme, response *ResponseScheme, err error) {

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

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

query := url.Values{}
if len(expand) != 0 {
query.Add("expand", strings.Join(expand, ","))
}

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

payloadAsReader, err := transformStructToReader(payload)
if err != nil {
return nil, nil, err
}

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

request.Header.Set("Accept", "application/json")
request.Header.Set("Content-Type", "application/json")

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

return
}

// Delete removes all restrictions (read and update) on a piece of content.
// Docs: https://docs.go-atlassian.io/confluence-cloud/content/restrictions#delete-restrictions
func (c *ContentRestrictionService) Delete(ctx context.Context, contentID string, expand []string) (
result *models.ContentRestrictionPageScheme, response *ResponseScheme, err error) {

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

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

query := url.Values{}
if len(expand) != 0 {
query.Add("expand", strings.Join(expand, ","))
}

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, nil, err
}

request.Header.Set("Accept", "application/json")

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

return
}

// Update updates restrictions for a piece of content. This removes the existing restrictions and replaces them with the restrictions in the request.
// Docs: https://docs.go-atlassian.io/confluence-cloud/content/restrictions#update-restrictions
func (c *ContentRestrictionService) Update(ctx context.Context, contentID string, payload *models.ContentRestrictionUpdatePayloadScheme,
expand []string) (result *models.ContentRestrictionPageScheme, response *ResponseScheme, err error) {

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

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

query := url.Values{}
if len(expand) != 0 {
query.Add("expand", strings.Join(expand, ","))
}

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

payloadAsReader, err := transformStructToReader(payload)
if err != nil {
return nil, nil, err
}

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

request.Header.Set("Accept", "application/json")
request.Header.Set("Content-Type", "application/json")

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

return
}
140 changes: 140 additions & 0 deletions confluence/contentRestrictionOperationGroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package confluence

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

type ContentRestrictionOperationGroupService struct{ client *Client }

// Get returns whether the specified content restriction applies to a group
// Note that a response of true does not guarantee that the group can view the page,
// as it does not account for account-inherited restrictions, space permissions, or even product access.
// Docs: https://docs.go-atlassian.io/confluence-cloud/content/restrictions/operations/group#get-content-restriction-status-for-group
func (c *ContentRestrictionOperationGroupService) Get(ctx context.Context, contentID, operationKey, groupNameOrID string) (response *ResponseScheme, err error) {

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

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

if len(groupNameOrID) == 0 {
return nil, models.ErrNoConfluenceGroupError
}

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

// check if the group id is an uuid type
// if so, it's the group id
groupID, err := uuid.Parse(groupNameOrID)

if err == nil {
endpoint.WriteString(fmt.Sprintf("byGroupId/%v", groupID.String()))
} else {
endpoint.WriteString(fmt.Sprintf("group/%v", groupNameOrID))
}

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 group to a content restriction. That is, grant read or update permission to the group for a piece of content.
// Docs: https://docs.go-atlassian.io/confluence-cloud/content/restrictions/operations/group#add-group-to-content-restriction
func (c *ContentRestrictionOperationGroupService) Add(ctx context.Context, contentID, operationKey, groupNameOrID string) (response *ResponseScheme, err error) {

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

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

if len(groupNameOrID) == 0 {
return nil, models.ErrNoConfluenceGroupError
}

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

// check if the group id is an uuid type
// if so, it's the group id
groupID, err := uuid.Parse(groupNameOrID)

if err == nil {
endpoint.WriteString(fmt.Sprintf("byGroupId/%v", groupID.String()))
} else {
endpoint.WriteString(fmt.Sprintf("group/%v", groupNameOrID))
}

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/group#remove-group-from-content-restriction
func (c *ContentRestrictionOperationGroupService) Remove(ctx context.Context, contentID, operationKey, groupNameOrID string) (response *ResponseScheme, err error) {

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

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

if len(groupNameOrID) == 0 {
return nil, models.ErrNoConfluenceGroupError
}

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

// check if the group id is an uuid type
// if so, it's the group id
groupID, err := uuid.Parse(groupNameOrID)

if err == nil {
endpoint.WriteString(fmt.Sprintf("byGroupId/%v", groupID.String()))
} else {
endpoint.WriteString(fmt.Sprintf("group/%v", groupNameOrID))
}

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 aa74abb

Please sign in to comment.