120 lines
4.5 KiB
C#
120 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using EnVisage.Models;
|
|
using NLog;
|
|
using EnVisage.Properties;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNet.Identity;
|
|
using Microsoft.AspNet.Identity.EntityFramework;
|
|
using EnVisage.Code.Cache;
|
|
using System.Text;
|
|
using System.Data.Entity;
|
|
|
|
namespace EnVisage.Code.BLL
|
|
{
|
|
public class AccountManager
|
|
{
|
|
private readonly EnVisageEntities _dbContext;
|
|
private readonly bool _isContexLocal = false;
|
|
protected readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
protected EnVisageEntities DbContext { get { return _dbContext; } }
|
|
protected bool IsContextLocal
|
|
{
|
|
get { return _isContexLocal; }
|
|
}
|
|
|
|
public AccountManager(EnVisageEntities dbContext)
|
|
{
|
|
if (dbContext == null)
|
|
{
|
|
_dbContext = new EnVisageEntities();
|
|
_isContexLocal = true;
|
|
}
|
|
else
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
}
|
|
public void Dispose()
|
|
{
|
|
if (_isContexLocal)
|
|
_dbContext.Dispose();
|
|
}
|
|
|
|
#region Public Methods
|
|
|
|
public async Task<IdentityResult> ChangePasswordAsync(string userId, string currentPassword, string newPassword)
|
|
{
|
|
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(DbContext));
|
|
return await userManager.ChangePasswordAsync(userId, currentPassword, newPassword);
|
|
}
|
|
|
|
public static bool UserByEmailExists(string email, string excludeUserId)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(email.Trim()))
|
|
return false;
|
|
var searchValue = email.Trim();
|
|
using (var dbContext = new EnVisageEntities())
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(excludeUserId))
|
|
return dbContext.AspNetUsers.Any(x => x.Email == searchValue && x.Id != excludeUserId);
|
|
else
|
|
return dbContext.AspNetUsers.Any(x => x.Email == searchValue);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Finds a people resource with the same email as for specified user.
|
|
/// </summary>
|
|
/// <param name="userId">An id of the user which email to search for in people resources.</param>
|
|
/// <returns><b>Null</b> - if there is no such user or resource, otherwise - returns PeopleResource.Id of returned people resource.</returns>
|
|
public Guid? GetCurrentResourceId(string userId)
|
|
{
|
|
var resourceId = (from u in DbContext.AspNetUsers
|
|
join r in DbContext.PeopleResources on u.Email equals r.Email
|
|
where u.Id == userId
|
|
select r.Id).FirstOrDefault();
|
|
return Guid.Empty.Equals(resourceId) ? (Guid?)null : resourceId;
|
|
}
|
|
|
|
public void SetLoginDates(string userIdAsText)
|
|
{
|
|
Guid userId = Guid.Empty;
|
|
if (Guid.TryParse(userIdAsText, out userId))
|
|
{
|
|
var user = new UsersCache().Value.FirstOrDefault(t => t.Id == userId);
|
|
if (user != null)
|
|
{
|
|
if (!user.LoginDate.HasValue || (user.LoginDate.HasValue && user.LoginDate.Value.Date != DateTime.Today))
|
|
{
|
|
var aspNetUser = DbContext.AspNetUsers.Where(x => x.Id == user.Id.ToString()).FirstOrDefault();
|
|
if (aspNetUser != null)
|
|
{
|
|
if (user.LoginDate.HasValue)
|
|
aspNetUser.LastLoginDate = user.LoginDate;
|
|
aspNetUser.LoginDate = DateTime.Now;
|
|
DbContext.Entry(aspNetUser).State = EntityState.Modified;
|
|
DbContext.SaveChanges();
|
|
(new UsersCache()).Invalidate();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
public class ApplicationUserManager : UserManager<ApplicationUser>
|
|
{
|
|
public ApplicationUserManager()
|
|
: base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
|
|
{
|
|
this.PasswordValidator = new EnVisage.Code.Validation.PrevuPasswordValidator(6, 30);
|
|
}
|
|
public override Task<IdentityResult> CreateAsync(ApplicationUser user)
|
|
{
|
|
return base.CreateAsync(user);
|
|
}
|
|
}
|
|
} |