// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. 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 EntityRoleStore : EntityRoleStore where TRole : EntityRole { public EntityRoleStore(DbContext context) : base(context) { } } public class EntityRoleStore : IQueryableRoleStore where TRole : EntityRole where TKey : IEquatable { private bool _disposed; public EntityRoleStore(DbContext context) { if (context == null) { throw new ArgumentNullException("context"); } Context = context; AutoSaveChanges = true; } public DbContext Context { get; private set; } /// /// If true will call SaveChanges after CreateAsync/UpdateAsync/DeleteAsync /// 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)) { // TODO: return Roles.SingleOrDefaultAsync(filter, cancellationToken); return Task.FromResult(Roles.SingleOrDefault(filter)); } public async virtual Task CreateAsync(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 UpdateAsync(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 DeleteAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (role == null) { throw new ArgumentNullException("role"); } Context.Delete(role); await SaveChanges(cancellationToken); } public Task GetRoleIdAsync(TRole role, CancellationToken cancellationToken = new CancellationToken()) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (role == null) { throw new ArgumentNullException("role"); } return Task.FromResult(role.Id); } public Task GetRoleNameAsync(TRole role, CancellationToken cancellationToken = new CancellationToken()) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (role == null) { throw new ArgumentNullException("role"); } return Task.FromResult(role.Name); } public Task SetRoleNameAsync(TRole role, string roleName, CancellationToken cancellationToken = new CancellationToken()) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (role == null) { throw new ArgumentNullException("role"); } role.Name = roleName; return Task.FromResult(0); } public virtual TKey ConvertId(string userId) { return (TKey)Convert.ChangeType(userId, typeof(TKey)); } /// /// Find a role by id /// /// /// /// public virtual Task FindByIdAsync(string id, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); var roleId = ConvertId(id); return GetRoleAggregate(u => u.Id.Equals(roleId), cancellationToken); } /// /// Find a role by name /// /// /// /// public virtual Task FindByNameAsync(string name, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); return GetRoleAggregate(u => u.Name.ToUpper() == name.ToUpper(), cancellationToken); } 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(); } } } }