blob: 97eb21b3a630570e634e66f884c7febbf997f12d (
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
|
using System.Threading.Tasks;
using ExamTemplate.Data.Models;
using Microsoft.AspNetCore.Identity;
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<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;
}
}
}
|