Files
mixtape/zero.Core/Identity/Services/UserRolesService.cs
T

117 lines
2.7 KiB
C#
Raw Normal View History

2021-11-23 22:56:22 +01:00
using FluentValidation.Results;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Raven.Client.Documents;
using System.Security.Claims;
namespace zero.Identity;
2021-11-26 11:21:49 +01:00
public class UserRolesService : IUserRolesService
2021-11-23 22:56:22 +01:00
{
protected IHttpContextAccessor HttpContextAccessor { get; set; }
protected UserManager<ZeroUser> UserManager { get; private set; }
protected RoleManager<ZeroUserRole> RoleManager { get; private set; }
2021-11-26 12:31:33 +01:00
protected IZeroContext Context { get; private set; }
2021-11-23 22:56:22 +01:00
2021-11-26 12:31:33 +01:00
protected IZeroDocumentSession Session => Context.Store.Session();
2021-11-27 16:09:02 +01:00
public UserRolesService(IZeroContext context, IHttpContextAccessor httpContextAccessor, UserManager<ZeroUser> userManager, RoleManager<ZeroUserRole> roleManager)
2021-11-23 22:56:22 +01:00
{
2021-11-26 12:31:33 +01:00
Context = context;
2021-11-23 22:56:22 +01:00
HttpContextAccessor = httpContextAccessor;
UserManager = userManager;
RoleManager = roleManager;
}
/// <inheritdoc />
public async Task<IList<ZeroUserRole>> GetAll()
{
return await Session.Query<ZeroUserRole>().OrderBy(x => x.Sort).ThenBy(x => x.Name).ToListAsync();
}
/// <inheritdoc />
public async Task<ZeroUserRole> GetById(string id)
{
return await RoleManager.FindByIdAsync(id);
}
/// <inheritdoc />
2021-11-26 15:47:11 +01:00
public async Task<Result<ZeroUserRole>> Save(ZeroUserRole model)
2021-11-23 22:56:22 +01:00
{
2021-11-26 12:31:33 +01:00
//ValidationResult validation = await new UserRoleValidator().ValidateAsync(model);
2021-11-23 22:56:22 +01:00
2021-11-26 12:31:33 +01:00
//if (!validation.IsValid)
//{
// return EntityResult<ZeroUserRole>.Fail(validation);
//}
2021-11-23 22:56:22 +01:00
if (model.Id.IsNullOrEmpty())
{
model.CreatedDate = DateTimeOffset.Now;
}
model.Alias = Safenames.Alias(model.Name);
await Session.StoreAsync(model);
string id = Session.Advanced.GetDocumentId(model);
await Session.SaveChangesAsync();
if (model.Id.IsNullOrEmpty())
{
model.Id = id;
}
2021-11-26 15:47:11 +01:00
return Result<ZeroUserRole>.Success(model);
2021-11-23 22:56:22 +01:00
}
/// <inheritdoc />
2021-11-26 15:47:11 +01:00
public async Task<Result<ZeroUserRole>> Delete(string id)
2021-11-23 22:56:22 +01:00
{
ZeroUserRole country = await Session.LoadAsync<ZeroUserRole>(id);
if (country == null)
{
2021-11-26 15:47:11 +01:00
return Result<ZeroUserRole>.Fail("@errors.ondelete.idnotfound");
2021-11-23 22:56:22 +01:00
}
Session.Delete(country);
await Session.SaveChangesAsync();
2021-11-26 15:47:11 +01:00
return Result<ZeroUserRole>.Success();
2021-11-23 22:56:22 +01:00
}
}
2021-11-26 11:21:49 +01:00
public interface IUserRolesService
2021-11-23 22:56:22 +01:00
{
/// <summary>
/// Get all user roles
/// </summary>
Task<IList<ZeroUserRole>> GetAll();
/// <summary>
/// Get role by id
/// </summary>
Task<ZeroUserRole> GetById(string id);
/// <summary>
/// Create or update a role
/// </summary>
2021-11-26 15:47:11 +01:00
Task<Result<ZeroUserRole>> Save(ZeroUserRole model);
2021-11-23 22:56:22 +01:00
/// <summary>
/// Deletes a role
/// </summary>
2021-11-26 15:47:11 +01:00
Task<Result<ZeroUserRole>> Delete(string id);
2021-11-23 22:56:22 +01:00
}