Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Step 2: Using the script to bulk update add/delete restrictions

Note

As this is a bulk operation, please ensure that you have configured the script with the right information before proceeding.

Prerequisites

...

Code Block
import csv
import requests
import json
import base64
# Replace with your Confluence Cloud domain, email, API token, and CSV file
CONFLUENCE_DOMAIN = '<instance-name>.atlassian.net'
EMAIL = '<EMAIL_ADDRESS>'
API_TOKEN = '<API_TOKEN>'
CSV_FILE = '<CSV_FILE>'
AUTH_STRING = EMAIL + ':' + API_TOKEN
BASIC_AUTH_TOKEN = base64.b64encode(AUTH_STRING.encode("ascii")).decode("ascii")
headers = {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'Authorization': f'Basic {BASIC_AUTH_TOKEN}'
}
MAX_RETRY = 5
def update_space_permissions(permission, owner_type, owner_id, space_key, retries = 0):
    if retries >= MAX_RETRY:
        print(f'Reached maximum recursion depth. Exiting recursive calls.')
        return
    url = f'https://{CONFLUENCE_DOMAIN}/wiki/rest/api/space/{space_key}/permission'
    data = {
        'operation': {
            'key': permission,
            'target': 'space'
        },
        'subject': {
            'type': owner_type,
            'identifier': owner_id
        }
    }
    response = requests.post(url, headers=headers, json=data)
    if response.status_code == 200:
        print(f'Updated permissions for {owner_id}: {permission} in space {space_key}')
    else:
        print(f'Failed to update permissions for {owner_id}: {permission} in space {space_key}. Error: {response.text}')
        if 'read space' in response.text:
            update_space_permissions('read', owner_type, owner_id, space_key, retries + 1)
            update_space_permissions(permission, owner_type, owner_id, space_key, retries + 1)
def main():
    with open(CSV_FILE, newline='') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            owner = row['Owner']
            owner_type = row['Type'].lower()
            owner_id = row['OwnerId']
            space_key = row['SpaceKey']
            update_space_permissions('restrict_content', owner_type, owner_id, space_key)
if __name__ == '__main__':
    main()

Anchor
configurescript
configurescript
Step 3: Configuring the script

Replace

  • <instance-name>.atlassian.net with your Confluence domain

  • <EMAIL_ADDRESS> with the email associated with your Confluence domain

...