using FluentValidation.Results; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Raven.Client.Documents; using System.Security.Claims; namespace zero.Identity; public class UserRolesService : IUserRolesService { protected IHttpContextAccessor HttpContextAccessor { get; set; } protected UserManager UserManager { get; private set; } protected RoleManager RoleManager { get; private set; } protected IZeroContext Context { get; private set; } protected IZeroDocumentSession Session => Context.Store.Session(); public UserRolesService(IZeroContext context, IHttpContextAccessor httpContextAccessor, UserManager userManager, RoleManager roleManager) { Context = context; HttpContextAccessor = httpContextAccessor; UserManager = userManager; RoleManager = roleManager; } /// public async Task> GetAll() { return await Session.Query().OrderBy(x => x.Sort).ThenBy(x => x.Name).ToListAsync(); } /// public async Task GetById(string id) { return await RoleManager.FindByIdAsync(id); } /// public async Task> Save(ZeroUserRole model) { //ValidationResult validation = await new UserRoleValidator().ValidateAsync(model); //if (!validation.IsValid) //{ // return EntityResult.Fail(validation); //} 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; } return Result.Success(model); } /// public async Task> Delete(string id) { ZeroUserRole country = await Session.LoadAsync(id); if (country == null) { return Result.Fail("@errors.ondelete.idnotfound"); } Session.Delete(country); await Session.SaveChangesAsync(); return Result.Success(); } } public interface IUserRolesService { /// /// Get all user roles /// Task> GetAll(); /// /// Get role by id /// Task GetById(string id); /// /// Create or update a role /// Task> Save(ZeroUserRole model); /// /// Deletes a role /// Task> Delete(string id); }