blob: 8e5c68253d854b4bd6fd26f88b2b0fbfbd5544eb (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
using System;
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<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);
}
}
}
|