aboutsummaryrefslogtreecommitdiff
path: root/ExamTemplate/Services
diff options
context:
space:
mode:
authorSyndamia <kamen.d.mladenov@protonmail.com>2021-05-08 16:49:17 +0300
committerSyndamia <kamen.d.mladenov@protonmail.com>2021-05-08 16:49:17 +0300
commita1e46b76a1299e35b1ac8cae69e77c66d74224a6 (patch)
tree068730b87c507df6386ff062ea620811dd6645ae /ExamTemplate/Services
parente18090705f9b3d2a4be877743364f77f5effc46c (diff)
downloadit-kariera-exam-template-a1e46b76a1299e35b1ac8cae69e77c66d74224a6.tar
it-kariera-exam-template-a1e46b76a1299e35b1ac8cae69e77c66d74224a6.tar.gz
it-kariera-exam-template-a1e46b76a1299e35b1ac8cae69e77c66d74224a6.zip
Moved user repository logic into user service logic (no more repos)
Diffstat (limited to 'ExamTemplate/Services')
-rw-r--r--ExamTemplate/Services/UserService.cs47
1 files changed, 33 insertions, 14 deletions
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<User> _signInManager;
+ private readonly UserManager<User> _userManager;
+ private readonly RoleManager<Role> _roleManager;
- public UserService(IMapper autoMapper, UserRepository userRepository, SignInManager<User> signInManager)
+ public UserService(IMapper autoMapper, TemplateContext templateContext, SignInManager<User> signInManager, UserManager<User> userManager, RoleManager<Role> roleManager)
{
this._autoMapper = autoMapper;
- this._userRepository = userRepository;
+ this._context = templateContext;
this._signInManager = signInManager;
+ this._userManager = userManager;
+ this._roleManager = roleManager;
}
public async Task<bool> RegisterUserAsync(RegisterUserServiceModel registerUserServiceModel)
{
User user = this._autoMapper.Map<User>(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<bool> 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<UserServiceModel> 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<UserServiceModel>(user);
}
public async Task<UserServiceModel> GetUserByClaimsAsync(ClaimsPrincipal claimsPrincipal)
{
- User user = await this._userRepository.GetByClaimsAsync(claimsPrincipal);
+ User user = await this._userManager.GetUserAsync(claimsPrincipal);
return this._autoMapper.Map<UserServiceModel>(user);
}
public async Task<bool> 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<bool> IsAuthorizedToModify(ClaimsPrincipal claimsPrincipal, string username)
+ {
+ User user = await this._userManager.GetUserAsync(claimsPrincipal);
+
+ return user.UserName == username || await this._userManager.IsInRoleAsync(user, "Administrator");
+ }
}
}