blob: 329d21e9fafc2ff52681852b20e9ebb9fd3be3ee (
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
|
using System.Security.Claims;
using System.Threading.Tasks;
using AutoMapper;
using ExamTemplate.Data.Models;
using ExamTemplate.Data.Repositories;
using ExamTemplate.Services.Models;
using Microsoft.AspNetCore.Identity;
namespace ExamTemplate.Services
{
public class UserService
{
private readonly IMapper _autoMapper;
private readonly UserRepository _userRepository;
private readonly SignInManager<User> _signInManager;
public UserService(IMapper autoMapper, UserRepository userRepository, SignInManager<User> signInManager)
{
this._autoMapper = autoMapper;
this._userRepository = userRepository;
this._signInManager = signInManager;
}
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);
return userCreateResult && addRoleResult;
}
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);
return result.Succeeded;
}
public async Task LogoutAsync()
{
await this._signInManager.SignOutAsync();
}
public async Task<UserServiceModel> GetUserByUsernameAsync(string username)
{
User user = await this._userRepository.GetByUsernameAsync(username);
return this._autoMapper.Map<UserServiceModel>(user);
}
public async Task<UserServiceModel> GetUserByClaimsAsync(ClaimsPrincipal claimsPrincipal)
{
User user = await this._userRepository.GetByClaimsAsync(claimsPrincipal);
return this._autoMapper.Map<UserServiceModel>(user);
}
public async Task<bool> EditUserAsync(ClaimsPrincipal claimsPrincipal, EditUserServiceModel editUserServiceModel)
{
User user = await this._userRepository.GetByClaimsAsync(claimsPrincipal);
user.UserName = editUserServiceModel.Username;
user.FirstName = editUserServiceModel.FirstName;
user.LastName = editUserServiceModel.LastName;
return await this._userRepository.EditUserAsync(user);
}
public bool IsSignedIn(ClaimsPrincipal claimsPrincipal)
{
return this._signInManager.IsSignedIn(claimsPrincipal);
}
}
}
|