Macro to get all permissions for a specific user

Arantxa Lacasa Madero July 31, 2012

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

4 votes
Answer accepted
Amalia
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 1, 2012

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>

Steve Goldberg
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 2, 2012

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.

Arantxa Lacasa Madero August 2, 2012

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

0 votes
Amalia
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 31, 2012

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>

Arantxa Lacasa Madero July 31, 2012

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!

Amalia
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 31, 2012

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

Amalia
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 31, 2012

which version of Confluence are you using?

Arantxa Lacasa Madero August 1, 2012

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
TAGS
AUG Leaders

Atlassian Community Events