From 72502154725594cf31878aa944f4bc9d9f3521a3 Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 16 Feb 2021 18:07:51 +0200 Subject: UserManager&RoleManager logic moved to Repo; Password hashing and validation moved to Repo and using ASPNET Core hashing methods; Added Migrations; Fixed Roles not added to user --- .../DevHive.Data/Repositories/RoleRepository.cs | 37 +++++++++++----------- 1 file changed, 19 insertions(+), 18 deletions(-) (limited to 'src/Data/DevHive.Data/Repositories/RoleRepository.cs') diff --git a/src/Data/DevHive.Data/Repositories/RoleRepository.cs b/src/Data/DevHive.Data/Repositories/RoleRepository.cs index 7d07c1b..99e0634 100644 --- a/src/Data/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/Data/DevHive.Data/Repositories/RoleRepository.cs @@ -2,53 +2,54 @@ using System; using System.Threading.Tasks; using DevHive.Data.Interfaces; using DevHive.Data.Models; +using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { public class RoleRepository : BaseRepository, IRoleRepository { - private readonly DevHiveContext _context; + private readonly RoleManager _roleManager; - public RoleRepository(DevHiveContext context) + public RoleRepository(DevHiveContext context, RoleManager roleManager) : base(context) { - this._context = context; + this._roleManager = roleManager; } + #region Create + public override async Task AddAsync(Role entity) + { + IdentityResult result = await this._roleManager.CreateAsync(entity); + + return result.Succeeded; + } + #endregion + #region Read public async Task GetByNameAsync(string name) { - return await this._context.Roles - .FirstOrDefaultAsync(x => x.Name == name); + return await this._roleManager.FindByNameAsync(name); } #endregion public override async Task EditAsync(Guid id, Role newEntity) { - Role role = await this.GetByIdAsync(id); - - this._context - .Entry(role) - .CurrentValues - .SetValues(newEntity); + newEntity.Id = id; + IdentityResult result = await this._roleManager.UpdateAsync(newEntity); - return await this.SaveChangesAsync(); + return result.Succeeded; } #region Validations public async Task DoesNameExist(string name) { - return await this._context.Roles - .AsNoTracking() - .AnyAsync(r => r.Name == name); + return await this._roleManager.RoleExistsAsync(name); } public async Task DoesRoleExist(Guid id) { - return await this._context.Roles - .AsNoTracking() - .AnyAsync(r => r.Id == id); + return await this._roleManager.Roles.AnyAsync(r => r.Id == id); } #endregion } -- cgit v1.2.3