diff options
Diffstat (limited to 'ExamTemplate/Data/Repositories/UserRepository.cs')
| -rw-r--r-- | ExamTemplate/Data/Repositories/UserRepository.cs | 66 |
1 files changed, 0 insertions, 66 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); - } - } -} |
