Macro to get all permissions for a specific user

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())
 
<h1>Permisos del usuario</h1>

<h3>Permisos individuales</h3>
<table class="confluenceTable">
	<tr>
	  <th class="confluenceTh">Espacio</th>
	</tr>
	#foreach($space in $allSpaces)  
	  #foreach ($permission in $space.getPermissions())
		#if ($permission.isUserPermission() && $permission.getType() == "VIEWSPACE" && $permission.getUserName() == ($paramnombreusuario))
		  <tr>
			<td class="confluenceTd">$space.getName()</td>
		  </tr>
		#end
	  #end
	#end
</table>

<h3>Permisos en grupos</h3>
<table class="confluenceTable">
	<tr>
	  <th class="confluenceTh">Espacio</th>
	  <th class="confluenceTh">Grupo</th>
	</tr>
	#foreach($space in $allSpaces)  
	  #foreach ($permission in $space.getPermissions())
		#if ($permission.isGroupPermission() && $permission.getType() == "VIEWSPACE")
		  #set ( $groupString = $permission.getGroup() )
		  #set ( $groupObject = $userAccessor.getGroup($groupString) )
		  #set ( $memberList = $userAccessor.getMemberNamesAsList($groupObject) )
		
		  #foreach ($member in $memberList)
			#if ($member == "$paramnombreusuario")
			  <tr>
				<td class="confluenceTd">$space.getName()</td>
				<td class="confluenceTd">$groupString</td>
			  </tr>		
			#end
		  #end
		#end
	  #end
	#end
</table>

 

Any ideas of how could I optimizate it?

Or is it something wrong in it?

Thanks!!

2 answers

1 accepted

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() && $permission.getType() == "VIEWSPACE" && $permission.getUserName() == ($paramnombreusuario))
          #set($result1 = "${result1}<tr>
            <td class='confluenceTd'>$space.getName()</td>
          </tr>")
        #elseif ($permission.isGroupPermission() && $permission.getType() == "VIEWSPACE")
          #set ( $groupString = $permission.getGroup() )
            #if ($userAccessor.hasMembership("$groupString", "$paramnombreusuario"))
              #set($result2 = "${result2}<tr>
                <td class='confluenceTd'>$space.getName()</td>
                <td class='confluenceTd'>$groupString</td>
              </tr>")      
            #end
        #end
      #end
    #end
 
<h1>Permisos del usuario</h1>
  
<h3>Permisos individuales</h3>
<table class="confluenceTable">
    <tr>
      <th class="confluenceTh">Espacio</th>
    </tr>
    $result1
</table>
 
<h3>Permisos en grupos</h3>
<table class="confluenceTable">
    <tr>
      <th class="confluenceTh">Espacio</th>
      <th class="confluenceTh">Grupo</th>
    </tr>
    $result2
</table>

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.

Great!! It worked now! Thanks so much :)

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() && $permission.getType() == "VIEWSPACE" && $permission.getUserName() == ($paramnombreusuario))
          #set($result1 = "${result1}<tr>
            <td class='confluenceTd'>$space.getName()</td>
          </tr>")
		#elseif ($permission.isGroupPermission() && $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}<tr>
                <td class='confluenceTd'>$space.getName()</td>
                <td class='confluenceTd'>$groupString</td>
              </tr>")      
            #end
          #end
        #end
      #end
    #end

<h1>Permisos del usuario</h1>
 
<h3>Permisos individuales</h3>
<table class="confluenceTable">
    <tr>
      <th class="confluenceTh">Espacio</th>
    </tr>
	$result1
</table>

<h3>Permisos en grupos</h3>
<table class="confluenceTable">
    <tr>
      <th class="confluenceTh">Espacio</th>
      <th class="confluenceTh">Grupo</th>
    </tr>
	$result2
</table>

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!

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

which version of Confluence are you using?

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...

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Tuesday in Confluence

We want to see the templates you've created in Confluence!

Hi Community, Jessica here from the Confluence Product Marketing team!  July’s community challenge is all about sharing pictures  — and as an extension of our first post on what ...

465 views 19 9
Join discussion

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