Remove all Content Permissions of a page [JAVA] [Confluence] [5.8.4]

Fabian Angst June 30, 2015

Hey guys,

I'm really confused. I allready had a question with a simular question (https://answers.atlassian.com/questions/18810508).
I want to restrict a page to some specific users with my plugin - which is no problem now, but I get nullpointers if I want to remove all restrictions to restrict the page to other users or open in to all which has the space permission.

If you know how to remove this please help me! smile

the code:

public void removePageRestriciton(Page page) {
        try {
            List<ContentPermission> toDeleteEdit = new ArrayList<ContentPermission>();
            List<ContentPermission> toDeleteView = new ArrayList<ContentPermission>();
            if (conPerMan.getContentPermissionSets(page,
                    ContentPermission.EDIT_PERMISSION) != null) {
                for (ContentPermission cp : page
                        .getContentPermissionSet(ContentPermission.EDIT_PERMISSION)) {
                    toDeleteEdit.add(cp);
                }
                for (ContentPermission cp : toDeleteEdit) {
                    conPerMan.removeContentPermission(cp);
                }
            }
            if (conPerMan.getContentPermissionSets(page,
                    ContentPermission.VIEW_PERMISSION) != null) {
                for (ContentPermission cp : page
                        .getContentPermissionSet(ContentPermission.VIEW_PERMISSION)) {
                    toDeleteView.add(cp);
                }
                for (ContentPermission cp : toDeleteView) {
                    conPerMan.removeContentPermission(cp);
                }
            }
            System.out.println("RESTRICTION REMOVED: " + page.getId());
        } catch (Exception e) {
            System.out.println("CATCH REMOVE RESTRICTION");
            e.printStackTrace();
        }
    }

the error:

[com.atlassian.hibernate.HibernateObjectDao] remove remove error!
org.springframework.orm.hibernate.HibernateSystemException: not-null 
property references a null or transient value: 
com.atlassian.confluence.security.ContentPermission.owningSet; nested 
exception is net.sf.hibernate.PropertyValueException: not-null property 
references a null or transient value: 
com.atlassian.confluence.security.ContentPermission.owningSet

 

 

It would be great if u would or could help me! smile

Best regards from Germany!

5 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
xpauls
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 30, 2015

Maybe try not to access ContentPermisionSets directly from page. Use contentPermissionManager instead.

Try this and see if it works

List<ContentPermissionSet> permSetList = contentPermissionManager.getContentPermissionSets(page, ContentPermission.VIEW_PERMISSION);
                for (ContentPermissionSet permSet : permSetList) {
                    List<ContentPermission> toRemove = new ArrayList<ContentPermission>();
                    for (ContentPermission perm : permSet)
                        toRemove.add(perm);
                    for (ContentPermission perm : toRemove)
                        contentPermissionManager.removeContentPermission(perm);
                }
1 vote
masopust August 13, 2015

I was facing the same issue earlier today when trying to reset all view permissions on a page, and setting the permissions based on a dialog I injected on the editor save button. I tried different approaches which led me to either the not-null-property exception thrown by Hibernate, or a java.util.ConcurrentModificationException when removing a permission via ContentPermissionManagers removePermission() method while iterating through a ContentPermissionSet.

I finally ended up managing to reset the permissions on a two-step approach:

  1. get all current permissions on the page and add them to a list
  2. iterate through that list and remove each permission

 

List<ContentPermissionSet> setList = contentPermissionManager.getContentPermissionSets(pageToUpdate, ContentPermission.VIEW_PERMISSION);


List<ContentPermission> toDelete = new LinkedList<ContentPermission>();
    	
log.debug("total sets: " + setList.size());
for( ContentPermissionSet set: setList ){
	log.debug("in set: " + set + ": " + set.size());
	for( ContentPermission perm: set ){
		log.debug("perm: " + perm);
		toDelete.add(perm);
	}
}
	
log.debug("removing permissions...");
for(ContentPermission perm: toDelete ){
	log.debug("trying to remove: " + perm);
	contentPermissionManager.removeContentPermission(perm);
	log.debug("removed.");
}
 
//checking to see if all permissions have been removed properly
setList = contentPermissionManager.getContentPermissionSets(pageToUpdate, ContentPermission.VIEW_PERMISSION);
log.debug("total sets: " + setList.size());

Note that

  • this only checks on view permissions
  • for logging purposes, I haven't really bothered to check for null values - or haven't followed the guidelines at all

 

I haven't encountered an exception so far, so this seems to work.

 

0 votes
Fabian Angst July 9, 2015

Its still the code above - but i also get a not-null-property exception if it code this:

public void removePageRestriciton(Page page) {
        try {
            ContentPermissionSet viewPerms = page
                    .getContentPermissionSet(ContentPermission.VIEW_PERMISSION);
            ContentPermissionSet editPerms = page
                    .getContentPermissionSet(ContentPermission.EDIT_PERMISSION);
            page.removeContentPermissionSet(viewPerms);
            page.removeContentPermissionSet(editPerms);
        } catch (Exception e) {
            System.out.println("CATCH REMOVE RESTRICTION");
            log.error(e.getMessage(), e);
        }
    }
0 votes
Fabian Angst July 1, 2015

@Pauls Žunda, thank you - it seems like I am to stupid to implement sth like that... laugh

Still got some errors...:

org.springframework.orm.hibernate.HibernateSystemException: could not 
insert: [com.atlassian.confluence.security.ContentPermission#8224769]; 
nested exception is net.sf.hibernate.HibernateException: could not 
insert: [com.atlassian.confluence.security.ContentPermission#8224769]

                    at 
org.springframework.orm.hibernate.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:597)

            

            
            
                caused by: net.sf.hibernate.HibernateException: could 
not insert: 
[com.atlassian.confluence.security.ContentPermission#8224769]

                    at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:475)
            

            
            
                caused by: net.sf.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

                    at net.sf.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:62)

and also:

 

java.lang.RuntimeException: Failed to add entry to queue

 

 

... thanks for your help..

xpauls
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.
July 2, 2015

Hmm, weird, this works for me on 5.7.4 version. Maybe it's some kind of version bug? Also it is weird that it is saying that it "could not insert ContentPermission" since you are trying to remove it. Maybe you are executing along with a code that throws this exception? Like trying to add ContentPermission for user which already has this permission?

Fabian Angst July 7, 2015

Thanks yap thats a problem - its caused by the not-null problem - i cant remove the restrictions... still my problem

xpauls
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.
July 7, 2015

It's hard to tell what is wrong. maybe you can paste your code?

0 votes
Nguyen Dang
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 30, 2015

cc @Edith Tom

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