113 lines
3.5 KiB
C#
113 lines
3.5 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace BlueWest.WebApi.Context.Users;
|
|
|
|
/// <summary>
|
|
/// Role storage management
|
|
/// </summary>
|
|
public class RoleStore : IRoleStore<ApplicationRole>
|
|
{
|
|
private ApplicationUserDbContext _dbContext;
|
|
|
|
/// <summary>
|
|
/// Role Store constructor
|
|
/// </summary>
|
|
/// <param name="dbContext"></param>
|
|
public RoleStore(ApplicationUserDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public void Dispose()
|
|
{
|
|
_dbContext = null;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get role name
|
|
/// </summary>
|
|
/// <param name="role"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<string> GetRoleNameAsync(ApplicationUserRole role, CancellationToken cancellationToken)
|
|
{
|
|
var foundRole = await _dbContext.Roles
|
|
.FirstOrDefaultAsync(x => x.Id == role.RoleId, cancellationToken: cancellationToken);
|
|
|
|
if (foundRole != null)
|
|
{
|
|
return foundRole.Name;
|
|
}
|
|
|
|
return string.Empty;
|
|
}
|
|
|
|
|
|
public async Task<IdentityResult> CreateAsync(ApplicationRole role, CancellationToken cancellationToken)
|
|
{
|
|
_dbContext.Roles.Add(role);
|
|
return await _dbContext.SaveChangesAsync(cancellationToken) >= 0 ? IdentityResult.Success : IdentityResult.Failed();
|
|
}
|
|
|
|
public async Task<IdentityResult> UpdateAsync(ApplicationRole role, CancellationToken cancellationToken)
|
|
{
|
|
_dbContext.Roles.Update(role);
|
|
return await _dbContext.SaveChangesAsync(cancellationToken) >= 0 ? IdentityResult.Success : IdentityResult.Failed();
|
|
}
|
|
|
|
public async Task<IdentityResult> DeleteAsync(ApplicationRole role, CancellationToken cancellationToken)
|
|
{
|
|
_dbContext.Roles.Remove(role);
|
|
return await _dbContext.SaveChangesAsync(cancellationToken) >= 0 ? IdentityResult.Success : IdentityResult.Failed();
|
|
}
|
|
|
|
public async Task<string> GetRoleIdAsync(ApplicationRole role, CancellationToken cancellationToken)
|
|
{
|
|
var x = await _dbContext.Roles.FirstOrDefaultAsync(x => x.Id == role.Id, cancellationToken: cancellationToken);
|
|
if (x != null)
|
|
{
|
|
return x.Id;
|
|
}
|
|
return string.Empty;
|
|
}
|
|
|
|
public Task<string> GetRoleNameAsync(ApplicationRole role, CancellationToken cancellationToken)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public Task SetRoleNameAsync(ApplicationRole role, string roleName, CancellationToken cancellationToken)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public Task<string> GetNormalizedRoleNameAsync(ApplicationRole role, CancellationToken cancellationToken)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public Task SetNormalizedRoleNameAsync(ApplicationRole role, string normalizedName, CancellationToken cancellationToken)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public Task<ApplicationRole> FindByIdAsync(string roleId, CancellationToken cancellationToken)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public Task<ApplicationRole> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
} |