aboutsummaryrefslogtreecommitdiff
path: root/ExamTemplate/Services/UserService.cs
diff options
context:
space:
mode:
Diffstat (limited to 'ExamTemplate/Services/UserService.cs')
-rw-r--r--ExamTemplate/Services/UserService.cs94
1 files changed, 0 insertions, 94 deletions
diff --git a/ExamTemplate/Services/UserService.cs b/ExamTemplate/Services/UserService.cs
deleted file mode 100644
index 3099bda..0000000
--- a/ExamTemplate/Services/UserService.cs
+++ /dev/null
@@ -1,94 +0,0 @@
-using System;
-using System.Security.Claims;
-using System.Threading.Tasks;
-using AutoMapper;
-using ExamTemplate.Data;
-using ExamTemplate.Data.Models;
-using ExamTemplate.Services.Models.User;
-using Microsoft.AspNetCore.Identity;
-using Microsoft.EntityFrameworkCore;
-using ExamTemplate.Services.Interfaces;
-
-namespace ExamTemplate.Services
-{
- public class UserService : IUserService
- {
- private readonly IMapper _autoMapper;
- private readonly TemplateContext _context;
- private readonly SignInManager<User> _signInManager;
- private readonly UserManager<User> _userManager;
- private readonly RoleManager<IdentityRole<Guid>> _roleManager;
-
- public UserService(IMapper autoMapper, TemplateContext templateContext, SignInManager<User> signInManager, UserManager<User> userManager, RoleManager<IdentityRole<Guid>> roleManager)
- {
- this._autoMapper = autoMapper;
- 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);
-
- user.PasswordHash = this._userManager.PasswordHasher.HashPassword(user, registerUserServiceModel.Password);
- IdentityResult userCreateResult = await this._userManager.CreateAsync(user);
- IdentityResult addRoleResult = await this._userManager.AddToRoleAsync(user, "User");
-
- return userCreateResult.Succeeded && addRoleResult.Succeeded;
- }
-
- public async Task<bool> 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<UserServiceModel> GetUserByUsernameAsync(string username)
- {
- User user = await this._userManager.Users
- .FirstOrDefaultAsync(x => x.UserName == username);
-
- return this._autoMapper.Map<UserServiceModel>(user);
- }
-
- public async Task<UserServiceModel> GetUserByClaimsAsync(ClaimsPrincipal 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._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 async Task<bool> DeleteUserAsync(ClaimsPrincipal claimsPrincipal)
- {
- User user = await this._userManager.GetUserAsync(claimsPrincipal);
-
- IdentityResult result = await this._userManager.DeleteAsync(user);
- return result.Succeeded;
- }
-
- public bool IsSignedIn(ClaimsPrincipal claimsPrincipal)
- {
- return this._signInManager.IsSignedIn(claimsPrincipal);
- }
- }
-}