Menu
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: Last Login Users data

Last Login Users data 1 year 10 months ago #52649

  • udaykiran
  • udaykiran's Avatar
  • Offline
  • New Member
  • Posts: 16
  • Karma: 0
Hi All,

I am using SuiteCRM 7.7.9 version and i want the list of last logged in users data.

thanks in advance.

Regards,
kiran.
Last Edit: 1 year 10 months ago by udaykiran.
The administrator has disabled public write access.

Last Login Users data 1 year 10 months ago #52687

  • pgr
  • pgr's Avatar
  • Offline
  • Administrator
  • Posts: 10481
  • Thank you received: 1563
  • Karma: 359
On a first look, I don't see that data in the database. The "users" table doesn't even seem to store the "last login time".

I suppose you could store that data using a Workflow, and surely you could with a logic_hook:

support.sugarcrm.com/Documentation/Sugar...ic_Hooks/User_Hooks/

Maybe there's an add-on already made for this? Try searching the SuiteCRM Store....

good luck

My SuiteCRM In-depth blog.
Thank you for always stating your SuiteCRM version, checking your logs, reading the Docs, and searching before you ask!
The administrator has disabled public write access.

Last Login Users data 1 year 10 months ago #52697

  • bmwtourer
  • bmwtourer's Avatar
  • Offline
  • Elite Member
  • Posts: 223
  • Thank you received: 23
  • Karma: 2
We tweaked the login information using logic hooks

We add cookies (for integration with another application) and also log last login
I added a custom field "userlastlogin" to the user record...
here is the code
<?php

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

class logic_hooks_class
{
    function after_login_method($bean, $event, $arguments)
    {
        //logic
        global $current_user, $timedate;
        setcookie("SuiteCRMUserID", $current_user->id, time()+360000, "/" );
        setcookie("AccountingValidCies", $current_user->accesstocies_c,time()+360000, "/")        ;

        $focus = new User();
        $focus->retrieve($current_user->id)
        $focus->userlastlogin_c = $timedate->getInstance()->nowDb();
        $focus->save();
        
    }

    function login_failed_method($bean, $event, $arguments)
    {
        //logic
        setcookie("SuiteCRMUserID",  " login failed ", time()-36000,"/");

    }

    function before_logout_method($bean, $event, $arguments)
    {
        //logic
        setcookie("SuiteCRMUserID",  " bye! ", time()-36000,"/");


    }
}

?>
The administrator has disabled public write access.
The following user(s) said Thank You: pgr, mmendes, 92641fa944, udaykiran

Last Login Users data 5 months 4 weeks ago #76664

  • Ramblin
  • Ramblin's Avatar
  • Offline
  • Junior Member
  • Posts: 29
  • Thank you received: 5
  • Karma: 0
bmwtourer wrote:
We tweaked the login information using logic hooks

Thank you for posting this

I am looking to do something similar.

Were do you put this php file? I am on SuiteCRM 7.10 if it maters.

Also, is there a way to keep a running record of logins / logouts for each user? I am assuming a new database field connected to the user and a means of displaying it. If you have answers to both, great, but for now could work with just the database setup and php file code and use a database query tool to see the results.

Have you ever done this?
The administrator has disabled public write access.

Last Login Users data 5 months 4 weeks ago #76666

  • bmwtourer
  • bmwtourer's Avatar
  • Offline
  • Elite Member
  • Posts: 223
  • Thank you received: 23
  • Karma: 2
I am happy to help,
find documentation on logic hooks there are plenty
you put it in /custom/modules/users...
historical information?, yes you will need a new table linked to the user table...
The administrator has disabled public write access.

Last Login Users data 5 months 4 weeks ago #76673

  • Ramblin
  • Ramblin's Avatar
  • Offline
  • Junior Member
  • Posts: 29
  • Thank you received: 5
  • Karma: 0
bmwtourer wrote:
I am happy to help,
you put it in /custom/modules/users...

Well, the good news is, I did not crash the system :-)

The bad news is, I am not getting the field populated.

I added the field and made it readonly
I added the field to both the editviewdefs and detailviewdefs layouts
I added the script (modified per below since I only needed the login field, not the cookies)
I Repair and Rebuild the system

