How can I show the Page Permissions in the page itself? (Confluence)

On the info page for a page it prints out a small box showing the page permissions for that page. We would really like to have this data on the page itself. Presumably there is some code to generate this information and I can't figure out what it might be.

Does anybody know?

2 answers

1 accepted

Äehm let's extend the Macro

## @noparams
 
#set($containerManagerClass=$action.class.forName('com.atlassian.spring.container.ContainerManager'))
#set($getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null))
#set($containerManager=$getInstanceMethod.invoke(null,null))
#set($containerContext=$containerManager.containerContext)
 
## the contentPermissionManager gives you access to the page permissions
#set($contentPermissionManager=$containerContext.getComponent('contentPermissionManager')) 
 
 
Users with view Permission
<ul>
 
##iterate over all view contentpermissionssets 
#foreach( $contentPermissionSet in $contentPermissionManager.getContentPermissionSets($content,"View") )
     
    #foreach( $contentPermission in $contentPermissionSet )   
         
        ##is the current permission a user permission
        #if($contentPermission.isUserPermission() )
             
            ##if yes print the name
            <li>$contentPermission.userName</li>
        #end
 
    #end
 
#end
</ul>

Users with edit Permission
<ul>
 
##iterate over all view contentpermissionssets 
#foreach( $contentPermissionSet in $contentPermissionManager.getContentPermissionSets($content,"Edit") )
     
    #foreach( $contentPermission in $contentPermissionSet )   
         
        ##is the current permission a user permission
        #if($contentPermission.isUserPermission() )
             
            ##if yes print the name
            <li>$contentPermission.userName</li>
        #end
 
    #end
 
#end
</ul>



Groups with view Permission
<ul>
 
##iterate over all view contentpermissionssets 
#foreach( $contentPermissionSet in $contentPermissionManager.getContentPermissionSets($content,"View") )
     
    #foreach( $contentPermission in $contentPermissionSet )   
         
        ##is the current permission a user permission
        #if($contentPermission.isGroupPermission() )
             
            ##if yes print the name
            <li>$contentPermission.groupName</li>
        #end
 
    #end
 
#end
</ul>

Groups with edit Permission
<ul>
 
##iterate over all view contentpermissionssets 
#foreach( $contentPermissionSet in $contentPermissionManager.getContentPermissionSets($content,"Edit") )
     
    #foreach( $contentPermission in $contentPermissionSet )   
         
        ##is the current permission a user permission
        #if($contentPermission.isGroupPermission() )
             
            ##if yes print the name
            <li>$contentPermission.groupName</li>
        #end
 
    #end
 
#end
</ul>

Here we go! Yes, that's great - the formatting is a little off but I think I can handle that - as long as I get the information then I can make it look however I want.

Thanks for your effort!

OK, so I'm very grateful for this macro.

Now - it is possible to adapt this to also include space's permissions too?

Oh yes it is.... simply add the following lines below the existing code

Space Group Permissions
<ul>
##iterate over all space permissions
#foreach( $spacePermission in $content.space.permissions)
     
            #if($spacePermission.isGroupPermission() )
              
            ##if yes print the name
            <li>$spacePermission.group ($spacePermission.type) </li>
        #end 
    
  
#end
</ul>

Space User Permissions
<ul>
##iterate over all space permissions
#foreach( $spacePermission in $content.space.permissions)
     
            #if($spacePermission.isUserPermission() )
              
                ##if yes print the name
                <li>$spacePermission.userName ($spacePermission.type) </li>
           #end 
    
  
#end
</ul>

Enjoy and rate it :) . Testet whith my CF 4

Like 1 person likes this

I know I'm being picky here but is it possible to have the macro just print out the "View" permission for the space? I spent about 30 mins tinkering with it myself but to no avail.

Many thanks for your continued help.

Simply add an additioinal condition to the if statement for the VIEW Type

Space Group Permissions
<ul>
##iterate over all space permissions
#foreach( $spacePermission in $content.space.permissions)
      
            #if($spacePermission.isGroupPermission() && $spacePermission.type == "VIEWSPACE" )
               
            ##if yes print the name
            <li>$spacePermission.group ($spacePermission.type) </li>
        #end 
     
   
#end
</ul>
 
Space User Permissions
<ul>
##iterate over all space permissions
#foreach( $spacePermission in $content.space.permissions)
      
            #if($spacePermission.isUserPermission()  && $spacePermission.type == "VIEWSPACE")
               
                ##if yes print the name
                <li>$spacePermission.userName ($spacePermission.type) </li>
           #end 
     
   
#end
</ul>

Like 1 person likes this

Thank you! I had a similar use case. I've linked your macro in the JIRA issue https://jira.atlassian.com/browse/CONF-21661

Thanks Sandro. Works for almost all cases. One situation where this doesn't work is when a parent page has more restrictions than a child page.

For example, Bob has view restrictions on Parent, and Amy has view restrictions on Child.

The macro will show Amy having View permissions on Child but if she attempts to view it, she will be denied.

Maybe this won't be possible until there's an ability to check for effective permissions.

Hi Sandro / everyone,

Is it possible to write some velocity to check if a *page* has no permissions set? I've written some code which checks if a *space* has a certain permission group set to it, but I want to be able to check if a page within that space has that group ALSO applied.

E.g.,:

if spacepermissions contain group:client
	$var = YES
else
	$var = NO


if pagepermissions = NULL
	$var = YES
elseif pagepermissions contain group:client
	$var = YES
else
	$ var = NO
end

I can do the first bit fine. It's the check to see if the page permissions are non-existent that I can't do. Any help to help me further this?

Cheers

Hi Sandro,

Thanks for your answer but that doesn't achieve what I want. Your macro only shows the individual users assigned to the page. It doesn't show the user groups. Seeing as most of our permissions are set on a group-basis, this macro doesn't help me.

Thanks anyway.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in New to Confluence

How to use Confluence Cloud for stakeholder management

Most of us don’t need much convincing that stakeholder management is important. It just makes sense that keeping everyone in-the-know on projects and assigning clearly defined roles is key to having ...

1,146 views 4 6
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you