diff options
| author | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-05-08 16:49:17 +0300 |
|---|---|---|
| committer | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-05-08 16:49:17 +0300 |
| commit | a1e46b76a1299e35b1ac8cae69e77c66d74224a6 (patch) | |
| tree | 068730b87c507df6386ff062ea620811dd6645ae | |
| parent | e18090705f9b3d2a4be877743364f77f5effc46c (diff) | |
| download | it-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)
| -rw-r--r-- | ExamTemplate/Data/Repositories/UserRepository.cs | 66 | ||||
| -rw-r--r-- | ExamTemplate/Services/UserService.cs | 47 | ||||
| -rw-r--r-- | ExamTemplate/Web/Startup.cs | 4 |
3 files changed, 34 insertions, 83 deletions
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<User> _userManager; - private readonly RoleManager<Role> _roleManager; - - public UserRepository(TemplateContext templateContext, UserManager<User> userManager, RoleManager<Role> roleManager) - { - this._context = templateContext; - this._userManager = userManager; - this._roleManager = roleManager; - } - - public async Task<User> GetByUsernameAsync(string username) - { - return await this._userManager.Users - .Include(x => x.Roles) - .FirstOrDefaultAsync(x => x.UserName == username); - } - - public async Task<User> GetByClaimsAsync(ClaimsPrincipal claimsPrincipal) - { - return await this._userManager.GetUserAsync(claimsPrincipal); - } - - public async Task<bool> 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<bool> 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<bool> EditUserAsync(User user) - { - IdentityResult result = await this._userManager.UpdateAsync(user); - - return result.Succeeded; - } - - public async Task<bool> 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<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");
+ }
}
}
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<UserService>();
- services.AddTransient<UserRepository>();
/*
* Database configuration
|
