From a1e46b76a1299e35b1ac8cae69e77c66d74224a6 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 8 May 2021 16:49:17 +0300 Subject: Moved user repository logic into user service logic (no more repos) --- ExamTemplate/Data/Repositories/UserRepository.cs | 66 ------------------------ ExamTemplate/Services/UserService.cs | 47 ++++++++++++----- ExamTemplate/Web/Startup.cs | 4 +- 3 files changed, 34 insertions(+), 83 deletions(-) delete mode 100644 ExamTemplate/Data/Repositories/UserRepository.cs diff --git a/ExamTemplate/Data/Repositories/UserRepository.cs b/ExamTemplate/Data/Repositories/UserRepository.cs deleted file mode 100644 index 3e5ceaa..0000000 --- a/ExamTemplate/Data/Repositories/UserRepository.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System.Security.Claims; -using System.Threading.Tasks; -using ExamTemplate.Data.Models; -using Microsoft.AspNetCore.Identity; -using Microsoft.EntityFrameworkCore; - -namespace ExamTemplate.Data.Repositories -{ - public class UserRepository - { - private readonly TemplateContext _context; - private readonly UserManager _userManager; - private readonly RoleManager _roleManager; - - public UserRepository(TemplateContext templateContext, UserManager userManager, RoleManager roleManager) - { - this._context = templateContext; - this._userManager = userManager; - this._roleManager = roleManager; - } - - public async Task GetByUsernameAsync(string username) - { - return await this._userManager.Users - .Include(x => x.Roles) - .FirstOrDefaultAsync(x => x.UserName == username); - } - - public async Task GetByClaimsAsync(ClaimsPrincipal claimsPrincipal) - { - return await this._userManager.GetUserAsync(claimsPrincipal); - } - - public async Task AddAsync(User user, string password) - { - user.PasswordHash = this._userManager.PasswordHasher.HashPassword(user, password); - IdentityResult result = await this._userManager.CreateAsync(user); - - return result.Succeeded; - } - - public async Task AddRoleToUserAsync(User user, string roleName) - { - bool succeeded = (await this._userManager.AddToRoleAsync(user, roleName)).Succeeded; - if (succeeded) - { - user.Roles.Add(await this._roleManager.FindByNameAsync(roleName)); - succeeded = await this._context.SaveChangesAsync() >= 1; - } - - return succeeded; - } - - public async Task EditUserAsync(User user) - { - IdentityResult result = await this._userManager.UpdateAsync(user); - - return result.Succeeded; - } - - public async Task VerifyPasswordAsync(User user, string password) - { - return await this._userManager.CheckPasswordAsync(user, password); - } - } -} diff --git a/ExamTemplate/Services/UserService.cs b/ExamTemplate/Services/UserService.cs index 329d21e..90a4bf4 100644 --- a/ExamTemplate/Services/UserService.cs +++ b/ExamTemplate/Services/UserService.cs @@ -1,41 +1,50 @@ using System.Security.Claims; using System.Threading.Tasks; using AutoMapper; +using ExamTemplate.Data; using ExamTemplate.Data.Models; -using ExamTemplate.Data.Repositories; using ExamTemplate.Services.Models; using Microsoft.AspNetCore.Identity; +using Microsoft.EntityFrameworkCore; namespace ExamTemplate.Services { public class UserService { private readonly IMapper _autoMapper; - private readonly UserRepository _userRepository; + private readonly TemplateContext _context; private readonly SignInManager _signInManager; + private readonly UserManager _userManager; + private readonly RoleManager _roleManager; - public UserService(IMapper autoMapper, UserRepository userRepository, SignInManager signInManager) + public UserService(IMapper autoMapper, TemplateContext templateContext, SignInManager signInManager, UserManager userManager, RoleManager roleManager) { this._autoMapper = autoMapper; - this._userRepository = userRepository; + this._context = templateContext; this._signInManager = signInManager; + this._userManager = userManager; + this._roleManager = roleManager; } public async Task RegisterUserAsync(RegisterUserServiceModel registerUserServiceModel) { User user = this._autoMapper.Map(registerUserServiceModel); - bool userCreateResult = await this._userRepository.AddAsync(user, registerUserServiceModel.Password); - bool addRoleResult = await this._userRepository.AddRoleToUserAsync(user, Role.UserRole); + user.PasswordHash = this._userManager.PasswordHasher.HashPassword(user, registerUserServiceModel.Password); + IdentityResult userCreateResult = await this._userManager.CreateAsync(user); - return userCreateResult && addRoleResult; + // Many to many relationships with Roles can cause problems, + // that's why I add the Role to the User and add the User to the Role + IdentityResult addRoleResult = await this._userManager.AddToRoleAsync(user, "User"); + user.Roles.Add(await this._roleManager.FindByNameAsync("User")); + bool roleAddedSuccessfuly = await this._context.SaveChangesAsync() >= 1; + + return userCreateResult.Succeeded && addRoleResult.Succeeded && roleAddedSuccessfuly; } public async Task LoginUserAsync(LoginUserServiceModel loginUserServiceModel) { - User user = await this._userRepository.GetByUsernameAsync(loginUserServiceModel.Username); - - var result = await this._signInManager.PasswordSignInAsync(loginUserServiceModel.Username, loginUserServiceModel.Password, false, false); + SignInResult result = await this._signInManager.PasswordSignInAsync(loginUserServiceModel.Username, loginUserServiceModel.Password, false, false); return result.Succeeded; } @@ -47,32 +56,42 @@ namespace ExamTemplate.Services public async Task GetUserByUsernameAsync(string username) { - User user = await this._userRepository.GetByUsernameAsync(username); + User user = await this._userManager.Users + .Include(x => x.Roles) + .FirstOrDefaultAsync(x => x.UserName == username); return this._autoMapper.Map(user); } public async Task GetUserByClaimsAsync(ClaimsPrincipal claimsPrincipal) { - User user = await this._userRepository.GetByClaimsAsync(claimsPrincipal); + User user = await this._userManager.GetUserAsync(claimsPrincipal); return this._autoMapper.Map(user); } public async Task EditUserAsync(ClaimsPrincipal claimsPrincipal, EditUserServiceModel editUserServiceModel) { - User user = await this._userRepository.GetByClaimsAsync(claimsPrincipal); + User user = await this._userManager.GetUserAsync(claimsPrincipal); user.UserName = editUserServiceModel.Username; user.FirstName = editUserServiceModel.FirstName; user.LastName = editUserServiceModel.LastName; - return await this._userRepository.EditUserAsync(user); + IdentityResult result = await this._userManager.UpdateAsync(user); + return result.Succeeded; } public bool IsSignedIn(ClaimsPrincipal claimsPrincipal) { return this._signInManager.IsSignedIn(claimsPrincipal); } + + public async Task IsAuthorizedToModify(ClaimsPrincipal claimsPrincipal, string username) + { + User user = await this._userManager.GetUserAsync(claimsPrincipal); + + return user.UserName == username || await this._userManager.IsInRoleAsync(user, "Administrator"); + } } } diff --git a/ExamTemplate/Web/Startup.cs b/ExamTemplate/Web/Startup.cs index 9c43601..0754bff 100644 --- a/ExamTemplate/Web/Startup.cs +++ b/ExamTemplate/Web/Startup.cs @@ -2,7 +2,6 @@ using System; using System.Linq; using ExamTemplate.Data; using ExamTemplate.Data.Models; -using ExamTemplate.Data.Repositories; using ExamTemplate.Services; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -14,7 +13,7 @@ using Microsoft.Extensions.Hosting; namespace Web { - public class Startup + public class Startup { public Startup(IConfiguration configuration) { @@ -34,7 +33,6 @@ namespace Web */ services.AddTransient(); - services.AddTransient(); /* * Database configuration -- cgit v1.2.3