Last Login Users data

I have been using the above with a custom Report to see who has been logging in but I realized that without any indication of how long they were logged in, it was incomplete information.

So, I added another field for collecting logout data added a script to populate that, and and edited the report to show both. It provides a more useful set of information. See new, complete code, below.

If anyone has figured out how to store this information in a table so we can view a history of the login/logout per User, feel free to add to this. I know it could be done by direct access to the sql code, but I am hoping to stay within the $bean-> structure.

So, to track and report on user login/logout activity:

  1. Create new fields in the User module to store the most recent login/logout per User
  2. Add the new fields to the Editview and Detailview of the User module
  3. Add script to populate the fields on login and logout
  4. Create a Report to show the most recent login and logout by User
  1. Create new fields in the User module to store the most recent login/logout per User
    Using the SuiteCRM Web Interface: SuiteCRM -> Admin -> Studio -> Users (module)

Fields -> Add
	Data Type: Date/Time
	Name: userlastlogin   (when it is stored, SuiteCRM will call it userlastlogin_c to show it was a user-custom field)
	Display Label: Last Login:
	Inline Edit: UnCheck
	Importable: No
	(wait, it takes a while)
Fields -> Add
	Data Type: Date/Time
	Name: userlastlogout   (when it is stored, SuiteCRM will call it userlastlogout_c to show it was a user-custom field)
	Display Label: Last Logout:
	Inline Edit: UnCheck
	Importable: No
	(wait, it takes a while)
  1. Add the new fields to the Editview and Detailview of the User module
    Using the SuiteCRM Web Interface: SuiteCRM -> Admin -> Studio -> Users (module) -> Layouts -> EditView

	Sync to Detail view: Check
	Scroll Down to row with "Address Country" taking full width and shrink it to 1/2 width
	Add a row after the "Address Country" row
	Drag and drop "Last Login" field to new row then shrink to 1/2 width
	Drag and drop "Last Logout" field to new row on right side

Using the CLI

cd {root directory of SuiteCRM installation}
cd custom/modules/Users/metadata
nano editviewdefs.php
	Change
		array (
		  0 => 'address_country',
		  1 =>
		  array (
			'name' => 'userlastlogin_c',
			'label' => 'LBL_USERLASTLOGIN',
		  ),
		),
	to
		array (
		  0 => 'address_country',
		  1 =>
		  array (
			'name' => 'userlastlogin_c',
			'label' => 'LBL_USERLASTLOGIN',
			'type' => 'readonly',
		  ),
		),
	and
		array (
		  0 => 'address_country',
		  1 =>
		  array (
			'name' => 'userlastlogout_c',
			'label' => 'LBL_USERLASTLOGOUT',
		  ),
		),
	to
		array (
		  0 => 'address_country',
		  1 =>
		  array (
			'name' => 'userlastlogout_c',
			'label' => 'LBL_USERLASTLOGOUT',
			'type' => 'readonly',
		  ),
		),
  1. Add script to populate the fields on login and logout
    Using the CLI
cd {root directory of SuiteCRM installation}
cd custom/modules/Users
nano record_user_login.php
	<?php

	if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

	class record_user_login_class
	{
			function record_user_login_method($bean, $event, $arguments)
			{
					global $timedate;
					$bean->userlastlogin_c = $timedate->nowDb();
					$bean->save();
			}
	}

	?>
nano record_user_logout.php
	<?php

	if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

	class record_user_logout_class
	{
			function record_user_logout_method($bean, $event, $arguments)
			{
					global $timedate;
					$bean->userlastlogout_c = $timedate->nowDb();
					$bean->save();
			}
	}

	?>
chown www-data:www-data record_user_login.php record_user_logout.php
chmod +x record_user_login.php record_user_logout.php
nano logic_hooks.php
	Change 
		<?php
		// Do not store anything in this file that is not part of the array or the hook version.  This file will
		// be automatically rebuilt in the future.
		 $hook_version = 1;
		$hook_array = Array();
		// position, file, function
		$hook_array['after_login'] = Array();
		$hook_array['after_login'][] = Array(1, 'SugarFeed old feed entry remover', 'modules/SugarFeed/SugarFeedFlush.php','SugarFeedFlush', 'flushStaleEntries');
		?>
	to  (ie add the last 2 lines)
		<?php
		// Do not store anything in this file that is not part of the array or the hook version.  This file will
		// be automatically rebuilt in the future.
		 $hook_version = 1;
		$hook_array = Array();
		// position, file, function
		$hook_array['after_login'] = Array();
		$hook_array['after_login'][] = Array(1, 'SugarFeed old feed entry remover', 'modules/SugarFeed/SugarFeedFlush.php','SugarFeedFlush', 'flushStaleEntries');
		$hook_array['after_login'][] = Array(98, 'Record last user login date and time', 'custom/modules/Users/record_user_login.php','record_user_login_class', 'record_user_login_method');
		$hook_array['before_logout'][] = Array(99, 'Record last user logout date and time', 'custom/modules/Users/record_user_logout.php','record_user_logout_class', 'record_user_logout_method');
		?>

Using the SuiteCRM web interface

	SuiteCRM -> Admin -> System (section) -> Repair -> Quick Repair and Rebuild
	(when the screen refreshes, scroll to the bottom in case it requires a manual sync of vardefs and database)

Using the CLI (maybe not necessary every time, but it never hurts to ensure correct permissions)

	cd {root directory of SuiteCRM installation}
	chown -R www-data:www-data .
	chmod -R 755 .
	chmod -R 775 cache custom modules themes data upload
	chmod 775 config_override.php 2>/dev/null

4)Create a Report to show the most recent login and logout by User
Using the SuiteCRM web interface

SuiteCRM -> All -> Reports -> Create Report
	Name: Last Login / Logout
	Module: Users
	Moule Tree: Users (select)
	Fileds: First Name, Last Name, User Name, Last Login, Last Logout

Logout
Login
SuiteCRM - All -> Reports -> Click on Last Login / Logout
It should now show your last login and logout (the ones you just did)

That’s it!

From now on, just go to Reports-> Last Login / Logout and you get a list of the most recent login and logout for each user.

You can infer that if the last login is after the last logout, the User is currently logged in, but that is not always true so only use it as an indicator.

2 Likes