Confluence macro for getting the restricted pages for any confluence space?
Asked by Tsol:

Hi everybody,

i'm trying to create a macro that will return the restricted pages for every space. I have create the following template for the macro

```
## Macro title: Restricted Pages## Macro has a body: Y or N (N)## Body processing: Selected body processing option## Output: Selected output option## Macro to list all restricted pages per space.## @noparams #set ( $spaces = $spaceManager.getAllSpaces() )#set($contentPermissionManager=$containerContext.getComponent('contentPermissionManager')) &lt;table class="confluenceTable"&gt;  &lt;tr&gt;    &lt;th class="confluenceTh"&gt;Space&lt;/th&gt;    &lt;th class="confluenceTh"&gt;Key&lt;/th&gt;	&lt;th class="highlight-red" data-highlight-colour="red"&gt;View Page Restricted to Users&lt;/th&gt;    &lt;th class="highlight-red" data-highlight-colour="red"&gt;View Page Restricted to Groups&lt;/th&gt;	&lt;th class="highlight-green" data-highlight-colour="green"&gt;Edit Page Restricted to Users&lt;/th&gt;    &lt;th class="highlight-green" data-highlight-colour="green"&gt;Edit Page Restricted to Groups&lt;/th&gt;  &lt;/tr&gt; #foreach ( $space in $spaces )  #if ($space.isGlobal())  &lt;tr&gt;    #set($result=$space.getDescription().getDescriptionTitle())     &lt;td class="confluenceTd"&gt;&lt;a href="$req.contextPath$space.getUrlPath()"&gt;$space.getName()&lt;/a&gt;&lt;/td&gt;    &lt;td class="confluenceTd"&gt;&lt;a href="$req.contextPath$space.getUrlPath()"&gt;$space.getKey()&lt;/a&gt;&lt;/td&gt;    &lt;td class="highlight-red" data-highlight-colour="red"&gt;				##In this cell i want to have the pages of space with view restriction to users				    &lt;/td&gt;    &lt;td class="highlight-red" data-highlight-colour="red"&gt;    	##In this cell i want to have the pages of space with view restriction to groups	    &lt;/td&gt;	&lt;td class="highlight-green" data-highlight-colour="green"&gt;			##In this cell i want to have the pages of space with edit restriction to users		    &lt;/td&gt;	&lt;td class="highlight-green" data-highlight-colour="green"&gt;    		##In this cell i want to have the pages of space with edit restriction to groups		    &lt;/td&gt;  &lt;/tr&gt;  #end#end&lt;/table&gt;
```

The thing that i'm missing is how i will get the restricted pages per space. I had a look in Confluence's Api but i didn't manage to get the results that i want it.

For the restricted pages i would like to have both types of restriction (View and Edit)

Thanx in advance,

Kostas


Answers:
author: Tsol
body: Finally i have created a user macro for getting the pages with restrictions. Macro is the following

```
## Macro title: Restricted Pages## Macro has a body: N## Body processing: Selected body processing option## Output: Selected output option#### Developed by: Kostas Tsolakos## Date created: 24/04/2013## Installed by: Kostas Tsolakos## Macro to display a list of pages with restrictions in current space## @noparams## get pagemanager#set($containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager'))#set($getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null))#set($containerManager=$getInstanceMethod.invoke(null,null))#set($containerContext=$containerManager.containerContext)#set($pageManager=$containerContext.getComponent('pageManager'))#set($pages=$pageManager.getPermissionPages($space))#set( $edit = "Edit" )#set( $view = "View")&lt;table class="confluenceTable"&gt;	&lt;tr&gt;		&lt;th class="confluenceTh"&gt;Restricted Parent Pages&lt;/th&gt;		&lt;th class="confluenceTh"&gt;View&lt;/th&gt;		&lt;th class="confluenceTh"&gt;Edit&lt;/th&gt;					&lt;/tr&gt;	#if (!$pages.isEmpty())	#foreach ($page in $pages)		#set($permview = $page.getContentPermissionSet($view) )	#set($permedit = $page.getContentPermissionSet($edit) )	#set ($viewuser = $permview.UserNames)	#set ($edituser = $permedit.UserNames)	#set ($viewgroup = $permview.GroupNames)	#set ($editgroup = $permedit.GroupNames)		&lt;tr&gt;		&lt;td&gt;		#contentLink2($page true false)&lt;br&gt;		&lt;/td&gt;								&lt;td&gt;			#if($viewuser || $viewgroup)			#if(!$viewuser.isEmpty())			&lt;strong&gt;Individual Users&lt;/strong&gt;&lt;br&gt;			#foreach($user in $viewuser)						#usernameLink($user)&lt;br&gt;			#end			#end			#if(!$viewgroup.isEmpty())			&lt;strong&gt;Groups&lt;/strong&gt;&lt;br&gt;			#foreach($group in $viewgroup)			#set ( $groupObject = $userAccessor.getGroup($group) )			#set ( $memberList = $userAccessor.getMemberNamesAsList($groupObject) )			$group&lt;br&gt;						#end			#end			#else			No view restrictions			#end			&lt;/td&gt;		&lt;td&gt;		#if($edituser || $editgroup)			#if(!$edituser.isEmpty())			&lt;strong&gt;Individual Users&lt;/strong&gt;&lt;br&gt;			#foreach($user in $edituser)						#usernameLink($user)&lt;br&gt;			#end			#end			#if(!$editgroup.isEmpty())			&lt;strong&gt;Groups&lt;/strong&gt;&lt;br&gt;			#foreach($group in $editgroup)			#set ( $groupObject = $userAccessor.getGroup($group) )			#set ( $memberList = $userAccessor.getMemberNamesAsList($groupObject) )			$group&lt;br&gt;						#end			#end			#else			No edit restrictions			#end						&lt;/td&gt;		&lt;/tr&gt;	#end			#else		&lt;tr&gt;			&lt;td colspan="3"&gt;			There are not restricted pages in this space			&lt;/td&gt;		&lt;/tr&gt;	#end&lt;/table&gt;
```

