It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to find last login date of users in Confluence

Hi,

Is there a way I can tell the last login date of all users in one screen? I know I can see it by clicking on the user name. However I need to look at over 1500 users and see who has not logged in for a while to be able to deactive these users. We are using Confluence 3.5.17

I have tried some of the earlier posted user macros but the result is either empyt field for last login daye, NEVER or the userlist is showing only ~20 users of the 1500.

Would also need to get the date when the account was created and a tab showing if the user is active or deactivated.

Thanks, Kjell

7 answers

I created a user macro with the help of some answers here https://answers.atlassian.com/questions/33385/macro-that-produces-a-list-of-users-last-login-date that creates a table with the Name, Create Date, Last Login and number of days since last login (roughly because of the time being in milliseconds).

## @param group:title=Gruppe|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($t1=$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 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.getUser($username))
        #set($crowdUser = $crowdService.getUser($user.name))
        
        <tr>
           <td class="confluenceTd">#usernameLink($user.name)</td>
           <td class="confluenceTd" style="text-align:center;">$action.dateFormatter.formatGivenString('dd.MM.yyyy', $crowdUser.createdDate)</td>
           <td class="confluenceTd" style="text-align:center;">$action.dateFormatter.formatGivenString('dd.MM.yyyy',$loginManager.getLoginInfo($user).lastSuccessfulLoginDate)</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">Nein</ac:parameter>
                   </ac:macro>
           #else
                   <ac:macro ac:name="status">
                   <ac:parameter ac:name="colour">Green</ac:parameter>
                   <ac:parameter ac:name="title">Ja</ac:parameter>
                   </ac:macro>
                  #set($totalactiveusers = $totalactiveusers + 1)
            #end
                

            </td>
         
          #if($loginManager.getLoginInfo($user).lastSuccessfulLoginDate)
                #set($t3= $content.currentDate.time -  $loginManager.getLoginInfo($user).lastSuccessfulLoginDate.time)
                #set($t3=$t3/1000/24/60/60)
           #end
           <td class="confluenceTd" style="text-align:center;">
           #if($t3 < 7) 
                 <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 < 14) 
                 <ac:macro ac:name="status">
                 <ac:parameter ac:name="colour">Yellow</ac:parameter>
                 <ac:parameter ac:name="title">$t3</ac:parameter>
                 </ac:macro>
           #elseif($t3 < 9999) 
                 <ac:macro ac:name="status">
                 <ac:parameter ac:name="colour">Red</ac:parameter>
                 <ac:parameter ac:name="title">$t3</ac:parameter>
                 </ac:macro>
           #else 
                 <ac:macro ac:name="status">
                 <ac:parameter ac:name="colour">Magenta</ac:parameter>
                 <ac:parameter ac:name="title">$t3</ac:parameter>
                 </ac:macro>

           #end 
           
           </td>
        </tr>
    #end
</table>
#end

You can give the group-name as parameter or you use confluence-users to show all users. Hope this helps.

Bruce

what if one wants to know the user creation date. I know these:

$user.fullName
$user.email
$user.name

But, is there a place where I can see the full list of parameters?

Thanks, Bruce. This macro made my day. :)

Many thanks, Bruce. The macro is great, it helps us to get information from different angles.

You can get them from the databse with this query:

select cu.user_name, cua.attribute_value from cwd_user_attribute cua left join cwd_user cu on cua.user_id=cu.id where attribute_name='lastAuthenticated';

The time is in unix format. Yoy may want to write a script to change that to a more human readable format.

This appears to only work for users in Confluence's local user directory, but not any users who authenticate through an external LDAP server (delegated LDAP auth).

Does Confluence no longer save the last login time for these users? (It used to work in 3.4.x -- the login date was stored in the OS_PROPERTYENTRY table under entity key "confluence.user.last.login.date")

The following works for me on Confluence 5.5 using Postgres. They perhaps changed the structure of the database. There are other examples in the Confluence docs:

SELECT cwd_user.user_name, to_timestamp(CAST(cwd_user_attribute.attribute_value as bigint)/1000)
FROM cwd_user, cwd_user_attribute
WHERE cwd_user_attribute.user_id = cwd_user.id
AND cwd_user_attribute.attribute_name = 'lastAuthenticated'
AND cwd_user.active='T'
ORDER BY to_timestamp;

This keys in on active users, you can take that AND clause out if you want to include inactive users. We use a mix of LDAP and a few local users and everyone seems to be in there.

I run a free SQL embedding plugin (check the store for Pocket Queries) and throw this list into a velocity template that was mostly stolen from Bruce. If you have a lot of users the SQL query runs faster than the full macro.

For good measure, this is a query for Jira 6.2.2 with a Postgres backend:

SELECT cwd_user.user_name, to_timestamp(CAST(cwd_user_attributes.attribute_value as bigint)/1000)
FROM cwd_user, cwd_user_attributes
WHERE cwd_user_attributes.user_id = cwd_user.id
AND cwd_user_attributes.attribute_name = 'lastAuthenticated'
AND cwd_user.active = '1'
ORDER BY to_timestamp;

It's weird, the macro is using $crowdUser.createdDate, but https://developer.atlassian.com/static/javadoc/embedded-crowd-api/latest/reference/com/atlassian/crowd/embedded/api/User.html doesn't contain the createdDate method. Anyone know where is that createdDate method from? I am trying to find the API equivalent. Thanks.

This is a GREAT macro - thank you very much @Bruce Schlueter 

A couple of updates for current Confluence version:

  1. Substituted escaped characters from the original (e.g., &lt;  for '<')
  2. Suggested options when creating:
    1. Visibility - Visible only to system administrators in the Macro Browser
    2. Definition of User Macro - Rendered

