1. Home
  2. Docs
  3. Confluence
  4. Confluence User Macros
  5. Group Members

Group Members

Summary

This Confluence user macro adds a sorted table with numbered rows for each member of a given group.
This macro is based on the request I probably got most: Who is in that group?

Source Code

#* ============================================================================
 * GROUP MEMBERS
 *
 * This Confluence user macro adds a sorted table with numbered rows for each
 * member of a given group.
 *
 * Version: 1.7.0
 * Date:    2019-06-28
 * Author:  George Lewe, Zsolt Balint
 * Source:  https://github.com/glewe/group-members
 * License: GNU LGPLv3
 *
 * Macro body:      None
 * Body processing: None
 *#

#* ----------------------------------------------------------------------------
 * PARAMETER
 *#
## @param Group:title=Group|type=string|desc=Enter the group name here|default=jira-administrators
## @param ShowUsername:title=Show username|type=boolean|desc=Select whether to show the username in an extra column.|default=false
## @param ShowEmailAddresses:title=Show mail addresses|type=boolean|desc=Select whether to show the email addresses in an extra column.|default=false
## @param ShowLastLogin:title=Show last login date and time|type=boolean|desc=Select whether to show the last login date and time.|default=false
## @param ShowOtherMemberships:title=Show other memberships|type=boolean|desc=Select whether to show all other group memberships for each user of this group. Attention: This could lead to a long runtime.|default=false
## @param ShowSendMailButton:title=Show send email button|type=boolean|desc=Select whether to show a "Send mail to group" button at the bottom.|default=false
## @param ShowCountOnly:title=Show count only|type=boolean|desc=Select whether to just show the group name and user count.|default=false

#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($loginManager = $containerContext.getComponent('loginManager'))

#* ----------------------------------------------------------------------------
 * PROCESS INPUT
 *#
#if (!$paramGroup)
   #set ($paramshowGroup="jira-administrators")
#else
   #set ($paramshowGroup=$paramGroup)
#end

#set ( $group = $userAccessor.getGroup($paramshowGroup) )

