public static void UpdateLastLogin( string userName )
{
if ( !string.IsNullOrWhiteSpace( userName ) && !userName.StartsWith( "rckipid=" ) )
{
using ( var rockContext = new RockContext() )
{
var userLoginService = new UserLoginService( rockContext );
var userLogin = userLoginService.GetByUserName( userName );
if ( userLogin != null )
{
userLogin.LastLoginDateTime = RockDateTime.Now;
if ( userLogin.PersonId.HasValue )
{
var summary = new System.Text.StringBuilder();
summary.AppendFormat( "User logged in with <span class='field-name'>{0}</span> username", userLogin.UserName );
if ( HttpContext.Current != null && HttpContext.Current.Request != null )
{
summary.AppendFormat( ", to <span class='field-value'>{0}</span>, from <span class='field-value'>{1}</span>",
HttpContext.Current.Request.Url.AbsoluteUri, HttpContext.Current.Request.UserHostAddress );
}
summary.Append( "." );
var historyService = new HistoryService( rockContext );
var personEntityTypeId = EntityTypeCache.Read( "Rock.Model.Person" ).Id;
var activityCategoryId = CategoryCache.Read( Rock.SystemGuid.Category.HISTORY_PERSON_ACTIVITY.AsGuid(), rockContext ).Id;
historyService.Add( new History
{
EntityTypeId = personEntityTypeId,
CategoryId = activityCategoryId,
EntityId = userLogin.PersonId.Value,
Summary = summary.ToString(),
Verb = "LOGIN"
} );
}
rockContext.SaveChanges();
}
}
}
}