Last active
August 29, 2015 14:05
-
-
Save YoungjaeKim/aa13712c8f97c324ecbd to your computer and use it in GitHub Desktop.
ASP.NET UserStore for Telerik DataAccess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// UserStore for Telerik DataAccess. | |
/// </summary> | |
public class TelerikUserStore : IUserStore<AspNetUser, int>, IUserPasswordStore<AspNetUser, int>, IUserClaimStore<AspNetUser, int>, | |
IUserRoleStore<AspNetUser, int>, IUserSecurityStampStore<AspNetUser, int>, IUserLoginStore<AspNetUser, int>, IUserLockoutStore<AspNetUser, int>, | |
IUserEmailStore<AspNetUser, int>, IUserPhoneNumberStore<AspNetUser, int>, IUserTwoFactorStore<AspNetUser, int> | |
{ | |
private bool _disposed; | |
public TelerikUserStore() | |
{ | |
this.DisposeContext = true; | |
} | |
public TelerikUserStore(DataAccessEntity context) | |
{ | |
if (context == null) | |
{ | |
throw new ArgumentNullException("context"); | |
} | |
this.Context = context; | |
this.AutoSaveChanges = true; | |
this.DisposeContext = true; | |
} | |
public DataAccessEntity Context { get; set; } | |
public bool AutoSaveChanges { get; set; } | |
public bool DisposeContext { get; set; } | |
public void Dispose() | |
{ | |
this.Dispose(true); | |
} | |
protected virtual void Dispose(bool disposing) | |
{ | |
if (this.DisposeContext && disposing && this.Context != null) | |
this.Context.Dispose(); | |
this._disposed = true; | |
this.Context = null; | |
} | |
public async Task CreateAsync(AspNetUser user) | |
{ | |
this.ThrowIfDisposed(); | |
if (user == null) | |
throw new ArgumentNullException("user"); | |
this.Context.Add(user); | |
await this.SaveChanges(); | |
} | |
public async Task UpdateAsync(AspNetUser user) | |
{ | |
this.ThrowIfDisposed(); | |
if (user == null) | |
throw new ArgumentNullException("user"); | |
await this.SaveChanges(); | |
} | |
public async Task DeleteAsync(AspNetUser user) | |
{ | |
this.ThrowIfDisposed(); | |
if (user == null) | |
throw new ArgumentNullException("user"); | |
this.Context.Delete(user); | |
await this.SaveChanges(); | |
} | |
public Task<AspNetUser> FindByIdAsync(int userId) | |
{ | |
this.ThrowIfDisposed(); | |
var result = Task.FromResult(this.Context.AspNetUsers.SingleOrDefault(o => o.Id == userId)); | |
return result; | |
} | |
public Task<AspNetUser> FindByNameAsync(string userName) | |
{ | |
this.ThrowIfDisposed(); | |
return Task.FromResult(this.Context.AspNetUsers.SingleOrDefault(o => String.Compare(o.UserName, userName, true) == 0)); | |
} | |
public Task SetPasswordHashAsync(AspNetUser user, string passwordHash) | |
{ | |
this.ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
user.PasswordHash = passwordHash; | |
return Task.FromResult<int>(0); | |
} | |
public Task<string> GetPasswordHashAsync(AspNetUser user) | |
{ | |
this.ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
return Task.FromResult<string>(user.PasswordHash); | |
} | |
public Task<bool> HasPasswordAsync(AspNetUser user) | |
{ | |
return Task.FromResult<bool>(user.PasswordHash != null); | |
} | |
public Task<IList<Claim>> GetClaimsAsync(AspNetUser user) | |
{ | |
this.ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
IList<Claim> claims = new List<Claim>(); | |
foreach (AspNetUserClaim claim in user.AspNetUserClaims) | |
{ | |
claims.Add(new Claim(claim.ClaimType, claim.ClaimValue)); | |
} | |
return Task.FromResult<IList<Claim>>(claims); | |
} | |
public Task AddClaimAsync(AspNetUser user, Claim claim) | |
{ | |
this.ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
IList<Claim> claims = new List<Claim>(); | |
foreach (AspNetUserClaim c in user.AspNetUserClaims) | |
{ | |
claims.Add(new Claim(c.ClaimType, c.ClaimValue)); | |
} | |
return Task.FromResult<IList<Claim>>(claims); | |
} | |
public Task RemoveClaimAsync(AspNetUser user, Claim claim) | |
{ | |
this.ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
if (claim == null) | |
{ | |
throw new ArgumentNullException("claim"); | |
} | |
List<AspNetUserClaim> list = user.AspNetUserClaims.Where(o => | |
{ | |
if (o.ClaimValue != claim.Value) | |
{ | |
return false; | |
} | |
return o.ClaimType == claim.Type; | |
}).ToList(); | |
foreach (AspNetUserClaim identityUserClaim in list) | |
{ | |
this.Context.Delete(identityUserClaim); | |
} | |
return Task.FromResult<int>(0); | |
} | |
public async Task AddToRoleAsync(AspNetUser user, string roleName) | |
{ | |
this.ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
if (string.IsNullOrWhiteSpace(roleName)) | |
{ | |
throw new ArgumentException("Value Cannot Be Null Or Empty", "roleName"); | |
} | |
AspNetRole identityRole = this.Context.AspNetRoles.FirstOrDefault(o => o.Name == roleName); | |
if (identityRole == null) | |
{ | |
throw new InvalidOperationException(string.Format("Role not found", roleName)); | |
} | |
identityRole.AspNetUsers.Add(user); | |
Context.Add(identityRole); | |
await SaveChanges(); | |
user.AspNetRoles.Add(identityRole); | |
} | |
public Task RemoveFromRoleAsync(AspNetUser user, string roleName) | |
{ | |
this.ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
if (string.IsNullOrWhiteSpace(roleName)) | |
{ | |
throw new ArgumentException("Value Cannot Be Null Or Empty", "roleName"); | |
} | |
AspNetRole identityRole = this.Context.AspNetRoles.FirstOrDefault(o => o.Name.ToUpper() == roleName.ToUpper()); | |
if (identityRole != null) | |
this.Context.Delete(identityRole); | |
user.AspNetRoles.Remove(identityRole); | |
return Task.FromResult<int>(0); | |
} | |
public Task<IList<string>> GetRolesAsync(AspNetUser user) | |
{ | |
this.ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
List<string> list = user.AspNetRoles.ToList().Select(o => o.Name).ToList(); | |
return Task.FromResult<IList<string>>(list); | |
} | |
public Task<bool> IsInRoleAsync(AspNetUser user, string roleName) | |
{ | |
this.ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
if (string.IsNullOrWhiteSpace(roleName)) | |
{ | |
throw new ArgumentException("Value Cannot Be Null Or Empty", "roleName"); | |
} | |
return Task.FromResult<bool>(user.AspNetRoles.Any(o => o.Name.ToUpper() == roleName.ToUpper())); | |
} | |
public Task SetSecurityStampAsync(AspNetUser user, string stamp) | |
{ | |
this.ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
user.SecurityStamp = stamp; | |
return Task.FromResult<int>(0); | |
} | |
public Task<string> GetSecurityStampAsync(AspNetUser user) | |
{ | |
this.ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
return Task.FromResult<string>(user.SecurityStamp); | |
} | |
public Task AddLoginAsync(AspNetUser user, UserLoginInfo login) | |
{ | |
this.ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
if (login == null) | |
{ | |
throw new ArgumentNullException("login"); | |
} | |
var identityUserLogin = new AspNetUserLogin | |
{ | |
AspNetUser = user, | |
ProviderKey = login.ProviderKey, | |
LoginProvider = login.LoginProvider | |
}; | |
this.Context.Add(identityUserLogin); | |
return Task.FromResult<int>(0); | |
} | |
public Task RemoveLoginAsync(AspNetUser user, UserLoginInfo login) | |
{ | |
this.ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
if (login == null) | |
{ | |
throw new ArgumentNullException("login"); | |
} | |
AspNetUserLogin userLogin = user.AspNetUserLogins.Where(o => | |
{ | |
if (o.LoginProvider != login.LoginProvider || o.AspNetUser != user) | |
{ | |
return false; | |
} | |
return o.ProviderKey == login.ProviderKey; | |
}).SingleOrDefault(); | |
if (userLogin != null) | |
this.Context.Delete(userLogin); | |
return Task.FromResult<int>(0); | |
} | |
public Task<IList<UserLoginInfo>> GetLoginsAsync(AspNetUser user) | |
{ | |
this.ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
var userLoginInfos = new List<UserLoginInfo>(); | |
foreach (AspNetUserLogin login in user.AspNetUserLogins) | |
userLoginInfos.Add(new UserLoginInfo(login.LoginProvider, login.ProviderKey)); | |
return Task.FromResult<IList<UserLoginInfo>>(userLoginInfos); | |
} | |
public Task<AspNetUser> FindAsync(UserLoginInfo login) | |
{ | |
this.ThrowIfDisposed(); | |
if (login == null) | |
{ | |
throw new ArgumentNullException("login"); | |
} | |
AspNetUserLogin userLogin = this.Context.AspNetUserLogins.FirstOrDefault(o => o.LoginProvider == login.LoginProvider && o.ProviderKey == login.ProviderKey); | |
return Task.FromResult(userLogin != null ? userLogin.AspNetUser : null); | |
} | |
private async Task SaveChanges() | |
{ | |
if (this.AutoSaveChanges) | |
{ | |
await Task.Run(() => | |
this.Context.SaveChanges()); | |
} | |
} | |
private void ThrowIfDisposed() | |
{ | |
if (this._disposed) | |
throw new ObjectDisposedException(this.GetType().Name); | |
} | |
#region Role Management | |
public virtual IQueryable<AspNetRole> FindRoleByName(string roleName) | |
{ | |
IQueryable<AspNetRole> identityRole = this.Context.AspNetRoles.Where(o => o.Name.Equals(roleName, StringComparison.InvariantCultureIgnoreCase)); | |
return identityRole; | |
} | |
public async Task<bool> CreateRoleAsync(string name) | |
{ | |
if (Context.AspNetRoles.Any(r => r.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase))) | |
return false; | |
var role = new AspNetRole() | |
{ | |
Name = name | |
}; | |
Context.Add(role); | |
await SaveChanges(); | |
return true; | |
} | |
public async Task ClearUserRoleAsync(AspNetUser user) | |
{ | |
this.ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
Context.Delete(user.AspNetRoles); | |
await SaveChanges(); | |
} | |
public async Task RemoveRoleAsync(string name) | |
{ | |
this.ThrowIfDisposed(); | |
if (name == null) | |
{ | |
throw new ArgumentNullException("name"); | |
} | |
Context.AspNetRoles.Where(o => o.Name.Equals(name)).DeleteAll(); | |
await SaveChanges(); | |
} | |
#endregion Role Management | |
#region IUserLockoutStore | |
public Task<DateTimeOffset> GetLockoutEndDateAsync(AspNetUser user) | |
{ | |
ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
return | |
Task.FromResult(user.LockoutEndDateUtc.HasValue | |
? new DateTimeOffset(DateTime.SpecifyKind(user.LockoutEndDateUtc.Value, DateTimeKind.Utc)) | |
: new DateTimeOffset()); | |
} | |
public Task SetLockoutEndDateAsync(AspNetUser user, DateTimeOffset lockoutEnd) | |
{ | |
ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
user.LockoutEndDateUtc = lockoutEnd == DateTimeOffset.MinValue ? (DateTime?)null : lockoutEnd.UtcDateTime; | |
return Task.FromResult(0); | |
} | |
public Task<int> IncrementAccessFailedCountAsync(AspNetUser user) | |
{ | |
ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
user.AccessFailedCount++; | |
return Task.FromResult(user.AccessFailedCount); | |
} | |
public Task ResetAccessFailedCountAsync(AspNetUser user) | |
{ | |
ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
user.AccessFailedCount = 0; | |
return Task.FromResult(0); | |
} | |
public Task<int> GetAccessFailedCountAsync(AspNetUser user) | |
{ | |
ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
return Task.FromResult(user.AccessFailedCount); | |
} | |
public Task<bool> GetLockoutEnabledAsync(AspNetUser user) | |
{ | |
ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
return Task.FromResult(user.LockoutEnabled); | |
} | |
public Task SetLockoutEnabledAsync(AspNetUser user, bool enabled) | |
{ | |
ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
user.LockoutEnabled = enabled; | |
return Task.FromResult(0); | |
} | |
#endregion IUserLockoutStore | |
/// <summary> | |
/// Set the user email | |
/// </summary> | |
/// <param name="user"></param> | |
/// <param name="email"></param> | |
/// <returns></returns> | |
public Task SetEmailAsync(AspNetUser user, string email) | |
{ | |
ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
user.Email = email; | |
return Task.FromResult(0); | |
} | |
/// <summary> | |
/// Get the user's email | |
/// </summary> | |
/// <param name="user"></param> | |
/// <returns></returns> | |
public Task<string> GetEmailAsync(AspNetUser user) | |
{ | |
ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
return Task.FromResult(user.Email); | |
} | |
public Task<bool> GetEmailConfirmedAsync(AspNetUser user) | |
{ | |
ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
return Task.FromResult(user.EmailConfirmed); | |
} | |
public Task SetEmailConfirmedAsync(AspNetUser user, bool confirmed) | |
{ | |
ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
user.EmailConfirmed = confirmed; | |
return Task.FromResult(0); | |
} | |
public Task<AspNetUser> FindByEmailAsync(string email) | |
{ | |
ThrowIfDisposed(); | |
return GetUserAggregateAsync(u => u.Email.ToUpper() == email.ToUpper()); | |
} | |
public Task SetPhoneNumberAsync(AspNetUser user, string phoneNumber) | |
{ | |
ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
user.PhoneNumber = phoneNumber; | |
return Task.FromResult(0); | |
} | |
public Task<string> GetPhoneNumberAsync(AspNetUser user) | |
{ | |
ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
return Task.FromResult(user.PhoneNumber); | |
} | |
public Task<bool> GetPhoneNumberConfirmedAsync(AspNetUser user) | |
{ | |
ThrowIfDisposed(); | |
if (user == null) { throw new ArgumentNullException("user"); } | |
return Task.FromResult(user.TwoFactorEnabled); | |
} | |
public Task SetPhoneNumberConfirmedAsync(AspNetUser user, bool confirmed) | |
{ | |
ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
user.PhoneNumberConfirmed = confirmed; | |
return Task.FromResult(0); | |
} | |
public Task SetTwoFactorEnabledAsync(AspNetUser user, bool enabled) | |
{ | |
ThrowIfDisposed(); | |
if (user == null) | |
{ throw new ArgumentNullException("user"); } | |
user.TwoFactorEnabled = enabled; | |
return Task.FromResult(0); | |
} | |
public Task<bool> GetTwoFactorEnabledAsync(AspNetUser user) | |
{ | |
ThrowIfDisposed(); | |
if (user == null) | |
{ | |
throw new ArgumentNullException("user"); | |
} | |
return Task.FromResult(user.TwoFactorEnabled); | |
} | |
private Task<AspNetUser> GetUserAggregateAsync(Expression<Func<AspNetUser, bool>> filter) | |
{ | |
return Task.FromResult( | |
Context.AspNetUsers.Include(u => u.AspNetRoles).Include(u => u.AspNetUserClaims).Include(u => u.AspNetUserLogins).FirstOrDefault(filter)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment