API
Version 7.x > API > User Last Login Date View modes: 
User avatar
Kentico MVP
Kentico MVP
Brenden Kehren - 1/17/2013 1:00:36 PM
   
User Last Login Date
Since this field updates real-time, I'd like to add a new field on the User Settings that when a user logs in, it updates that user setting field with the old last login date before that date is updated. Very similar functionality as you'd see in a banking site. Say I login today at 8am but my last time I logged in was 10 days ago, the label would show the 10 days ago date and not this morning at 8am. Then when I log in tomorrow, it would show I logged in at 8am the previous day. Can you point me in the right direction of the API so I can override the method?

User avatar
Kentico Support
Kentico Support
kentico_janh - 1/18/2013 8:43:29 AM
   
RE:User Last Login Date
Hello,

It is not necessary to store that value to some extra table/column. You can easily take the last login datetime and store it to a session and work with this value instead, because you don't have to know this datetime after session expires (log off, close browser, etc.)

Best regards,
Jan Hermann

User avatar
Kentico MVP
Kentico MVP
Brenden Kehren - 1/18/2013 8:46:46 AM
   
RE:User Last Login Date
I could although when does the LastLoginDate property get changed? When the user is authenticated? If so, I need to override a method to get the old value before the current date/time is written, what method do I override or do I have to create my own? I'm all for the session variable, just not sure where to implement it.

User avatar
Kentico Support
Kentico Support
kentico_janh - 1/21/2013 8:33:40 AM
   
RE:User Last Login Date
Hello,

Oh ok, you can call your own global event over an object before gets updated:

        public override void Init()
{
// Assigns custom handlers to the appropriate events
ObjectEvents.Update.Before += User_Authenticate_Before;
}

private void User_Authenticate_Before(object sender, ObjectEventArgs e)
{
// Add custom actions here
if (e.Object.ObjectType.ToLower() == "cms.user") {
...
}
}


Please follow the link below to our documentation where you can find information about global events:

http://devnet.kentico.com/docs/devguide/index.html?event_handlers_overview.htm

Best regards,
Jan Hermann

User avatar
Kentico MVP
Kentico MVP
Brenden Kehren - 1/24/2013 8:34:42 AM
   
RE:User Last Login Date
That will be very helpful with other items. I've attempted to implement this as you mention and have been less than successful. It, runs an infinate loop. All I want to do is when someone logs in, I want to the the LastLogon date (before it is updated) and save it in a custom field in the user settings table. Also this code is showing the user has already been updated so the Authentication process must be updating that LastLogon property before the Object.Update.Before event is fired. Any thoughts on this code?
private void User_Authenticate_Before(object sender, ObjectEventArgs e)
{
if (e.Object.ObjectType.ToLower() == "cms.user")
{
// update the user settings to save the previous login date
// get the settings for the current user
UserInfo ui = UserInfoProvider.GetUserInfo(e.Object.GetIntegerValue("UserID", -1));
if (ui != null)
{
// probably need to compare the dates so its not updating everytime the user object is updated.
UserSettingsInfo usi = UserSettingsInfoProvider.GetUserSettingsInfo(ui.UserSettings.UserSettingsID);
usi.SetValue("UserLastAccountActivity", ValidationHelper.GetDateTime(ui.LastLogon, DateTime.Now));
UserSettingsInfoProvider.SetUserSettingsInfo(usi);
}
}
}

User avatar
Kentico Support
Kentico Support
kentico_janh - 1/24/2013 9:06:05 AM
   
RE:User Last Login Date
Hello,

In the code the e.Object contains values which will be stored in the database, however they are not yet, so you can take it directly from the database. The reason why are you getting the endless loop is that you are updating/setting a new value to a user so it calls your code over and over again. This is also the reason why I suggest you to store that last login to a session instead.

Best regards,
Jan Hermann

User avatar
Kentico MVP
Kentico MVP
Brenden Kehren - 1/24/2013 10:00:55 AM
   
RE:User Last Login Date
I did add a simple check to end the loop.

I'm still confused as to why the my instance of the UserInfo object (ui) already has the updated values when the update shouldn't have even completed yet. This line of code
UserInfo ui = UserInfoProvider.GetUserInfo(e.Object.GetIntegerValue("UserID", -1));
is getting an instance of UserInfo object based on the e.Object.UserID and has nothing to do with the LastLogon from the e.Object. By all rights, the ui object should have the old data from the database while the e.Object should have the new data from the form/page.

User avatar
Kentico Support
Kentico Support
kentico_janh - 1/29/2013 9:37:58 AM
   
RE:User Last Login Date
Hello,

Yes, you are right, the user object is already updated, however that change is not stored in the database yet, so you would need to get the LastLogon value directly from the CMS_User table.

Best regards,
Jan Hermann

User avatar
Kentico MVP
Kentico MVP
Brenden Kehren - 5/15/2013 9:40:05 AM
   
RE:User Last Login Date
Hi Jan, sorry to bring the old post back to life but I'm back to this requirement and have learned quite a bit since the last post and was able to get it working with a few simple lines of code and thought I'd share.
public override void Init()
{
SecurityEvents.Authenticate.Execute += Authenticate_Execute;
}

private void Authenticate_Execute(object sender, AuthenticationEventArgs e)
{
UserInfo ui = e.User;
UserSettingsInfo usi = UserSettingsInfoProvider.GetUserSettingsInfoByUser(ui.UserID);
usi.SetValue("UserLastAccountActivity", ui.LastLogon);
UserSettingsInfoProvider.SetUserSettingsInfo(usi);
}

User avatar
Certified Developer 9
Certified Developer 9
r.zareianfard-gmail - 12/28/2013 4:29:33 PM
   
RE:User Last Login Date
Hi dear,
I have the same issue, please check this thread and answer if you have any solution :

http://devnet.kentico.com/Forums/f65/t42847/Global-Events-Causes-Loop.aspx