So the field does show on the layouts and the field is readonly, but when the user logs in and out, and back in again, no data is shown in the new field.

Any ideas what I am missing?

See below for what I did.

Using the SuiteCRM GUI as Admin
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 Date/Time:
	Inline Edit: UnCheck
	Importable: No
Layouts -> EditView
	Sync to Detail view: Check
	Scroll Down to row with Address Country taking full width and shrink to 1/2 width
	Drag and drop "Last Login Date/Time:" filed to space opened up

From 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',
				  ),
				),
cd {root directory of SuiteCRM installation}
cd custom/modules/Users
nano record-last-login-date-time-for-user.php
	<?php

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

	class logic_hooks_class
	{
		function after_login_method($bean, $event, $arguments)
		{
			global $current_user, $timedate;
			$focus = new User();
			$focus->retrieve($current_user->id)
			$focus->userlastlogin_c = $timedate->getInstance()->nowDb();
			$focus->save();
			
		}
	}

	?>

From the SuiteCRM GUI
SuiteCRM -> Admin -> System (section) -> Repair -> Quick Repair and Rebuild

From the CLI
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

But, as I said, the login date/time is not being populated to the field.

Any ideas?
The administrator has disabled public write access.

Last Login Users data 5 months 4 weeks ago #76675

  • Ramblin
  • Ramblin's Avatar
  • Offline
  • Junior Member
  • Posts: 29
  • Thank you received: 5
  • Karma: 0
bmwtourer wrote:
you put it in /custom/modules/users...

OK,

Apparently, in addition to creating the file that does the action, you have to actually link that file to the hook.

So, do all the above and then ...

I have seen two versions of how that is done, but neither one is working for me right now.

Version 1:
The file at /var/www/html/suiteayudacrm/custom/modules/Users/logic_hooks.php curently contains
<?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');
?>

so I added to that
$hook_array['after_login'][] = Array(99, 'Record last user login date and time', 'logic_hooks_class.php','logic_hooks_class', 'after_login_method');

Making the full file now
<?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(99, 'Record last user login date and time', 'logic_hooks_class.php','logic_hooks_class', 'after_login_method');
?>

I also changed the name of the file from record-last-login-date-time-for-user.php to logic_hooks_class.php

After a Quick Repair and a login/logout/login still no entry in the new field


Version 2
There was another post that said to add entries using the extensions framework, which appeared to be talking about a way to add logic hook actions and protect the edits from future upgrades wiping them out, but I could not really follow the whole code suggestions there.

So I am close, but no cigar.

Any ideas?
The administrator has disabled public write access.

Last Login Users data 5 months 4 weeks ago #76678

  • Ramblin
  • Ramblin's Avatar
  • Offline
  • Junior Member
  • Posts: 29
  • Thank you received: 5
  • Karma: 0
bmwtourer wrote:
find documentation on logic hooks there are plenty.

In case it matters, I did manually add to the SQL database an entry for userlastlogin_c for one of my users and when I brought up that user's profile, it does show properly, so the issue is not the field or layout edits; it is the logic of my coding.

At this point I have exhausted my level of expertise (I know, that did not take much) so any help would be appreciated.
Last Edit: 5 months 4 weeks ago by Ramblin. Reason: typos
The administrator has disabled public write access.

Last Login Users data 5 months 4 weeks ago #76679

  • Ramblin
  • Ramblin's Avatar
  • Offline
  • Junior Member
  • Posts: 29
  • Thank you received: 5
  • Karma: 0
One thing I did figure out

The path I declared to the file logic_hooks_class.php assumed the current working directory was the same directory as the calling file but it is not. Curse those relative paths ...

So I edited the path to the logic_hooks_class.php file and made it relative to the SutieCRM root directory as in
$hook_array['after_login'][] = Array(99, 'Record last user login date and time', 'custom/modules/Users/logic_hooks_class.php','logic_hooks_class', 'after_login_method');

I think that did now make a difference and I think the file is now being called since it did change how I have to login; I get a blank screen then refresh the URL and I am in.

The userlastlogin_c field is NOT being populated though.

So although that is not in itself good, at least I now think the hook is calling the file.

But now I need to correct what the code in the file is doing.

