How to add a Site Admin to all restricted spaces in Confluence or read restrictions with a script?

Eduardo Diez December 11, 2024

 

 

Hello Atlassian Community!

I’m looking for a way to automatically add a site admin to all spaces with restrictions in Confluence. My goal is to ensure that site admins have access to all pages, including those that are restricted.

If this is not directly possible, is there a way to use a Java script to:

  1. Read the access restrictions for pages in Confluence (who has access and what restrictions apply).
  2. Determine if a page is restricted or open, and which users/groups have access.
  3. Access only non-restricted pages or those that are open for reading.

Below is an example of code I’m using to check the restrictions of a specific page, but I’d love to know if there’s a more efficient or direct solution for managing access restrictions through the Confluence API.

package local_to_cloud;

import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
import org.json.JSONArray;
import org.json.JSONObject;

public class ConfluenceRestrictedOnePage {

    public static void main(String[] args) {
        // Credentials and configuration
        String username = "mail@mail"; 
        String token = "TOKEN"; 
        String baseUrl = "https://mysite.atlassian.net"; 
        String pageId = "2645229574"; // Page ID to check

        try {
            // Check the page restrictions
            HttpResponse<JsonNode> response = Unirest.get(baseUrl + "/wiki/rest/api/content/" + pageId + "/restriction")
                .basicAuth(username, token)
                .header("Accept", "application/json")
                .asJson();
            System.out.println(response.getBody());

            if (response.getStatus() == 200) {
                // Convert JsonNode to org.json.JSONObject
                org.json.JSONObject jsonResponse = new org.json.JSONObject(response.getBody().toString());
                System.out.println("Page restrictions:");
                processRestrictions(jsonResponse);
            } else {
                System.out.println("Error getting restrictions: Code " + response.getStatus());
            }
        } catch (Exception e) {
            System.out.println("Execution error: " + e.getMessage());
        }
    }

    private static void processRestrictions(org.json.JSONObject jsonResponse) {
        try {
            JSONArray results = jsonResponse.getJSONArray("results");

            for (int i = 0; i < results.length(); i++) {
                JSONObject restriction = results.getJSONObject(i);
                String operation = restriction.getString("operation");
                JSONObject restrictions = restriction.getJSONObject("restrictions");

                // Process user restrictions
                JSONObject userRestrictions = restrictions.getJSONObject("user");
                JSONArray users = userRestrictions.getJSONArray("results");
                System.out.println("Operation: " + operation);
                System.out.println("Users with restriction (" + users.length() + "):");
                for (int j = 0; j < users.length(); j++) {
                    JSONObject user = users.getJSONObject(j);
                    System.out.println("- " + user.getString("displayName") + " (" + user.getString("accountId") + ")");
                }

                // Process group restrictions
                JSONObject groupRestrictions = restrictions.getJSONObject("group");
                JSONArray groups = groupRestrictions.getJSONArray("results");
                System.out.println("Groups with restriction (" + groups.length() + "):");
                for (int j = 0; j < groups.length(); j++) {
                    JSONObject group = groups.getJSONObject(j);
                    System.out.println("- " + group.getString("name"));
                }
            }
        } catch (Exception e) {
            System.out.println("Error processing restrictions: " + e.getMessage());
        }
    }
}

Has anyone had experience adding a site admin to all restricted spaces in Confluence or automating the reading of access restrictions via a script?

Thanks in advance for your help!

1 answer

0 votes
Guess_ Brandi N_
Contributor
December 11, 2024

I know that this is possible, maybe not through API, Im not sure of the specifics. but I know for ours, it scans and adds the admins to every space, and I believe will add admins back if they are removed as well.  I am not the one who wrote this script however, so I am going to ask my coworker to take a look at this question and see if he can help. 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events