using System; using System.Linq; using System.Threading.Tasks; namespace Microsoft.AspNet.Identity { /// /// Exposes role related api which will automatically save changes to the RoleStore /// /// public class RoleManager : RoleManager where TRole : class, IRole { /// /// Constructor /// /// public RoleManager(IRoleStore store) : base(store) { } } /// /// Exposes role related api which will automatically save changes to the RoleStore /// /// /// public class RoleManager : IDisposable where TRole : class, IRole where TKey : IEquatable { private bool _disposed; /// /// Constructor /// /// The IRoleStore is responsible for commiting changes via the UpdateAsync/CreateAsync methods public RoleManager(IRoleStore store) { if (store == null) { throw new ArgumentNullException("store"); } Store = store; RoleValidator = new RoleValidator(this); } /// /// Persistence abstraction that the Manager operates against /// protected IRoleStore Store { get; private set; } /// /// Used to validate roles before persisting changes /// public IRoleValidator RoleValidator { get; set; } /// /// Returns an IQueryable of roles if the store is an IQueryableRoleStore /// public virtual IQueryable Roles { get { var queryableStore = Store as IQueryableRoleStore; if (queryableStore == null) { throw new NotSupportedException(Resources.StoreNotIQueryableRoleStore); } return queryableStore.Roles; } } /// /// Dispose this object /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private async Task ValidateRoleInternal(TRole role) { return (RoleValidator == null) ? IdentityResult.Success : await RoleValidator.Validate(role).ConfigureAwait(false); } /// /// Create a role /// /// /// public virtual async Task Create(TRole role) { ThrowIfDisposed(); if (role == null) { throw new ArgumentNullException("role"); } var result = await ValidateRoleInternal(role); if (!result.Succeeded) { return result; } await Store.Create(role); return IdentityResult.Success; } /// /// Update an existing role /// /// /// public virtual async Task Update(TRole role) { ThrowIfDisposed(); if (role == null) { throw new ArgumentNullException("role"); } var result = await ValidateRoleInternal(role); if (!result.Succeeded) { return result; } await Store.Update(role).ConfigureAwait(false); return IdentityResult.Success; } /// /// Delete a role /// /// /// public virtual async Task Delete(TRole role) { ThrowIfDisposed(); if (role == null) { throw new ArgumentNullException("role"); } await Store.Delete(role).ConfigureAwait(false); return IdentityResult.Success; } /// /// Returns true if the role exists /// /// /// public virtual async Task RoleExists(string roleName) { ThrowIfDisposed(); if (roleName == null) { throw new ArgumentNullException("roleName"); } return await FindByName(roleName).ConfigureAwait(false) != null; } /// /// Find a role by id /// /// /// public virtual async Task FindById(TKey roleId) { ThrowIfDisposed(); return await Store.FindById(roleId).ConfigureAwait(false); } /// /// Find a role by name /// /// /// public virtual async Task FindByName(string roleName) { ThrowIfDisposed(); if (roleName == null) { throw new ArgumentNullException("roleName"); } return await Store.FindByName(roleName).ConfigureAwait(false); } private void ThrowIfDisposed() { if (_disposed) { throw new ObjectDisposedException(GetType().Name); } } /// /// When disposing, actually dipose the store /// /// protected virtual void Dispose(bool disposing) { if (disposing && !_disposed) { Store.Dispose(); } _disposed = true; } } }