Remove a user's space permission via the API [Confluence]

Lionel Hutz
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 20, 2012

Is there a way to remove a user's space permission via the Confluence API? I'd like my plugin to be able to "clean up" a user from a space. I've checked the PermissionManager and SpacePermissionManager but neither seems to do what I want.

Here's my initial code:

for(String perm : SpacePermission.GENERIC_SPACE_PERMISSIONS){
if(spacePermissionManager.hasPermission(perm, airSpace, user)){
//remove perm
}
}

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Deleted user June 10, 2012

maybe that helps:

Space space = spaceManager.getSpace("somespace");            
            SpacePermission spacePermission = SpacePermission.createUserSpacePermission(SpacePermission.VIEWSPACE_PERMISSION, space, "username");
            spacePermissionManager.removePermission(spacePermission);

Cheers,

Lars

Lionel Hutz
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 14, 2012

That looks to be a good way to do this, thanks!

Ibrahiem Mohammad November 28, 2018

@[deleted] Could you help me understand how to use spaceManager? I'm trying to use as following but keep getting a null pointer exception error which is essentially saying that my space is null:


import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import com.atlassian.confluence.spaces.*;
import com.atlassian.confluence.security.*;
import com.atlassian.user.User;
import com.atlassian.user.UserManager;
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
import com.atlassian.confluence.user.ConfluenceUser;
import com.atlassian.confluence.user.UserAccessor;
// import com.atlassian.confluence.it.space.SpacePermissions;

import java.util.ArrayList;
import java.util.List;

@XmlRootElement(name = "link")
@XmlAccessorType(XmlAccessType.FIELD)
public class SpacePermissionsModel {

private SpaceManager spaceManager;
private Space space;
private String spaceName;
private String spaceURL;
private String spaceDisplayName;
private List<User> spaceAdmins;

private boolean error;

public SpacePermissionsModel() {}

public SpacePermissionsModel(String spaceKey) {
super();
this.space = this.spaceManager.getSpace(spaceKey);
}


}
0 votes
Michael Weilbächer August 24, 2016

I'm pushing this since the API which was used in @Lars Kleen's example is deprecated in Versions 5.9 and up. Any ideas on how to circumvent using the deprecated 'removePermission()' ?

spacePermissionManager.removePermission(spacePermission);
Hua Soon SIM _Akeles_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
March 4, 2018

I tried using the SpacePermissionManagerInternal in my plugin but cannot inject it successfully.

It seemed that the recommendation is to use com.atlassian.confluence.internal.security.SpacePermissionManagerInternal class

Deprecated. since 5.9. Use SpacePermissionManagerInternal.removePermission(SpacePermission, com.atlassian.confluence.internal.security.SpacePermissionContext) instead.

 

but according to the javadoc, the internal interfaces are not exposed the plugins

Internal version of the SpacePermissionManager; see package-info.java for rationale.

which states

/**
 * Internal interfaces that are NOT exposed to the plugin classpath, by being excluded in the packageScanningConfiguration
 * of bootstrapContext.xml.
 * <p>
 * This allows us to slowly reduce the surface area of confluence-core as an API, now that we are intentionally
 * exposing an official API in confluence-java-api.
 * <p>
 * When adding new internal interface methods that return multiple entities, they <em>must</em> accept a
 * {@link com.atlassian.confluence.api.model.pagination.LimitedRequest} as an argument.
 *
 * @since 5.5
 */
Like Miroslav Kralik likes this
Miroslav Kralik November 24, 2018

Any findings? 

Hua Soon SIM _Akeles_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
November 27, 2018

Nope :(

Gianluca Cristiano April 2, 2019

I tried using the SpacePermissionManagerInternal in my plugin but cannot inject it successfully.

Can you help me, I need to add or remove permissions for the spaces?

 

Thanks

Duc-Hung_Le August 27, 2019

it is interesting as the SpacePermissionManagerInternal is not exposed, but the SpacePermissionManager is deprecated and suggest SpacePermissionManagerInternal.

 

I tried but cannot inject SpacePermissionManagerInternal too. I am using SpacePermissionManager and it works.

Like # people like this
0 votes
Martin Mitry May 20, 2012

Hi Lionel,

you can try this:

for(String perm : SpacePermission.GENERIC_SPACE_PERMISSIONS){
	if(spacePermissionManager.hasPermission(perm, airSpace, user)){
		spacePermissionManager.removeAllUserPermissions(user.getName());
	}
}

Which Confluence version are you using?

Take a look at:
https://developer.atlassian.com/display/CONFDEV/Confluence+Permissions+Architecture

Cheers
Martin

Lionel Hutz
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 20, 2012

I was thinking about that, but removeAllUserPermissions doesn't take a space so I assume it whipes out ALL of a user's permissions? I just want to remove them from one space.

We're on Confluence 3.5.

0 votes
Martin Mitry May 20, 2012

which

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events