comments: author: Tsol
body: I have edited a bit the macro. You can now see users and groups that have view and edit permissions on the restricted pages


author: Andy Cippico
body: Hi Kostas,Thanks for publishing your macro but when I run it all I see is this:Restricted \| Parent \| Pages \| View \| Edit ==============================There are not restricted pages in this space Do I need to run it from another space to see them all? Please forgive my noobness, I am only just starting out with this stuff.


author: Tsol
body: Hi Andy,in my answer i have put the latest version of macro. It works as expected in my Confluence instance. Which version do you use?It returns the restricted pages of the space that you use it.


author: Andy Cippico
body: Hi Kostas,I am using quite an old verion (3.5). As a matter of interest, I moved the macro to another space and it returned some values, although not the ones I expected. In fact, I've just read the script comments and realised that it only returns the pages for the current space. I missed that!I have about 20 spaces. I'm guessing I need to move the macro to the space I need the report on, then run it from there?


author: Andy Cippico
body: Ah, moving the macro to a space where there are page restrictions set seems to have worked. What I need now is a script that will produce a report for ALL spaces without having to run the macro in each space. Thanks very much for your help so far.


author: Bob Swift (personal)
body: Try [Script - list pages that have permissions set](https://bobswift.atlassian.net/wiki/display/SCRP/Script+-+list+pages+that+have+permissions+set)

comments: author: Tsol
body: Hey Bob,

thanx for the help but finally i did it by writing a simple macro and experimenting a bit with the API.

Cheers,

Kostas


author: Matthias Born
body: I combined both examples to one user macro, that displays all restricted pages from all spaces in a single table.

```
## Macro title: Restricted Pages## Macro has a body: N## Body processing: Selected body processing option## Output: Selected output option#### Developed by: Kostas Tsolakos (customized by Matthias Born)## Date created: 24/04/2013## Installed by: Matthias Born## Macro to display a list of pages with restrictions from all spaces## @noparams## get pagemanager#set ( $spaces = $spaceManager.getAllSpaces() )#set($containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager'))#set($getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null))#set($containerManager=$getInstanceMethod.invoke(null,null))#set($containerContext=$containerManager.containerContext)#set($pageManager=$containerContext.getComponent('pageManager'))#set( $edit = "Edit" )#set( $view = "View")&lt;style type="text/css"&gt;ul{margin: 0 !important;}&lt;/style&gt;&lt;table&gt;	&lt;colgroup&gt;		&lt;col style="width:50%;"/&gt;		&lt;col style="width:25%;"/&gt;		&lt;col style="width:25%;"/&gt;	&lt;/colgroup&gt;#foreach ($space in $spaces)#set($pages=$pageManager.getPermissionPages($space))&lt;tr&gt;&lt;th&gt;&lt;h1&gt;$space.getName()&lt;/h1&gt;&lt;/th&gt;&lt;th&gt;View&lt;/th&gt;&lt;th&gt;Edit&lt;/th&gt;&lt;/tr&gt;		#if (!$pages.isEmpty())		#foreach ($page in $pages)		 		#set($permview = $page.getContentPermissionSet($view) )		#set($permedit = $page.getContentPermissionSet($edit) )		#set ($viewuser = $permview.UserNames)		#set ($edituser = $permedit.UserNames)		#set ($viewgroup = $permview.GroupNames)		#set ($editgroup = $permedit.GroupNames)		&lt;tr&gt;			&lt;td&gt;#contentLink2($page true false)&lt;/td&gt;			&lt;td&gt;				#if($viewuser || $viewgroup)					#if(!$viewuser.isEmpty())						&lt;p&gt;Individual Users&lt;/p&gt;						&lt;ul&gt;						#foreach($user in $viewuser)&lt;li&gt;#usernameLink($user)&lt;/li&gt;#end						&lt;/ul&gt;					#end					#if(!$viewgroup.isEmpty())						&lt;p&gt;Groups&lt;/p&gt;						&lt;ul&gt;							#foreach($group in $viewgroup)								#set ( $groupObject = $userAccessor.getGroup($group) )								#set ( $memberList = $userAccessor.getMemberNamesAsList($groupObject) )								&lt;li&gt;$group&lt;/li&gt;							#end						&lt;/ul&gt;					#end					#else &lt;p&gt;No view restrictions&lt;/p&gt;				#end			&lt;/td&gt;			&lt;td&gt; 				#if($edituser || $editgroup)					#if(!$edituser.isEmpty())					&lt;p&gt;Individual Users&lt;/p&gt;					&lt;ul&gt;						#foreach($user in $edituser)&lt;li&gt;#usernameLink($user)&lt;/li&gt;#end					&lt;/ul&gt;					#end					#if(!$editgroup.isEmpty())					&lt;p&gt;Groups&lt;/p&gt;					&lt;ul&gt;					#foreach($group in $editgroup)						#set ( $groupObject = $userAccessor.getGroup($group) )						#set ( $memberList = $userAccessor.getMemberNamesAsList($groupObject) )						&lt;li&gt;$group&lt;/li&gt;					#end					&lt;/ul&gt;					#end					#else &lt;p&gt;No edit restrictions&lt;/p&gt;				#end			&lt;/td&gt;		&lt;/tr&gt;		#end		#else			&lt;tr&gt;				&lt;td colspan="3"&gt;There are not restricted pages in this space&lt;/td&gt;			&lt;/tr&gt;		#end	#end &lt;/table&gt;
```


Tags: macro

report

restriction