Macro that produces a list of users' last login date?
Asked by Steve Goldberg:

Would anybody be prepared to write a macro that I can use to produce a list of users showing when they last logged in? I'm aware that you can use com.atlassian.jira.bc.security.login.LoginInfo to do this, but I don't have the skills to write the macro myself. Any help would be appreciated.

Thanks

EDIT: If this you're just interested in the code, I have created a 'master' version that I frequently update following contributions from Remo Siegwart, Bruce Schlueter, and myself: [https://github.com/stevegoldberg/ConfluenceMacros/tree/master/Last%20Login](https://github.com/stevegoldberg/ConfluenceMacros/tree/master/Last%2520Login)


Answers:
author: Remo Siegwart
body: The following user macro should display all users and their last successful login date:

```
## @noparams#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'))#set($users = $userAccessor.getUsers())<table class="confluenceTable">    <tr>        <th class="confluenceTh">User</th>        <th class="confluenceTh">Last Successful Login Date</th>    </tr>    #foreach($user in $users)        <tr>            <td class="confluenceTd">#usernameLink($user.name)</td>            <td class="confluenceTd">$action.dateFormatter.formatDateTime($loginManager.getLoginInfo($user).lastSuccessfulLoginDate)</td>        </tr>    #end</table>
```

Hope this helps

comments: author: Steve Goldberg
body: That's incredibly useful, thanks. I may pester you again for some modifications if I can't figure them out myself.

Cheers


author: Remo Siegwart
body: Great, I'm glad that it's useful for you!

Here is some more information about the API of the objects in use:

