using System; using System.Linq; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; using Microsoft.Data.Entity; namespace Microsoft.AspNet.Identity.Entity { public class RoleStore : IQueryableRoleStore where TRole : IdentityRole where TKey : IEquatable { private bool _disposed; public RoleStore(EntityContext context) { if (context == null) { throw new ArgumentNullException("context"); } Context = context; AutoSaveChanges = true; } public EntityContext Context { get; private set; } /// /// If true will call SaveChanges after Create/Update/Delete /// public bool AutoSaveChanges { get; set; } private async Task SaveChanges(CancellationToken cancellationToken) { if (AutoSaveChanges) { await Context.SaveChangesAsync(cancellationToken); } } public virtual Task GetRoleAggregate(Expression> filter, CancellationToken cancellationToken = default(CancellationToken)) { return Roles.SingleOrDefaultAsync(filter, cancellationToken); } public async virtual Task Create(TRole role, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (role == null) { throw new ArgumentNullException("role"); } await Context.AddAsync(role, cancellationToken); await SaveChanges(cancellationToken); } public async virtual Task Update(TRole role, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (role == null) { throw new ArgumentNullException("role"); } await Context.UpdateAsync(role, cancellationToken); await SaveChanges(cancellationToken); } public async virtual Task Delete(TRole role, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (role == null) { throw new ArgumentNullException("role"); } Context.Delete(role); await SaveChanges(cancellationToken); } public Task GetRoleId(TRole role, CancellationToken cancellationToken = new CancellationToken()) { return Task.FromResult(role.Id); } public Task GetRoleName(TRole role, CancellationToken cancellationToken = new CancellationToken()) { return Task.FromResult(role.Name); } public virtual TKey ConvertId(string userId) { return (TKey)Convert.ChangeType(userId, typeof(TKey)); } /// /// Find a role by id /// /// /// /// public virtual Task FindById(string id, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); var roleId = ConvertId(id); return Roles.SingleOrDefaultAsync(r => r.Id.Equals(roleId), cancellationToken); //return GetRoleAggregate(u => u.Id.Equals(id)); } /// /// Find a role by name /// /// /// /// public virtual Task FindByName(string name, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); return Roles.SingleOrDefaultAsync(r => r.Name.ToUpper() == name.ToUpper(), cancellationToken); //return GetRoleAggregate(u => u.Name.ToUpper() == name.ToUpper()); } private void ThrowIfDisposed() { if (_disposed) { throw new ObjectDisposedException(GetType().Name); } } /// /// Dispose the store /// public void Dispose() { _disposed = true; } public IQueryable Roles { get { return Context.Set(); } } } }