-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from ctreminiom/feature/content-restrictions
✨ Added the Confluence Restriction Service
- Loading branch information
Showing
16 changed files
with
2,437 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.