using System.Security.Claims; using System.Threading.Tasks; using AutoMapper; using ExamTemplate.Data; using ExamTemplate.Data.Models; using ExamTemplate.Services.Models; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; namespace ExamTemplate.Services { public class UserService { private readonly IMapper _autoMapper; private readonly TemplateContext _context; private readonly SignInManager _signInManager; private readonly UserManager _userManager; private readonly RoleManager _roleManager; public UserService(IMapper autoMapper, TemplateContext templateContext, SignInManager signInManager, UserManager userManager, RoleManager roleManager) { this._autoMapper = autoMapper; this._context = templateContext; this._signInManager = signInManager; this._userManager = userManager; this._roleManager = roleManager; } public async Task RegisterUserAsync(RegisterUserServiceModel registerUserServiceModel) { User user = this._autoMapper.Map(registerUserServiceModel); user.PasswordHash = this._userManager.PasswordHasher.HashPassword(user, registerUserServiceModel.Password); IdentityResult userCreateResult = await this._userManager.CreateAsync(user); // 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) { SignInResult result = await this._signInManager.PasswordSignInAsync(loginUserServiceModel.Username, loginUserServiceModel.Password, false, false); return result.Succeeded; } public async Task LogoutAsync() { await this._signInManager.SignOutAsync(); } public async Task GetUserByUsernameAsync(string 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._userManager.GetUserAsync(claimsPrincipal); return this._autoMapper.Map(user); } public async Task EditUserAsync(ClaimsPrincipal claimsPrincipal, EditUserServiceModel editUserServiceModel) { User user = await this._userManager.GetUserAsync(claimsPrincipal); user.UserName = editUserServiceModel.Username; user.FirstName = editUserServiceModel.FirstName; user.LastName = editUserServiceModel.LastName; 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"); } } }