Is there any API to get spaces which has anonymous access
Asked by Himanshu:

Is there API to get spaces which has anonymous access ?


Answers:
author: SunRiser
body: Hi Himanshu,

You can run the below SQL query to get a list of all spaces that can be accessed by anonymous users:

*SELECT SPACENAME FROM SPACES WHERE SPACEID IN (SELECT SPACEID FROM SPACEPERMISSIONS WHERE PERMTYPE = 'VIEWSPACE' AND PERMGROUPNAME IS NULL AND PERMUSERNAME IS NULL AND PERMALLUSERSSUBJECT IS NULL);*

Regards,

Mahesh

comments: author: Himanshu
body: Hi Mahesh ,

I have same SQL query but we need to get information through API or Curl command

Thanks

Regards ,

Himanshu


author: Himanshu
body: We have created SQL query to get spaces which as anonymous access , but our client don't want to run SQL queries directly in prod system.

So , We are looking for other alternative may be API.could you please suggest a feasible alternative here?


author: SunRiser
body: Can you please let us know how you are planning to share the data with your client? Are you are planning to show the results on a Confluence page or some other way?


author: Himanshu
body: We are planing to share data in excel through email.

The intention here is to trigger a email notification to get spaces which has anonymous access for that we are planing to create Jenkins job to achieve this .

Could you please suggest some feasible way.

Note : SQL query is not a feasible solution as per client requirement


author: SunRiser
body: Hi Himanshu,

Did some research and found the following example which should retrieve the Anonymous user permissions for Space

```
curl -v --data '["SpaceKey", ""]' -u admin:admin -H "Content-Type: application/json" http://localhost:8090/confluence/rpc/json-rpc/confluenceservice-v2/getPermissionsForUser?os_authType=basic\\
```

When I tried it in my local test instance, it returned empty even though I have anonymous access enabled for few spaces. This seems to be because *anonymous* does not have an actual user name, but the method requires two parameters, which are:

1. Space key
2. User Name

I tested the following three possibilities for the user name parameter, but all returned empty results:

1. "" - Empty value (as shown in the example above)
2. "null"
3. "anonymous"

With that in mind, we need to find an alternative way of gathering that information. I was able to find the following method that may be useful:

```
/rpc/json-rpc/confluenceservice-v2/getSpacePermissionSets
```

* `SpacePermissionSet\[\] getSpacePermissionSets(String token, String spaceKey)` - retrieves all permission sets specified for space with given `spaceKey`.

This method returns all permissions for a specific space key. Once you get the response, you can review it to find out if anonymous access is enabled. Here is how we can do that:

1. In my test, I enabled only the VIEW permission for anonymous
2. Inspecting the output of the method above, we can see this:  

   ```
   "type": "VIEWSPACE",

   "spacePermissions": [

   {

   "type": "VIEWSPACE",

   "userName": null,

   "groupName": null

   },
   ```

   <br />

   3. When you see \*userName\* and \*groupName\* set to \*NULL\*, this means that Anonymous access has that permission

Let us know if this option is useful in your use case.

**REST API**

I did some research on the REST API methods and could see that we still don't have the ability to search for space permissions through the API. Here are the feature requests which are under consideration for future implementation:

* <https://jira.atlassian.com/browse/CRA-1501>
* <https://jira.atlassian.com/browse/CRA-358>

Hope this information helps you.

Best Regards,

Mahesh


author: Himanshu
body: Hi Mahesh ,

`getSpacePermissionSets(String token, String spaceKey)`

What we need to pass in "String Token"

Could you please provide some example

Thanks in advance

Regards ,

Himanshu


author: SunRiser
body: Hi Himanshu,

The token is used for authentication: [creating a token](https://confluence.atlassian.com/cloud/api-tokens-938839638.html?_ga=2.181451297.781891690.1560237793-1332295599.1549460855)

It is only valid if your Confluence is running on Cloud. For self-hosted or server versions, you can try the below (please tweak the values)

```
USRNAME="admin"
USRPWD="admin"
SPACEKEY="S1"
CONFBASEURL="http://localhost:8090"
curl --user $USRNAME:$USRPWD -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{ "jsonrpc" : "2.0", "method" : "getSpacePermissionSets", "params" : [ "S1" ] , "id": 7 }' $CONFBASEURL/rpc/json-rpc/confluenceservice-v2?os_authType=basic 2>/dev/null | python -mjson.tool
```

Good luck!

Mahesh


author: Himanshu
body: Hi Mahesh ,

we are running confluence on cloud and we are struggling , Because we don't know how to use above provided URL and after that how to use below provided response

```
"type": "VIEWSPACE",

"spacePermissions": [

{

"type": "VIEWSPACE",

"userName": null,

"groupName": null

},
```

Do you have any sample code ?


author: SunRiser
body: Please refer to link on how to create and use tokens: [Confluence Tokens](https://confluence.atlassian.com/cloud/api-tokens-938839638.html)

In the response, if you see the below then it means that space has anonymous access. You may need to write additional logic to extract the space names from the response.

```
"userName": null,

"groupName": null
```