How can I show the Page Permissions in the page itself? (Confluence)
Asked by Steve Goldberg:

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?


Answers:
author: Sandro Herrmann [Communardo]
body: ?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&lt;ul&gt; ##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            &lt;li&gt;$contentPermission.userName&lt;/li&gt;        #end     #end #end&lt;/ul&gt;Users with edit Permission&lt;ul&gt; ##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            &lt;li&gt;$contentPermission.userName&lt;/li&gt;        #end     #end #end&lt;/ul&gt;Groups with view Permission&lt;ul&gt; ##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            &lt;li&gt;$contentPermission.groupName&lt;/li&gt;        #end     #end #end&lt;/ul&gt;Groups with edit Permission&lt;ul&gt; ##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            &lt;li&gt;$contentPermission.groupName&lt;/li&gt;        #end     #end #end&lt;/ul&gt;
```

comments: author: Steve Goldberg
body: 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!


author: Steve Goldberg
body: OK, so I'm very grateful for this macro.

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


author: Sandro Herrmann [Communardo]
body: Oh yes it is.... simply add the following lines below the existing code

```
Space Group Permissions&lt;ul&gt;##iterate over all space permissions#foreach( $spacePermission in $content.space.permissions)                 #if($spacePermission.isGroupPermission() )                          ##if yes print the name            &lt;li&gt;$spacePermission.group ($spacePermission.type) &lt;/li&gt;        #end       #end&lt;/ul&gt;Space User Permissions&lt;ul&gt;##iterate over all space permissions#foreach( $spacePermission in $content.space.permissions)                 #if($spacePermission.isUserPermission() )                              ##if yes print the name                &lt;li&gt;$spacePermission.userName ($spacePermission.type) &lt;/li&gt;           #end       #end&lt;/ul&gt;
```

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


author: Steve Goldberg
body: 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.


author: Sandro Herrmann [Communardo]
body: Simply add an additioinal condition to the if statement for the VIEW Type

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


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


author: David Yu
body: 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](https://jira.atlassian.com/browse/CONF-8799).


author: Steve Goldberg
body: 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 = YESelse	$var = NOif pagepermissions = NULL	$var = YESelseif pagepermissions contain group:client	$var = YESelse	$ var = NOend
```

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


author: Chris Textor
body: Hi Sandro,

I like the code a lot.

Is there a way to show the user avatar instead of the userName?

In your code:

##if yes print the name  
\<li\>$contentPermission.userName\</li\>

Thanks you in advance


author: Sandro Herrmann [Communardo]
body: You can do this whith a little user macro. please see my example code from here:

<https://answers.atlassian.com/questions/3895/how-to-create-a-list-people-who-can-view-a-current-page>

comments: author: Steve Goldberg
body: 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.


Tags: permission

restriction