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

5 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.

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

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
Community showcase
Published Nov 29, 2018 in New to Confluence

How to use Confluence Cloud for stakeholder management

Most of us don’t need much convincing that stakeholder management is important. It just makes sense that keeping everyone in-the-know on projects and assigning clearly defined roles is key to having ...

1,168 views 4 6
Read article

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