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/Services/UserService.cs | 47 +++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 14 deletions(-) (limited to 'ExamTemplate/Services/UserService.cs') 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"); + } } } -- cgit v1.2.3