#if ($group)

   #set ( $usernames = $userAccessor.getMemberNamesAsList($group) )
   
   #* ----------------------------------------------------------------------------
    * BUBBLE SORT BY FULL NAME
    *#
   ## 
   ## Create an array that we can sort
   ##
   #set( $uarray = [] )
   #set( $unarray = [] )
   
   ##
   ## Now fill it with the full names
   ##
   #set ( $count = 0 )
   #foreach( $username in  $usernames )
      #set( $fullName = $uarray.add( $userAccessor.getUserByName($username).getFullName() ) )
      #set( $un = $unarray.add( $username) )
      #set( $count = $count + 1 )
   #end
   
   ##
   ## Bubble Sort (takes n^2 passes)
   ##
   #set($size=$uarray.size())
   #foreach($junk in $uarray)
      #set($actual=-1)
      #foreach($line in $uarray)
         #set($actual=$actual+1)
         #if($velocityCount<$size)
            ##
            ## Compare with previous. If smaller, swap.
            ##
            #if ($line.compareToIgnoreCase($uarray.get($velocityCount)) > 0 )
               #set ($tmp=$uarray.get($velocityCount))
               #set ($junk=$uarray.set($velocityCount,$line))
               #set ($junk=$uarray.set($actual,$tmp))
               ##
               ## Sort usernames together with fullnames as map is not available in confluence velocity version
               ##
               #set ($tmp2=$unarray.get($velocityCount))
               #set ($junk=$unarray.set($velocityCount, $unarray.get($actual)))
               #set ($junk=$unarray.set($actual, $tmp2))
            #end
         #end
      #end
   #end
   
   #* ----------------------------------------------------------------------------
    * OUTPUT
    *#
   #set ($colspan = 2)
   #if ($paramShowUsername == true)
      #set ($colspan = $colspan + 1)
   #end
   #if ($paramShowEmailAddresses == true)
      #set ($colspan = $colspan + 1)
   #end
   #if ($paramShowLastLogin == true)
      #set ($colspan = $colspan + 1)
   #end
   #if ($paramShowOtherMemberships == true)
      #set ($colspan = $colspan + 1)
   #end
    
   #set ( $i = 1 )
   #set ( $j = 0 )
   #set ( $distributionList = "" )

   <!-- Group Members Macro -->
   #if ($paramShowCountOnly == true)
      <strong>$paramshowGroup ($count)</strong>
   #else
      <table class="confluenceTable">
         <tr>
            <th colspan="$colspan" class="highlight-blue confluenceTh" style="padding:12px;min-width:300px;"><div class="aui-icon aui-icon-large aui-iconfont-group" style="padding-right:16px;float:left;">Group</div><div style="font-size:16px;padding-top:4px;">$paramshowGroup ($count)</div></th>
         </tr>
         <tr>
            <th class="confluenceTh" style="text-align:center;">#</th>
            #if ($paramShowUsername == true)
               <th class="confluenceTh"><span class="aui-icon aui-icon-small aui-iconfont-person">Username</span> Username</th>
            #end
            <th class="confluenceTh"><span class="aui-icon aui-icon-small aui-iconfont-person-circle">Profile</span> Profile</th>
            #if ($paramShowEmailAddresses == true)
               <th class="confluenceTh"><span class="aui-icon aui-icon-small aui-iconfont-email">Email</span> E-mail</th>
            #end
            #if ($paramShowLastLogin == true)
               <th class="confluenceTh"><span class="aui-icon aui-icon-small aui-iconfont-sign-in">Last Login</span> Last Login</th>
            #end
            #if ($paramShowOtherMemberships == true)
               <th class="confluenceTh"><span class="aui-icon aui-icon-small aui-iconfont-people">Membership</span> Other Memberships</th>
            #end
         </tr>
         #foreach ($item in $uarray)
            #set( $eMail = $userAccessor.getUserByName($unarray.get($j)).getEmail() ) 
            #set( $lastLogin = $loginManager.getLoginInfo($unarray.get($j)).lastSuccessfulLoginDate )
            #set( $distributionList = $distributionList + $eMail + "," )
            <tr>
               <td class="confluenceTd" style="text-align: right;">$i</td>
               #if ($paramUsername == true)
                  <td class="confluenceTd">$unarray.get($j)</td>
               #end
               <td class="confluenceTd"><a href='/display/~$unarray.get($j)' title="Show profile...">$item</a></td>
               #if ($paramShowEmailAddresses == true)
                  <td class="confluenceTd"><a href='mailto:$eMail' title="Send email to this user...">$eMail</a></td>
               #end
               #if ($paramShowLastLogin == true)
                  <td class="confluenceTd">$action.dateFormatter.formatGivenString('yyyy-MM-dd HH:mm', $lastLogin)</td>
               #end
               #if ($paramShowOtherMemberships == true)
               <td>
                  #set ( $groupnames = $userAccessor.getGroupNamesForUserName($unarray.get($j)) )
                  #foreach ($groupname in $groupnames)
                     $groupname<br>
                  #end
               </td>
               #end
            </tr>
            #set ( $j = $i )
            #set ( $i = $i + 1 )
         #end
      </table>
      #if ($paramShowSendMailButton == true)
         <p><a href="mailto:$distributionList"><button class="aui-button aui-button-primary"><span class="aui-icon aui-icon-small aui-iconfont-email">Email</span> Send mail to group</button></a></p>
      #end
   #end
#else

   #* ----------------------------------------------------------------------------
    * OUTPUT: Group not found
    *#
   <!-- Group Members Macro -->
   <table class="confluenceTable">
      <tr>
         <th class="confluenceTh"><span class="aui-icon aui-icon-small aui-iconfont-people">Group</span> $paramshowGroup (not found)</th>
      </tr>
   </table>

#end
Was this article helpful to you? Yes No

Leave a Reply

Your email address will not be published. Required fields are marked *