Macro that produces a list of users' last login date?

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

12 answers

1 accepted

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

That's incredibly useful, thanks. I may pester you again for some modifications if I can't figure them out myself.

Cheers

Great, I'm glad that it's useful for you!

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

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

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?

You could use the UserAccessor 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>

Wow, that's amazing. How about a column that adds in when the account was created?

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>

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

You should be able to use the formatGivenString(pattern, date) method of the DateFormatter. Something like this should work:

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

Mucho gracias. Very helpful.

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

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

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

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

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 for more details.

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

Hi Remo and Thomas!

It works thank you very much!!

Rumi

Remo you're a life saver!

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.

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

For crowd see this answer, just below your comment...

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

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>

If I modify the code to include that change, will it still work for non-Crowd users?

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.

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

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

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?

Try the following:

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

Thanks a lot, that is what I was missing.

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

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.

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.

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

 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;

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.

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.

Hope this helps

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

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.

You could use Bob Swift's Table-plus plugin to get a sortable table.

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

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!

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

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

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

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

Cheers, Stefan

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?

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

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}

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

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
Asked Thursday in Confluence

What are the resources that you use to learn more about Atlassian Products?

I am gathering information about resources available for Atlassian product knowledge transferring for a presentation in our local Atlassian User Group. I want to group them in four categories From ...

165 views 5 7
View question

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