* the **$user** object is of type: [com.atlassian.user.User](http://docs.atlassian.com/atlassian-user/2.0/apidocs/com/atlassian/user/User.html)
* with **$loginManager.getLoginInfo($user)** you get a [com.atlassian.confluence.security.login.LoginInfo](http://docs.atlassian.com/confluence/latest/com/atlassian/confluence/security/login/LoginInfo.html) object, which extends [com.atlassian.confluence.security.login.HistoricalLoginInfo](http://docs.atlassian.com/confluence/latest/com/atlassian/confluence/security/login/HistoricalLoginInfo.html)

This information should help you to customize the report to your needs.


author: Steve Goldberg
body: Well seeing as you're being so helpful, what I want to know is if it is possible to add a third column which would show whether the user has been disabled? We want to use the macro to quickly see who's inactive and can have their account disabled/deleted and a tick or cross next to each user to show if they are enabled or disabled would be helpful. Is this possible?


author: Remo Siegwart
body: You could use the [UserAccessor](http://docs.atlassian.com/atlassian-confluence/latest/com/atlassian/confluence/user/UserAccessor.html) for that. By calling **$userAccessor.isDeactivated($user)** you can find out if a user is deactivated. The end result could be something like this:

```
## @noparams#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'))#set($users = $userAccessor.getUsers())<table class="confluenceTable">    <tr>        <th class="confluenceTh">User</th>        <th class="confluenceTh">Last Successful Login Date</th>        <th class="confluenceTh">Active</th>    </tr>    #foreach($user in $users)        <tr>            <td class="confluenceTd">#usernameLink($user.name)</td>            <td class="confluenceTd">$action.dateFormatter.formatDateTime($loginManager.getLoginInfo($user).lastSuccessfulLoginDate)</td>            <td class="confluenceTd" style="text-align:center;">                #if($userAccessor.isDeactivated($user))                    <img src="/s/en_GB/3047/11/_/images/icons/emoticons/error.png" width="18" height="18" border="0">                #else                    <img src="/s/en_GB/3047/11/_/images/icons/emoticons/check.png" width="18" height="18" border="0">                #end            </td>        </tr>    #end</table>
```


author: Steve Goldberg
body: Wow, that's amazing. How about a column that adds in when the account was created?


author: Remo Siegwart
body: How about this:

```
## @noparams#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'))#set($crowdService = $containerContext.getComponent('crowdService'))#set($users = $userAccessor.getUsers())<table class="confluenceTable">    <tr>        <th class="confluenceTh">User</th>        <th class="confluenceTh">Last Successful Login Date</th>        <th class="confluenceTh">Creation Date</th>        <th class="confluenceTh">Active</th>    </tr>    #foreach($user in $users)        #set($crowdUser = $crowdService.getUser($user.name))        <tr>            <td class="confluenceTd">#usernameLink($user.name)</td>            <td class="confluenceTd">$action.dateFormatter.formatDateTime($loginManager.getLoginInfo($user).lastSuccessfulLoginDate)</td>            <td class="confluenceTd">$action.dateFormatter.formatDateTime($crowdUser.createdDate)</td>            <td class="confluenceTd" style="text-align:center;">                #if($userAccessor.isDeactivated($user))                    <img src="/s/en_GB/3047/11/_/images/icons/emoticons/error.png" width="18" height="18" border="0">                #else                    <img src="/s/en_GB/3047/11/_/images/icons/emoticons/check.png" width="18" height="18" border="0">                #end            </td>        </tr>    #end</table>
```


author: Steve Goldberg
body: Fantastic. So, just taking a look at it. Using the {table-plus} macro I am able to sort columns by clicking on the header but I have noticed that it is sorting alphabetically - how do you change the format of the dates so that it goes yy/mm/dd hh:mm ? I know you can change ".formatDateTime" to, say, ".format" but how do I use .DateFormatter?

Thanks


author: Remo Siegwart
body: You should be able to use the **formatGivenString(pattern, date)** method of the [DateFormatter](http://docs.atlassian.com/atlassian-confluence/latest/com/atlassian/confluence/core/DateFormatter.html). Something like this should work:

```
$action.dateFormatter.formatGivenString('yy/mm/dd hh:mm', $crowdUser.createdDate)
```


author: Steve Goldberg
body: Mucho gracias. Very helpful.


author: Rumceisz
body: Hi Remo,

I inserted the macro but the 'Last Successful Login Date' is empty for all the users! The user list is correct: the table lists all the Confluence users, but the other column is empty. What can be wrong? Are there any settings needed?

Thanks in advance!

Best regards,

Rumi


author: Remo Siegwart
body: Do you use Crowd for user management?

[See this answer by Thomas](https://community.atlassian.com/t5/Confluence-questions/Re-Macro-that-produces-a-list-of-users-last-login-date/qaq-p/426624/comment-id/72347#M72347)


author: Rumceisz
body: Hi Remo,

tha macro is fantastic!

Can you please show an extension/modification for the macro which display the last login of a particular group? Hence, our $users and confluence-users group (which are the members who can really login to docspace) have 300 members difference! Who can be these 300 plus members??


author: Remo Siegwart
body: Not sure if I understand your question correctly, but if you want to display only users of a particular group, use something like this:

```
## @param group:title=Group|type=string|required=true|desc=The group you want to report on#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'))#set($group = $userAccessor.getGroup($paramgroup))#if($group)    #set($usernames = $userAccessor.getMemberNames($group))    &lt;table class="confluenceTable"&gt;        &lt;tr&gt;            &lt;th class="confluenceTh"&gt;User&lt;/th&gt;            &lt;th class="confluenceTh"&gt;Last Successful Login Date&lt;/th&gt;        &lt;/tr&gt;        #foreach($username in $usernames)            #set($user = $userAccessor.getUser($username))            &lt;tr&gt;                &lt;td class="confluenceTd"&gt;#usernameLink($user.name)&lt;/td&gt;                &lt;td class="confluenceTd"&gt;$action.dateFormatter.formatDateTime($loginManager.getLoginInfo($user).lastSuccessfulLoginDate)&lt;/td&gt;            &lt;/tr&gt;        #end    &lt;/table&gt;#else    &lt;p&gt;&lt;i&gt;No group with name "$paramgroup" found!&lt;/i&gt;&lt;/p&gt;#end
```

Hope this helps


author: Rumceisz
body: Hi Remo,

you're right understood! Your macro lists all the users of the Confluence instance. I only need a particular group members last login.

Thank you for the macro! I only ask where can I put the group-name in the code? I guess to `$paramgroup))?`

Thanks again,

Rumi


author: Thomas Berger
body: Hi Rumi, you need to put Remo's code in a 'User Macro' and name it e.g. 'lastlogin', see [https://confluence.atlassian.com/display/DOC/Writing+User+Macros](https://confluence.atlassian.com/display/DOC/Writing+User+Macros.) for more details.

Then you would use it like {lastlogin:group=confluence-users} on the page you want the report.


author: Rumceisz
body: Hi Remo and Thomas!

It works thank you very much!!

Rumi


author: Selcuk Savas
body: Remo you're a life saver!


author: prakash ganeshan
body: Superb Macro .. :)


author: Royce Wong
body: I would like to know where $crowdUser.createdDate is from. <https://developer.atlassian.com/static/javadoc/embedded-crowd-api/latest/reference/com/atlassian/crowd/embedded/api/User.html> doesn't contain createdDate method. I am trying to find the API equivalent. Please let me know who you find out about $crowdUser.createdDate. Thanks.


author: Anudeep Gudipelli
body: Hi Remo ,

Reading comments felt me that this macro is awesome. We use crowd for user directories, and i din't find solution for that, when click on the link you provided for thomas answer reg crowd user management, the link says its invaild parameters.

I appreciate your help

Thank you,

Anudeep


author: Remo Siegwart
body: For crowd see [this answer](https://community.atlassian.com/t5/Confluence-questions/Re-Macro-that-produces-a-list-of-users-last-login-date/qaq-p/426624/comment-id/72347#M72347), just below your comment...


author: Anudeep Gudipelli
body: Does this macro works on v5.9 and 5.10?

Currently we have two instances v5.9 and v 5.10 and in future we are going to migrate into one instance which would be v6.6.

Is there any chance to check, if there are some steps, I would like to exercise them.

Thank you


author: Charles Pikscher
body: Seems like this broke for Confluence 7, no more loginManager. Anyone know the fix?

comments: author: Charles Pikscher
body: Bah. They updated this to show no macro solution in pre-7 Confluence. Guess who's staying at 6.15.9 for awhile...

<https://confluence.atlassian.com/confkb/how-to-get-a-list-of-active-users-counting-towards-the-confluence-license-298978076.html>


author: Sam
body: Is there a version of this fix that includes the last login date?


author: Chris Kent
body: Has anyone been able to get this User Macro working for 7.x?

If so can you please share the code. I expect the ContainerManager has moved, or has a new class name perhaps, but I can't find it.

comments: author: KC Integrations
body: I managed to get my User Macro working in 7.x. For those interested here is my code:

## [@Param](/t5/user/viewprofilepage/user-id/1345631) group:title=Group\|type=string\|required=true\|desc=Group to show

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

#set($timeNow=$content.currentDate.time)  
#set($group = $userAccessor.getGroup($paramgroup))

#if($group)  
#set($usernames = $userAccessor.getMemberNames($group))

\<table class="confluenceTable" width="660px"\>  
\<tr\>  
\<th class="confluenceTh " width="220px"\>Name\</th\>  
\<th class="confluenceTh" width="110px"\>Create date\</th\>  
\<th class="confluenceTh" width="110px"\>Last Successful Login\</th\>  
\<th class="confluenceTh" width="110px"\>Last Failed Login\</th\>  
\<th class="confluenceTh" width="110px" style="text-align:center;"\>Activated\</th\>  
\<th class="confluenceTh" width="110px" style="text-align:center;"\>Days\</th\>  
\</tr\>  
#foreach($username in $usernames)  
#set($t3="9999")  
#set($user = $userAccessor.getUserByName($username))  
#set($crowdUser = $crowdService.getUser($user.name))  
#set($lastLogin = $loginManager.getLoginInfo($user).lastSuccessfulLoginDate)

\<tr\>  
\<td class="confluenceTd"\>  
\<a target="_tab" href="/admin/users/viewuser.action?username=$username"\>$user.getFullName()\</a\>  
\</td\>  
\<td class="confluenceTd" style="text-align:center;"\>  
$action.dateFormatter.formatGivenString('yyyy.MM.dd', $crowdUser.createdDate)\</td\>  
\<td class="confluenceTd" style="text-align:center;"\>  
\<div\>$action.dateFormatter.formatGivenString('yyyy.MM.dd',$lastLogin)\</div\>  
\</td\>  
\<td class="confluenceTd" style="text-align:center;"\>  
$action.dateFormatter.formatGivenString('yyyy.MM.dd',$loginManager.getLoginInfo($user).lastFailedLoginDate)\</td\>  
\<td class="confluenceTd" style="text-align:center;"\>  
#if($userAccessor.isDeactivated($user))  
\<ac:macro ac:name="status"\>  
\<ac:parameter ac:name="colour"\>Red\</ac:parameter\>  
\<ac:parameter ac:name="title"\>No\</ac:parameter\>  
\</ac:macro\>  
#else  
\<ac:macro ac:name="status"\>  
\<ac:parameter ac:name="colour"\>Green\</ac:parameter\>  
\<ac:parameter ac:name="title"\>Yes\</ac:parameter\>  
\</ac:macro\>  
#set($totalactiveusers = $totalactiveusers + 1)  
#end  
\</td\>

#if($loginManager.getLoginInfo($user).lastSuccessfulLoginDate)  
#set($t3= $timeNow - $loginManager.getLoginInfo($user).lastSuccessfulLoginDate.time)  
#set($t3=$t3/1000/24/60/60)  
#end  

\<td class="confluenceTd" style="text-align:center;"\>  
#if($t3 \< 31)  
\<ac:macro ac:name="status"\>  
\<ac:parameter ac:name="colour"\>Green\</ac:parameter\>  
\<ac:parameter ac:name="title"\>$t3\</ac:parameter\>  
\</ac:macro\>  
#elseif($t3 \< 365)  
\<ac:macro ac:name="status"\>  
\<ac:parameter ac:name="colour"\>Yellow\</ac:parameter\>  
\<ac:parameter ac:name="title"\>$t3\</ac:parameter\>  
\</ac:macro\>  
#else  
\<ac:macro ac:name="status"\>  
\<ac:parameter ac:name="colour"\>Red\</ac:parameter\>  
\<ac:parameter ac:name="title"\>$t3\</ac:parameter\>  
\</ac:macro\>  
#end  
\</td\>  
\</tr\>  
#end  
\</table\>  
#end


author: Diego
body: hey [@KC Integrations](/t5/user/viewprofilepage/user-id/518669)

Thanks for updating it and sharing but for some reason mine is not showing the Create, Last Login and Last Failed login columns. Days is always showing red and 9999. Do you happen to know what could I be doing wrong? I just copied and pasted your code. Is your still working?

Thanks!


author: Chris Kent
body: Hi [@Diego](/t5/user/viewprofilepage/user-id/3106983) ,

I think you need to enable some classes from the velocity context. Edit the file:

and at the bottom, comment out some classes, here is a copy of the last section of the file: WEB-INF/classes/velocity.properties

# ----------------------------------------------------------------------------  
# SECURE INTROSPECTOR  
# ----------------------------------------------------------------------------  
# If selected, prohibits methods in certain classes and packages from being  
# accessed. If you are adding new package or class restriction, please also  
# add it to /confluence-core/confluence/src/etc/java/org/apache/velocity/runtime/defaults/velocity.properties  
# The list is maintained at:  
# <https://extranet.atlassian.com/display/CSPF/Restrict+packages+and+classes+usage+from+velocity+files>  
# ----------------------------------------------------------------------------

#introspector.restrict.packages = java.lang.reflect,\\  
introspector.restrict.packages = \\  
com.atlassian.cache,\\  
com.atlassian.confluence.util.http,\\  
com.atlassian.failurecache,\\  
com.atlassian.vcache,\\  
com.atlassian.sal.api.net,\\  
com.google.common.cache,\\  
com.google.common.net,\\  
com.hazelcast,java.jms,\\  
java.rmi,\\  
javax.management,\\  
javax.naming,\\  
org.apache.commons.httpclient,\\  
org.apache.httpcomponents.httpclient,\\  
org.apache.http.client,\\  
org.ehcache,\\  
com.google.common.reflect,\\  
com.sun.jmx,com.sun.jna,\\  
javax.xml,jdk.nashorn,\\  
net.bytebuddy,\\  
net.sf.cglib,org.apache.bcel,\\  
org.javassist,org.ow2.asm,\\  
sun,\\  
com.atlassian.activeobjects,\\  
com.atlassian.hibernate,\\  
#java.sql,\\  
javax.persistence,\\  
#javax.sql,\\  
liquibase,\\  
net.java.ao,\\  
net.sf.hibernate,\\  
org.hibernate,\\  
com.atlassian.confluence.setup.bandana,\\  
com.atlassian.filestore,\\  
com.atlassian.media,\\  
com.google.common.io,java.io,\\  
java.nio,java.util.jar,\\  
java.util.zip,\\  
org.apache.commons.io,\\  
com.atlassian.confluence.impl.util.sandbox,\\  
com.atlassian.confluence.util.io,\\  
com.atlassian.confluence.util.sandbox,\\  
com.atlassian.quartz,\\  
com.atlassian.scheduler,\\  
com.atlassian.utils.process,\\  
com.atlassian.util.concurrent,\\  
io.atlassian.util.concurrent,\\  
java.util.concurrent,\\  
org.apache.commons.exec,\\  
org.springframework.util.concurrent,\\  
org.quartz,\\  
oshi

#introspector.restrict.classes = java.lang.Class,\\  
introspector.restrict.classes = \\  
java.lang.ClassLoader,\\  
java.lang.Compiler,\\  
java.lang.InheritableThreadLocal,\\  
java.lang.Package,\\  
java.lang.Process,\\  
java.lang.Runtime,\\  
java.lang.RuntimePermission,\\  
java.lang.SecurityManager,\\  
java.lang.System,\\  
java.lang.Thread,\\  
java.lang.ThreadGroup,\\  
java.lang.ThreadLocal, \\  
com.atlassian.applinks.api.ApplicationLinkRequestFactory,\\  
com.atlassian.confluence.util.ConfluenceUberClassLoader,\\  
com.atlassian.core.util.ClassLoaderUtils,\\  
com.atlassian.core.util.ClassHelper


author: Frederick Roeser
body: [@Chris Kent](/t5/user/viewprofilepage/user-id/502150)   

Thanks for posting your code. The original "last login" macro is my favourite admin plugin. Unfortunately it does not work anymore .  

I'm on 7.4.5 (server version) and I tried your solution. Sadly it does not work for me. It just produces a blank page.

Does anyone has an idea why?


author: KC Integrations
body: Hi,

Did you remove all the security restrictions for velocity templates. These security settings are new and prevent a lot of complicated user macros from working ;(


author: Charles Pikscher
body: Finally got around to trying this (editing velocity.properties). I can confirm it works on 7.9.3. Nice find!


author: Charles Pikscher
body: FYI. there was a change (just updated to 7.19.12) per:

<https://developer.atlassian.com/server/confluence/confluence-objects-accessible-from-velocity/>

Had to add this to setenv.sh:

```
CATALINA_OPTS="-Dmacro.required.velocity.context.keys=action,req,spaceManager,permissionHelper,userAccessor,htmlUtil ${CATALINA_OPTS}"
```


author: Robert Barbosa
body: I tried using this user list with last login user macro in 8.5.11 with no luck.

What tweak may I use?


author: Romain LORY
body: Hello,

I try to do this on Confluence 7.2.1 and the column Last successful login date is still black.

Have you an idea on how to solved it?

Regards

comments: author: Gerald MURIEL
body: Hello same here, we are on 7.18.3.


author: Bruce Schlueter
body: Hi Remo,

a really nice job you did with your macro. What I noticed is that the sort order seems to brake if we add the time to the line, so we just keep the date like this :

```
&lt;td class="confluenceTd"&gt;$action.dateFormatter.formatGivenString('dd.MM.yyyy', $crowdUser.createdDate)&lt;/td&gt;
```

That is enough for us as we do not really need the exact time and the day suffices.

Another point concerning the last column "Active". Is there a possibility to sort this? I cant get it working...

Any idea would be great, like adding "True" or "False" and hide this and just show the image.

Thanks

comments: author: Steve Goldberg
body: Instead of using tick and cross icons, I just use the status macro:

```
#if($userAccessor.isDeactivated($user))	&lt;ac:macro ac:name="status"&gt;		&lt;ac:parameter ac:name="colour"&gt;Red&lt;/ac:parameter&gt;		&lt;ac:parameter ac:name="title"&gt;NO&lt;/ac:parameter&gt;	&lt;/ac:macro&gt;#else	&lt;ac:macro ac:name="status"&gt;		&lt;ac:parameter ac:name="colour"&gt;Green&lt;/ac:parameter&gt;		&lt;ac:parameter ac:name="title"&gt;YES&lt;/ac:parameter&gt;	&lt;/ac:macro&gt;#set($totalactiveusers = $totalactiveusers + 1)#end
```


author: Bruce Schlueter
body: Thanks, that works like a charme.

We are just setting up our wiki and we would like to know who never logged in. I tried something like

```
#if($loginManager.getLoginInfo($user).lastSuccessfulLoginDate ==' ')or this #if($loginManager.getLoginInfo($user).lastSuccessfulLoginDate ==null)
```

But I just get an empty table. How can I check for null values?


author: Remo Siegwart
body: Try the following:

```
#if(!$loginManager.getLoginInfo($user).lastSuccessfulLoginDate)
```


author: Bruce Schlueter
body: Thanks a lot, that is what I was missing.


author: Thomas Berger
body: Little bug for Crowd users: '$user' should be '$user.name' for the last successful login date:

```
<td class="confluenceTd">$action.dateFormatter.formatDateTime($loginManager.getLoginInfo($user.name).lastSuccessfulLoginDate)</td>
```

comments: author: Steve Goldberg
body: If I modify the code to include that change, will it still work for non-Crowd users?


author: Thomas Berger
body: |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Not sure, don't think so. We are using Crowd and I was referring to the code you've posted at (Feb 08 at 11:15 AM). But I've noticed that the version above that was not for Crowd. |


author: Application Services
body: I found out that the updated_date in the Confluence \[dbo\].\[cwd_user\] table actually shows last log in date/time.

Very useful.


author: Ankit Jhingan
body: I am getting the error using the macro shared by [@remo](/t5/user/viewprofilepage/user-id/1544989)

page isnt working because it took too long to respond.  
HTTP ERROR 504


author: JD Gebicki
body: Thank you so much, Remo! We are always close to our user limit, and this will be immensely helpful in identifying inactive users to be disabled. Really, thank you!!


author: Dana Jansen
body: is it possible to prevent specific groups from being listed. i.e. the confluence-users group will have too many users to list


author: Alex Fox
body: Is it possible to use this on a confluence page but to view the user's last login date to Jira? Our confluence uses Jira for user management. Great macro though!


author: SimratPal Singh
body: How do I consume this code withing my Confluence?

comments: author: George Lewe (LSY)
body: Hi SimratPal,

you have to write a User Macro

<https://confluence.atlassian.com/doc/writing-user-macros-4485.html>

Only works with Confluence Server though.


author: Charles Pikscher
body: Sorted by date. Sorry, can't get the spaces right.

```
## @param group:title=Group|type=string|required=true|desc=Group Name|default=confluence-users
#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'))
#set($group = $userAccessor.getGroup($paramgroup))
#set($usernames = $userAccessor.getMemberNames($group))

#set($rows= [])
#foreach($username in $usernames)
 #set($user = $userAccessor.getUser($username))
 #set($foo = $rows.add( {"name" : $user.name, "date" : $loginManager.getLoginInfo($user).lastSuccessfulLoginDate} ))
#end

#set($ii = $rows.size() - 2)
#foreach($i in [0..$ii])
 #set($jj = $rows.size() - $i - 2)
 #foreach($j in [0..$jj])
 #set($inc = $j + 1)
 #if ($rows.get($j).get("date"))
 #if ($rows.get($inc).get("date"))
 #if ($rows.get($j).get("date").before($rows.get($inc).get("date")))
 #set($tmp = $rows.get($j))
 #set($foo = $rows.set($j, $rows.get($inc)))
 #set($foo = $rows.set($inc, $tmp))
 #end
 #end
 #else
 #set($tmp = $rows.get($j))
 #set($foo = $rows.set($j, $rows.get($inc)))
 #set($foo = $rows.set($inc, $tmp))
 #end
 #end
#end

<h2>$group</h2>

<table class="confluenceTable">
 <tr>
 <th class="confluenceTh">Count</th>
 <th class="confluenceTh">User</th>
 <th class="confluenceTh">Last Successful Login Date</th>
 </tr>
 #foreach($row in $rows)
 <tr>
 <td class="confluenceTd">$velocityCount</td>
 <td class="confluenceTd">#usernameLink($row.get("name"))</td>
 <td class="confluenceTd">$action.dateFormatter.formatDateTime($row.get("date"))</td>
 </tr>
 #end
</table>
```

comments: author: Nidhi Telang
body: Hi Folks,

Could you please advise for Bitbucket. I need to find last login date of users in bitbucket.

com.atlassian.jira.bc.security.login.LoginInfo - this one is for jira, can't find one for bitbucket. Please advice.

Thanks


author: Charles Pikscher
body: For Bitbucket, I use the REST API (rest/api/1.0/admin/users) to get lastAuthenticationTimestamp (ms from 1/1/1970).


author: Dana Grahf
body: Do I just need to paste the above code snipped into Create UserMacro from the Admin console? If so what Macro Processing Body should I select: No Macro Body, Escaped, Unrendered, Rendered


author: Charles Pikscher
body: I used No macro body. And then add the macro to a page with Add-\>Other Macros


author: Dana Grahf
body: Thank you. Also on the login details topic - trying to solve this item too... <https://community.atlassian.com/t5/Confluence-questions/postgres-statement-for-null-successdate/qaq-p/884856>


author: Charles Pikscher
body: This macro will show a blank for users in the group that have never logged in.


author: Stefan Baader
body: Remo, very useful User Macro. Do you know how to filter only users of some given space?

Cheers, Stefan

comments: author: Steve Goldberg
body: What do you mean by 'space'? Do you mean 'group' or do you mean something more complicated like the users who can access a particular space?


author: Stefan Baader
body: Steve, I mean all users with permissions to a specific space (kind of community). Groups, single users.


author: George Lewe (LSY)
body: Try this. It displays last login of user permitted to current space:{code}## Macro title: Last Login By Current Space## Macro has a body: N## Body processing: No macro body#### Macro to display the last login date of users who have access to the current space## @noparams#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'))#set($users = $userAccessor.getUsers())\<table class="confluenceTable"\> \<tr\> \<th class="confluenceTh"\>User\</th\> \<th class="confluenceTh"\>Last Successful Login\</th\> \</tr\> #foreach($user in $users) ## list the last login date of users who can view the current space #if ($permissionHelper.canView($user, $space)) \<tr\> \<td class="confluenceTd"\>#usernameLink($user.name)\</td\> #if (!$loginManager.getLoginInfo($user).lastSuccessfulLoginDate) \<td class="confluenceTd" style="background-color:#ff0000"\> \<strong\>NEVER\</strong\> \</td\> #else \<td class="confluenceTd"\>$action.dateFormatter.formatDateTime($loginManager.getLoginInfo($user.name).lastSuccessfulLoginDate)\</td\> #end \</tr\> #end #end\</table\>{code}


author: George Lewe (LSY)
body: Jesus, I am commenting this for the third time now. What is the markup for a code block in a comment here?:-)


author: Selcuk Savas
body: Hi Remo,I have been using your macro and it works perfectly.Can I ask a favor?We recently created new groups and new users and it become complicated for me to report on them base on their last login details.Is it possible for you to add users login id and groups that they are in it to the macro?I tried myself but no success andf I am lost.I really appreciate your help.Cheers,Selcukl


author: Selcuk Savas
body: Hi Remo,I have been using your macro and it works perfectly.Can I ask a favor?We recently created new groups and new users and it become complicated for me to report on them base on their last login details.Is it possible for you to add users login id and groups that they are in it to the macro?I tried myself but no success andf I am lost.I really appreciate your help.Cheers,Selcuk

comments: author: Steve Goldberg
body: Hi Selcuk, you can use $userAccessor.getGroupNamesForUserName($username) to get the groups a user is part of and then use a loop to print them out as you want. E.g.:

```
#set ($groups = $userAccessor.getGroupNamesForUserName($username))	&lt;ul&gt;		#foreach ($group in $groups)			&lt;li&gt;$group&lt;/li&gt;		#end	&lt;/ul&gt;#end
```


author: tg
body: Hi and thanks all the above!

I'm doing this in Confluence 3.3 and `$lastLoginDate.time `doesn't work. (But $content.currentDate.time works fine.) Any suggestions on how else I might get $loginManager.getLoginInfo($user.name).lastSuccessfulLoginDate) in milliseconds?

Also, how do I format my compare date that is in milliseconds into a formatted date?

I'm using the code from here as the basis of my macro, so no JQuery or javascript is involved.

Any help would be greatly appreciated!


author: Doods Perea
body: Thanks Remo it works!

One little kink though, we are on Confluence 4.2.7 so the columns won't sort. Anyway you could extend help by providing extra code to the one you wrote above?  

Kind regards,

Doods


author: Doods Perea
body: Hi Remo - excellent macro.

Is there a way to display only those that have not logged in to the system for the last 30 days? Also, can the column titles become clickable for sorting (desc/asc)?  

Thanks in advance.

comments: author: Remo Siegwart
body: Just add an if-clause and compare the last login date with the current date:

```
## @noparams#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'))#set($users = $userAccessor.getUsers())#set($thirtyDaysInMilliseconds = 30 * 24 * 60 * 60 * 1000)#set($thirtyDaysInThePastInMilliseconds = $content.currentDate.time - $thirtyDaysInMilliseconds)&lt;table class="confluenceTable"&gt;    &lt;tr&gt;        &lt;th class="confluenceTh"&gt;User&lt;/th&gt;        &lt;th class="confluenceTh"&gt;Last Successful Login Date&lt;/th&gt;    &lt;/tr&gt;    #foreach($user in $users)        #set($lastLoginDate = $loginManager.getLoginInfo($user).lastSuccessfulLoginDate)        #if($thirtyDaysInThePastInMilliseconds &gt; $lastLoginDate.time)            &lt;tr&gt;                &lt;td class="confluenceTd"&gt;#usernameLink($user.name)&lt;/td&gt;                &lt;td class="confluenceTd"&gt;$action.dateFormatter.formatDateTime($lastLoginDate)&lt;/td&gt;            &lt;/tr&gt;        #end    #end&lt;/table&gt;
```

Table columns should be sortable by default [since Confluence version 4.3](https://confluence.atlassian.com/display/DOC/Confluence+4.3+Release+Notes#Confluence4.3ReleaseNotes-Tablesortingandhighlighting).

Hope this helps


author: Doods Perea
body: Thanks Remo it works!

One little kink though, we are on Confluence 4.2.7 so the columns won't sort. Anyway you could extend help by providing extra code to the one you wrote above?  

Kind regards,

Doods


author: Remo Siegwart
body: Sadly, there is no easy way to do that in a user macro. You would need to write a custom plugin for that. So, I would suggest to wait for 4.3.


author: Thomas Berger
body: You could use Bob Swift's Table-plus plugin to get a sortable table.


author: Kjell Lauren
body: I tried this on 3.5.17 and it shows empty fields for the last login, what might be the problem?

comments: author: Thomas Berger
body: ```
 Try '$user.name' instead of '$user', seems to be a v3.5.x related issue...
```

```
&lt;td class="confluenceTd"&gt;$action.dateFormatter.formatDateTime($loginManager.getLoginInfo($user.name).lastSuccessfulLoginDate)&lt;/td&gt;
```


author: Thomas Berger
body: Not sure, don't think so. We are using Crowd and I was referring to the code you've posted at (Feb 08 at 11:15 AM).

But I've noticed that the version above that was not for Crowd.


author: Jason Pepper
body: Thats a great little script..

Please can you tell me how I would get a count of user logins e.g. user1 has logged in 123 times.. ?

Thx

comments: author: Remo Siegwart
body: As far as I know it's not possible by default in Confluence. Confluence only stores a "total failed login count", but no total login count.


Tags: last-login

login

user