Macro to get all permissions for a specific user
Asked by Arantxa Lacasa Madero:

![](../../upfiles/error.jpg)

Hi all!!

I am developing a macro to know all view permissions for a specific user in every spaces in Confluence. For that, first I loop for all the spaces looking for his view individual permissions, and then I loop again to loop through the groups with permissions in the space to know if the user is there.

The problem is that we have many users and spaces, and after thinking for a while it shows the error that I attach.

Here is my macro:

```
## Macro title: User permissions## Macro has a body: Y or N (N)## Body processing: Selected body processing option## Output: Selected output option#### Developed by: Arantxa Lacasa Madero## Date created: 01/08/2012## Installed by: Arantxa Lacasa Madero ## Macro que lista todos los espacios en los que un usuario tiene acceso## @param nombreusuario:title=Nombre de usuario|type=string|desc=El nombre de inicio de sesion del usuario|required=true#set($allSpaces = $spaceManager.getAllSpaces()) &lt;h1&gt;Permisos del usuario&lt;/h1&gt;&lt;h3&gt;Permisos individuales&lt;/h3&gt;&lt;table class="confluenceTable"&gt;	&lt;tr&gt;	  &lt;th class="confluenceTh"&gt;Espacio&lt;/th&gt;	&lt;/tr&gt;	#foreach($space in $allSpaces)  	  #foreach ($permission in $space.getPermissions())		#if ($permission.isUserPermission() &amp;&amp; $permission.getType() == "VIEWSPACE" &amp;&amp; $permission.getUserName() == ($paramnombreusuario))		  &lt;tr&gt;			&lt;td class="confluenceTd"&gt;$space.getName()&lt;/td&gt;		  &lt;/tr&gt;		#end	  #end	#end&lt;/table&gt;&lt;h3&gt;Permisos en grupos&lt;/h3&gt;&lt;table class="confluenceTable"&gt;	&lt;tr&gt;	  &lt;th class="confluenceTh"&gt;Espacio&lt;/th&gt;	  &lt;th class="confluenceTh"&gt;Grupo&lt;/th&gt;	&lt;/tr&gt;	#foreach($space in $allSpaces)  	  #foreach ($permission in $space.getPermissions())		#if ($permission.isGroupPermission() &amp;&amp; $permission.getType() == "VIEWSPACE")		  #set ( $groupString = $permission.getGroup() )		  #set ( $groupObject = $userAccessor.getGroup($groupString) )		  #set ( $memberList = $userAccessor.getMemberNamesAsList($groupObject) )				  #foreach ($member in $memberList)			#if ($member == "$paramnombreusuario")			  &lt;tr&gt;				&lt;td class="confluenceTd"&gt;$space.getName()&lt;/td&gt;				&lt;td class="confluenceTd"&gt;$groupString&lt;/td&gt;			  &lt;/tr&gt;					#end		  #end		#end	  #end	#end&lt;/table&gt;
```

```
 
```

Any ideas of how could I optimizate it?

Or is it something wrong in it?

Thanks!!


Answers:
author: Amalia
body: How about this one... I think you don't need to go through loop to know if a user is a member of a group because there's a function for that. But I'm not sure if this is the same as the above code...

```
## Macro title: User permissions## Macro has a body: Y or N (N)## Body processing: Selected body processing option## Output: Selected output option#### Developed by: Arantxa Lacasa Madero## Date created: 01/08/2012## Installed by: Arantxa Lacasa Madero   ## Macro que lista todos los espacios en los que un usuario tiene acceso  ## @param nombreusuario:title=Nombre de usuario|type=string|desc=El nombre de inicio de sesion del usuario|required=true  #set($allSpaces = $spaceManager.getAllSpaces())#set($result1 = "")#set($result2 = "")     #foreach($space in $allSpaces)        #foreach ($permission in $space.getPermissions())        #if ($permission.isUserPermission() &amp;&amp; $permission.getType() == "VIEWSPACE" &amp;&amp; $permission.getUserName() == ($paramnombreusuario))          #set($result1 = "${result1}&lt;tr&gt;            &lt;td class='confluenceTd'&gt;$space.getName()&lt;/td&gt;          &lt;/tr&gt;")        #elseif ($permission.isGroupPermission() &amp;&amp; $permission.getType() == "VIEWSPACE")          #set ( $groupString = $permission.getGroup() )            #if ($userAccessor.hasMembership("$groupString", "$paramnombreusuario"))              #set($result2 = "${result2}&lt;tr&gt;                &lt;td class='confluenceTd'&gt;$space.getName()&lt;/td&gt;                &lt;td class='confluenceTd'&gt;$groupString&lt;/td&gt;              &lt;/tr&gt;")                  #end        #end      #end    #end &lt;h1&gt;Permisos del usuario&lt;/h1&gt;  &lt;h3&gt;Permisos individuales&lt;/h3&gt;&lt;table class="confluenceTable"&gt;    &lt;tr&gt;      &lt;th class="confluenceTh"&gt;Espacio&lt;/th&gt;    &lt;/tr&gt;    $result1&lt;/table&gt; &lt;h3&gt;Permisos en grupos&lt;/h3&gt;&lt;table class="confluenceTable"&gt;    &lt;tr&gt;      &lt;th class="confluenceTh"&gt;Espacio&lt;/th&gt;      &lt;th class="confluenceTh"&gt;Grupo&lt;/th&gt;    &lt;/tr&gt;    $result2&lt;/table&gt;
```

