Is there a Java API to remove a permissions from a Space
Asked by Andreas von Euw:

We've build a custom Confluence Plugin for managing Space permissions.

In Confluence 5.9 we've used com.atlassian.confluence.spaces.Space.removePermission to remove a existing permission from a Space. This does not work anymore with Confluence 6.x

Does somebody know how to implement this in newer Confluence Versions?


Answers:
author: Steven Kling
body: [@Andreas von Euw](/t5/user/viewprofilepage/user-id/510295) did you ever find an answer? The only way I see in the 6.x Java API to get user-related permissions:

```
DefaultSpaceManager.getAllPermissionsForUser(ConfluenceUser user)?
```

...but its protected and not accessible via the API.

I have a list of ConfluenceUser(s) and have to remove them from a specific space's permissions. I see no way of accomplishing this aside from a nuclear option:

```
//Remove all permissions entirely
DefaultSpaceManager.removeAllPermissions(Space space)?
```

...then rebuilding all remaining users based on a predefined role-permission mapping

Forgot to add, if you have the SpacePermission already you can use this:

```
removePermission(SpacePermission permission, SpacePermissionContext context)
```

creating the SpacePermissionContext is straightforward:

```
SpacePermissionContext context =?SpacePermissionContext.builder().build();
```

You can add event-related options to the context if you want to suppress them, etc.

comments: author: Damian Jesionek
body: [@Steven Kling](/t5/user/viewprofilepage/user-id/1601741)which classes removePermission() are you using, since most of them are deprecated in the most recent versions. Also I have issues using Classes from the package com.atlassian.confluence.internal.security (like SpacePermissionContext from you solution).

I suppose they are protected? Is there a way to use them?

Thanks in advance!

EDIT:

I also settled to use removeAllPermissions() which is also quite hacky as you need to prevent the side effects that result from this method call (space attribute of the permissions are set to null and the list is cleared).  
A prettier solution for that would be appreciated I think


author: Thomas Clemens
body: I am stuck @exactly the same spot. Everything I try to use is deprecated or not working. Everything new I try to import is protected. So frustrating to write scheduled Tasks for this.. Software... #annoyedPluginDeveloperwhocantfindthingsheneeds


author: Thomas Clemens
body: My latest fail:

AuthenticatedUserImpersonator {#toc-hId--1353512138}
----------------------------------------------------

fired my problem:

```
SpacesQuery query = SpacesQuery.newQuery().forUser(null).withSpaceType(SpaceType.GLOBAL).withPermission(SpacePermission.VIEWSPACE_PERMISSION).build();
ListBuilder<Space> bereichebuilder = spaceManager.getSpaces(query);

if (bereichebuilder.getAvailableSize() != 0) {
List<Space> bereiche = bereichebuilder.getRange(0, bereichebuilder.getAvailableSize());
for (Space aktspace : bereiche) {
if (aktspace.getKey().equals("FUNFACTORY")) {
List<SpacePermission> spaceperms = new ArrayList<SpacePermission>();
SpacePermission myperm;
myperm = SpacePermission.createAnonymousSpacePermission(SpacePermission.REMOVE_OWN_CONTENT_PERMISSION, aktspace);
spaceperms.add(myperm);
ConfluenceUser aktuser = userAccessor.getUserByName("myadminaccountname");

 AuthenticatedUserImpersonator.REQUEST_AGNOSTIC.asUser(() ->{
    aktspace.setPermissions(spaceperms);
    return null;
}, aktuser);
} //if
} // for
} //if

```

Not Working.. even trying to add a permission @ the moment.


Tags: atl-dev

confluence-server

developer

permission

server

space