## @param group:title=Gruppe|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($t1=$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 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.getUser($username))
#set($crowdUser = $crowdService.getUser($user.name))

<tr>
<td class="confluenceTd">#usernameLink($user.name)</td>
<td class="confluenceTd" style="text-align:center;">$action.dateFormatter.formatGivenString('dd.MM.yyyy', $crowdUser.createdDate)</td>
<td class="confluenceTd" style="text-align:center;">$action.dateFormatter.formatGivenString('dd.MM.yyyy',$loginManager.getLoginInfo($user).lastSuccessfulLoginDate)</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">Nein</ac:parameter>
</ac:macro>
#else
<ac:macro ac:name="status">
<ac:parameter ac:name="colour">Green</ac:parameter>
<ac:parameter ac:name="title">Ja</ac:parameter>
</ac:macro>
#set($totalactiveusers = $totalactiveusers + 1)
#end

</td>

#if($loginManager.getLoginInfo($user).lastSuccessfulLoginDate)
#set($t3= $content.currentDate.time - $loginManager.getLoginInfo($user).lastSuccessfulLoginDate.time)
#set($t3=$t3/1000/24/60/60)
#end
<td class="confluenceTd" style="text-align:center;">
#if($t3 < 7)
<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 < 14)
<ac:macro ac:name="status">
<ac:parameter ac:name="colour">Yellow</ac:parameter>
<ac:parameter ac:name="title">$t3</ac:parameter>
</ac:macro>
#elseif($t3 < 9999)
<ac:macro ac:name="status">
<ac:parameter ac:name="colour">Red</ac:parameter>
<ac:parameter ac:name="title">$t3</ac:parameter>
</ac:macro>
#else
<ac:macro ac:name="status">
<ac:parameter ac:name="colour">Magenta</ac:parameter>
<ac:parameter ac:name="title">$t3</ac:parameter>
</ac:macro>

#end

</td>
</tr>
#end
</table>
#end

@Jack D. Pond @Bruce Schlueter , you saved me! Thank you for the sharing!

It's Amazing but just a quick request 

can you display the Username also ?? 

I haven't actually tested this, but . . .

After:

<th class="confluenceTh " width="220px">Name</th>

add 

<th class="confluenceTh " width="110px">UserName</th>

 and after

<td class="confluenceTd">#usernameLink($user.name)</td>

add

<td class="confluenceTd">$username</td>

Hope this works! 

Oh man you are the Gandalf of the Macro :D  .

Thanks a lot it's Work  

The macro doesn't seem to be working for me on Confluence 7.4.0. For everyone, "Create Date" and "Last Login" are blank, and "Days" is 9999.

Edit -- I see now that there's actually no way to fix the macro in Confluence 7.x:

https://confluence.atlassian.com/doc/deprecated-code-paths-removed-in-7-0-967879993.html?_ga=2.220289368.1533145928.1575273676-803821957.1552482208

Isn't there any replacement for the removed functions in Confluence 7.x?

I know this is an old thread, but I'm pretty sure someone will find it handy to have a SQL that works with recursive (LDAP) groups. It requires a database with Common Table Expressions, e.g. MS SQLServer

-- CTE (Common Table Expression)
WITH recursive_groups ( group_id, name, directory_id)
AS
(
	-- Anchor, i.e. the login groups
	SELECT
		g.id, group_name, directory_id
	FROM
		cwd_group g
		JOIN cwd_directory d ON (g.directory_id = d.id)
	WHERE
		d.active = 'T' -- only enabled directories
		AND g.group_name in ('confluence-users-ldap','confluence-administrators','confluence-administrators-system') -- put your login groups here
	UNION ALL
	-- Recursive sub groups
	SELECT
		g.id, g.group_name, g.directory_id
	FROM
		cwd_membership m
		JOIN recursive_groups to_the_root ON (to_the_root.group_id = m.parent_id)
		JOIN cwd_group g ON (m.child_group_id = g.id)
		JOIN cwd_directory d ON (g.directory_id = d.id)
	WHERE
		d.active = 'T' -- only enabled directories
)
SELECT
	distinct u.id AS [UserId], u.lower_user_name AS [User], u.display_name AS FullName, l.SUCCESSDATE AS [LastLogin], du.directory_name AS DirectoryName
FROM
	recursive_groups g
	JOIN cwd_membership m ON (m.parent_id = g.group_id)
	JOIN cwd_user u ON (u.id = m.child_user_id)
	JOIN cwd_directory du ON (u.directory_id = du.id)
	LEFT JOIN user_mapping map ON (map.username = u.user_name)
	LEFT JOIN logininfo l ON (l.USERNAME = map.user_key)
WHERE
	u.active = 'T' -- only enabled users
ORDER BY
	u.lower_user_name

Hi Bruce

i tried the macro but it only list the users and the last login date comes empty , Could you let me know where i am doing wrong .

BR

Aakanksha 

same issue for me as well.. last login fields are empty for all users

Same issue here.  Create date and Last login fields are both empty. 

You might be interested in this improvement request on Atlassian's Confluence project:

https://jira.atlassian.com/browse/CONF-1556

You can both watch and vote on the issue as important to you as well as review the SQL statement provided in the comments as a temporary work-around to your need.

Using Bruce's work, I have updated the version that I use and put it on GitHub: https://github.com/stevegoldberg/ConfluenceMacros/blob/master/Last%20Login/lastlogin.css

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Confluence

What project did you transition or start on Confluence with the shift to remote work?

It’s been great to hear from fellow users over the last few weeks about the best tips and fun moments you’ve had working on Confluence since the transition to working remote. I’d love to keep the c...

227 views 3 8
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you