comments: author: Steve Goldberg
body: This is really cool. Now I just need to figure out how to add JS so that people can make a query without having to edit the page.


author: Arantxa Lacasa Madero
body: Great!! It worked now! Thanks so much :)


author: Amalia
body: How about this one? I put only a single loop for both tables :)

```
## Macro title: User permissions## Macro has a body: Y or N (N)## Body processing: Selected body processing option## Output: Selected output option#### Developed by: Arantxa Lacasa Madero## Date created: 01/08/2012## Installed by: Arantxa Lacasa Madero  ## Macro que lista todos los espacios en los que un usuario tiene acceso ## @param nombreusuario:title=Nombre de usuario|type=string|desc=El nombre de inicio de sesion del usuario|required=true #set($allSpaces = $spaceManager.getAllSpaces())#set($result1 = "")#set($result2 = "")    #foreach($space in $allSpaces)        #foreach ($permission in $space.getPermissions())		#if ($permission.isUserPermission() &amp;&amp; $permission.getType() == "VIEWSPACE" &amp;&amp; $permission.getUserName() == ($paramnombreusuario))          #set($result1 = "${result1}&lt;tr&gt;            &lt;td class='confluenceTd'&gt;$space.getName()&lt;/td&gt;          &lt;/tr&gt;")		#elseif ($permission.isGroupPermission() &amp;&amp; $permission.getType() == "VIEWSPACE")          #set ( $groupString = $permission.getGroup() )          #set ( $groupObject = $userAccessor.getGroup($groupString) )          #set ( $memberList = $userAccessor.getMemberNamesAsList($groupObject) )                   #foreach ($member in $memberList)            #if ($member == "$paramnombreusuario")              #set($result2 = "${result2}&lt;tr&gt;                &lt;td class='confluenceTd'&gt;$space.getName()&lt;/td&gt;                &lt;td class='confluenceTd'&gt;$groupString&lt;/td&gt;              &lt;/tr&gt;")                  #end          #end        #end      #end    #end&lt;h1&gt;Permisos del usuario&lt;/h1&gt; &lt;h3&gt;Permisos individuales&lt;/h3&gt;&lt;table class="confluenceTable"&gt;    &lt;tr&gt;      &lt;th class="confluenceTh"&gt;Espacio&lt;/th&gt;    &lt;/tr&gt;	$result1&lt;/table&gt;&lt;h3&gt;Permisos en grupos&lt;/h3&gt;&lt;table class="confluenceTable"&gt;    &lt;tr&gt;      &lt;th class="confluenceTh"&gt;Espacio&lt;/th&gt;      &lt;th class="confluenceTh"&gt;Grupo&lt;/th&gt;    &lt;/tr&gt;	$result2&lt;/table&gt;
```

comments: author: Arantxa Lacasa Madero
body: Hello Amalia, that's a good idea!

Did you test if it worked for you? It does not work for me, and I would like to discard any code mistake.

Thanks so much!


author: Amalia
body: Yes I have tested it and it's working for me. What does it show?


author: Amalia
body: which version of Confluence are you using?


author: Arantxa Lacasa Madero
body: I'm using version 3.5.7.

I'm afraid that I'm getting a java heap space while searching for group permissions. Only with individual permissions it works all right...


Tags: content