aboutsummaryrefslogtreecommitdiff
path: root/src/Data/DevHive.Data/Repositories
diff options
context:
space:
mode:
authortranstrike <transtrike@gmail.com>2021-02-16 18:07:51 +0200
committertranstrike <transtrike@gmail.com>2021-02-16 18:07:51 +0200
commit72502154725594cf31878aa944f4bc9d9f3521a3 (patch)
tree6ea1f475f35fba320b32664084d4c39e2d084e49 /src/Data/DevHive.Data/Repositories
parenta2ca63701b71eed2ffa3fff6de16b9babe8bba08 (diff)
downloadDevHive-72502154725594cf31878aa944f4bc9d9f3521a3.tar
DevHive-72502154725594cf31878aa944f4bc9d9f3521a3.tar.gz
DevHive-72502154725594cf31878aa944f4bc9d9f3521a3.zip
UserManager&RoleManager logic moved to Repo; Password hashing and validation moved to Repo and using ASPNET Core hashing methods; Added Migrations; Fixed Roles not added to user
Diffstat (limited to 'src/Data/DevHive.Data/Repositories')
-rw-r--r--src/Data/DevHive.Data/Repositories/RoleRepository.cs37
-rw-r--r--src/Data/DevHive.Data/Repositories/UserRepository.cs82
2 files changed, 82 insertions, 37 deletions
diff --git a/src/Data/DevHive.Data/Repositories/RoleRepository.cs b/src/Data/DevHive.Data/Repositories/RoleRepository.cs
index 7d07c1b..99e0634 100644
--- a/src/Data/DevHive.Data/Repositories/RoleRepository.cs
+++ b/src/Data/DevHive.Data/Repositories/RoleRepository.cs
@@ -2,53 +2,54 @@ using System;
using System.Threading.Tasks;
using DevHive.Data.Interfaces;
using DevHive.Data.Models;
+using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
namespace DevHive.Data.Repositories
{
public class RoleRepository : BaseRepository<Role>, IRoleRepository
{
- private readonly DevHiveContext _context;
+ private readonly RoleManager<Role> _roleManager;
- public RoleRepository(DevHiveContext context)
+ public RoleRepository(DevHiveContext context, RoleManager<Role> roleManager)
: base(context)
{
- this._context = context;
+ this._roleManager = roleManager;
}
+ #region Create
+ public override async Task<bool> AddAsync(Role entity)
+ {
+ IdentityResult result = await this._roleManager.CreateAsync(entity);
+
+ return result.Succeeded;
+ }
+ #endregion
+
#region Read
public async Task<Role> GetByNameAsync(string name)
{
- return await this._context.Roles
- .FirstOrDefaultAsync(x => x.Name == name);
+ return await this._roleManager.FindByNameAsync(name);
}
#endregion
public override async Task<bool> EditAsync(Guid id, Role newEntity)
{
- Role role = await this.GetByIdAsync(id);
-
- this._context
- .Entry(role)
- .CurrentValues
- .SetValues(newEntity);
+ newEntity.Id = id;
+ IdentityResult result = await this._roleManager.UpdateAsync(newEntity);
- return await this.SaveChangesAsync();
+ return result.Succeeded;
}
#region Validations
public async Task<bool> DoesNameExist(string name)
{
- return await this._context.Roles
- .AsNoTracking()
- .AnyAsync(r => r.Name == name);
+ return await this._roleManager.RoleExistsAsync(name);
}
public async Task<bool> DoesRoleExist(Guid id)
{
- return await this._context.Roles
- .AsNoTracking()
- .AnyAsync(r => r.Id == id);
+ return await this._roleManager.Roles.AnyAsync(r => r.Id == id);
}
#endregion
}
diff --git a/src/Data/DevHive.Data/Repositories/UserRepository.cs b/src/Data/DevHive.Data/Repositories/UserRepository.cs
index 9fa6eca..d570480 100644
--- a/src/Data/DevHive.Data/Repositories/UserRepository.cs
+++ b/src/Data/DevHive.Data/Repositories/UserRepository.cs
@@ -1,12 +1,8 @@
using System;
using System.Collections.Generic;
-using System.Globalization;
-using System.Linq;
using System.Threading.Tasks;
-using AutoMapper.Mappers;
using DevHive.Data.Interfaces;
using DevHive.Data.Models;
-using DevHive.Data.Models.Relational;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
@@ -14,18 +10,42 @@ namespace DevHive.Data.Repositories
{
public class UserRepository : BaseRepository<User>, IUserRepository
{
- private readonly DevHiveContext _context;
+ private readonly UserManager<User> _userManager;
+ private readonly RoleManager<Role> _roleManager;
- public UserRepository(DevHiveContext context)
+ public UserRepository(DevHiveContext context, UserManager<User> userManager, RoleManager<Role> roleManager)
: base(context)
{
- this._context = context;
+ this._userManager = userManager;
+ this._roleManager = roleManager;
}
+ #region Create
+ public override async Task<bool> AddAsync(User entity)
+ {
+ entity.PasswordHash = this._userManager.PasswordHasher.HashPassword(entity, entity.PasswordHash).ToString();
+ IdentityResult result = await this._userManager.CreateAsync(entity);
+
+ return result.Succeeded;
+ }
+
+ public async Task<bool> AddRoleToUser(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.SaveChangesAsync();
+ }
+
+ return succeeded;
+ }
+ #endregion
+
#region Read
public override async Task<User> GetByIdAsync(Guid id)
{
- return await this._context.Users
+ return await this._userManager.Users
.Include(x => x.Roles)
.Include(x => x.Languages)
.Include(x => x.Technologies)
@@ -37,7 +57,7 @@ namespace DevHive.Data.Repositories
public async Task<User> GetByUsernameAsync(string username)
{
- return await this._context.Users
+ return await this._userManager.Users
.Include(x => x.Roles)
.Include(x => x.Languages)
.Include(x => x.Technologies)
@@ -49,6 +69,14 @@ namespace DevHive.Data.Repositories
#endregion
#region Update
+ public override async Task<bool> EditAsync(Guid id, User newEntity)
+ {
+ newEntity.Id = id;
+ IdentityResult result = await this._userManager.UpdateAsync(newEntity);
+
+ return result.Succeeded;
+ }
+
public async Task<bool> UpdateProfilePicture(Guid userId, string pictureUrl)
{
User user = await this.GetByIdAsync(userId);
@@ -59,24 +87,41 @@ namespace DevHive.Data.Repositories
}
#endregion
+ #region Delete
+ public override async Task<bool> DeleteAsync(User entity)
+ {
+ IdentityResult result = await this._userManager.DeleteAsync(entity);
+
+ return result.Succeeded;
+ }
+ #endregion
+
#region Validations
+ public async Task<bool> VerifyPassword(User user, string password)
+ {
+ return await this._userManager.CheckPasswordAsync(user, password);
+ }
+
+ public async Task<bool> IsInRoleAsync(User user, string roleName)
+ {
+ return await this._userManager.IsInRoleAsync(user, roleName);
+ }
+
public async Task<bool> DoesUserExistAsync(Guid id)
{
- return await this._context.Users
- .AsNoTracking()
- .AnyAsync(x => x.Id == id);
+ return await this._userManager.Users.AnyAsync(x => x.Id == id);
}
public async Task<bool> DoesUsernameExistAsync(string username)
{
- return await this._context.Users
+ return await this._userManager.Users
.AsNoTracking()
.AnyAsync(u => u.UserName == username);
}
public async Task<bool> DoesEmailExistAsync(string email)
{
- return await this._context.Users
+ return await this._userManager.Users
.AsNoTracking()
.AnyAsync(u => u.Email == email);
}
@@ -87,7 +132,7 @@ namespace DevHive.Data.Repositories
foreach (var username in usernames)
{
- if (!await this._context.Users.AnyAsync(x => x.UserName == username))
+ if (!await this.DoesUsernameExistAsync(username))
{
valid = false;
break;
@@ -96,11 +141,10 @@ namespace DevHive.Data.Repositories
return valid;
}
- public bool DoesUserHaveThisUsername(Guid id, string username)
+ public async Task<bool> DoesUserHaveThisUsernameAsync(Guid id, string username)
{
- return this._context.Users
- .AsNoTracking()
- .Any(x => x.Id == id &&
+ return await this._userManager.Users
+ .AnyAsync(x => x.Id == id &&
x.UserName == username);
}
#endregion