Hopefully you have some ideas.
Last Edit: 5 months 4 weeks ago by Ramblin.
The administrator has disabled public write access.

Last Login Users data 5 months 3 weeks ago #76685

  • pgr
  • pgr's Avatar
  • Offline
  • Administrator
  • Posts: 10481
  • Thank you received: 1563
  • Karma: 359
You're not supposed to add the field on the database, you should do it from Studio (or manually in the vardefs).

When you do a QR&R, you need to scroll to the bottom and click a button that says "sync database with vardefs" if there is one there. Did you check this?

My SuiteCRM In-depth blog.
Thank you for always stating your SuiteCRM version, checking your logs, reading the Docs, and searching before you ask!
The administrator has disabled public write access.

Last Login Users data 5 months 3 weeks ago #76687

  • Ramblin
  • Ramblin's Avatar
  • Offline
  • Junior Member
  • Posts: 29
  • Thank you received: 5
  • Karma: 0
pgr wrote:
You're not supposed to add the field on the database, you should do it from Studio (or manually in the vardefs).

I did add the field using Studio and it does show properly.

I manually added DATA to the database to check if it was displaying properly in the layout, which it is.

The issue is getting the script, triggered by the after_login hook, to populate the database, which it is currently NOT doing.
The administrator has disabled public write access.

Last Login Users data 5 months 3 weeks ago #76688

  • pgr
  • pgr's Avatar
  • Offline
  • Administrator
  • Posts: 10481
  • Thank you received: 1563
  • Karma: 359
Ah ok, sorry.

Can you check the returns from each function, to see which part is not working? The "retrieve", etc.

My SuiteCRM In-depth blog.
Thank you for always stating your SuiteCRM version, checking your logs, reading the Docs, and searching before you ask!
The administrator has disabled public write access.

Last Login Users data 5 months 3 weeks ago #76692

  • Ramblin
  • Ramblin's Avatar
  • Offline
  • Junior Member
  • Posts: 29
  • Thank you received: 5
  • Karma: 0
bmwtourer wrote:
I am happy to help,

I have it working!

Thank you for pointing me in the right direction.

So, in one posting, here is what I did in SuiteCRM 7.10.11 to have the User->Profile page display a field showing when the User last logged in

Using the SuiteCRM GUI (this part I had correct all along - Thanks to the Sugar?Suite CRM development folks ...)
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 Date/Time:
	Inline Edit: UnCheck
	Importable: No
Layouts -> EditView
	Sync to Detail view: Check
	Scroll Down to row with "Address Country" taking full width and shrink it to 1/2 width
	Drag and drop "Last Login Date/Time:" field to space opened up

From the CLI (had to change this part)
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',
		  ),
		),
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();
			}
	}

	?>
cd {root directory of SuiteCRM installation}
cd custom/modules/Users
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 line)
		<?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(99, 'Record last user login date and time', 'custom/modules/Users/record_user_login.php','record_user_login_class', 'record_user_login_method');
		?>

Back to the SuiteCRM GUI
SuiteCRM -> Admin -> System (section) -> Repair -> Quick Repair and Rebuild

And back to 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

It works!

Thanks to all who posted code snippets in this and other forums.

Open Source RULES!
The administrator has disabled public write access.
The following user(s) said Thank You: pgr

Last Login Users data 2 months 3 weeks ago #82244

  • Ramblin
  • Ramblin's Avatar
  • Offline
  • Junior Member
  • Posts: 29
  • Thank you received: 5
  • Karma: 0
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)

2) 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',
		  ),
		),


3) 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.
The administrator has disabled public write access.
The following user(s) said Thank You: pgr

Last Login Users data 2 months 3 weeks ago #82287

  • pgr
  • pgr's Avatar
  • Offline
  • Administrator
  • Posts: 10481
  • Thank you received: 1563
  • Karma: 359
Thanks, once again. You might like to know that I have this post referenced here so one day we can start moving more of these things into our Documentation Site (Technical blog) section.

github.com/salesagility/SuiteDocs/issues/206

My SuiteCRM In-depth blog.
Thank you for always stating your SuiteCRM version, checking your logs, reading the Docs, and searching before you ask!
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Time to create page: 0.057 seconds
Powered by Kunena Forum