aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Services/Services/UserService.cs
diff options
context:
space:
mode:
authortranstrike <transtrike@gmail.com>2021-01-21 09:23:06 +0200
committertranstrike <transtrike@gmail.com>2021-01-21 09:23:06 +0200
commit009e01dc3dc2f78db6a660c65bf0d20bae702348 (patch)
tree04d792a08f13e9bfe23841b393439dcd37ab8b16 /src/DevHive.Services/Services/UserService.cs
parent8179af787a7bf375753a178b89111a91d84a8bb8 (diff)
downloadDevHive-009e01dc3dc2f78db6a660c65bf0d20bae702348.tar
DevHive-009e01dc3dc2f78db6a660c65bf0d20bae702348.tar.gz
DevHive-009e01dc3dc2f78db6a660c65bf0d20bae702348.zip
Tryed implementing the http patch
Diffstat (limited to 'src/DevHive.Services/Services/UserService.cs')
-rw-r--r--src/DevHive.Services/Services/UserService.cs78
1 files changed, 41 insertions, 37 deletions
diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs
index 51c4432..629b489 100644
--- a/src/DevHive.Services/Services/UserService.cs
+++ b/src/DevHive.Services/Services/UserService.cs
@@ -7,15 +7,14 @@ using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Microsoft.IdentityModel.Tokens;
-using System.Security.Cryptography;
using System.Text;
using System.Collections.Generic;
using DevHive.Common.Models.Identity;
using DevHive.Services.Interfaces;
using DevHive.Data.Interfaces.Repositories;
-using Microsoft.AspNetCore.JsonPatch;
using System.Linq;
-using Newtonsoft.Json;
+using DevHive.Common.Models.Misc;
+using System.Reflection;
namespace DevHive.Services.Services
{
@@ -52,7 +51,7 @@ namespace DevHive.Services.Services
User user = await this._userRepository.GetByUsernameAsync(loginModel.UserName);
- if (user.PasswordHash != GeneratePasswordHash(loginModel.Password))
+ if (user.PasswordHash != PasswordModifications.GeneratePasswordHash(loginModel.Password))
throw new ArgumentException("Incorrect password!");
return new TokenModel(WriteJWTSecurityToken(user.Id, user.Roles));
@@ -67,7 +66,7 @@ namespace DevHive.Services.Services
throw new ArgumentException("Email already exists!");
User user = this._userMapper.Map<User>(registerModel);
- user.PasswordHash = GeneratePasswordHash(registerModel.Password);
+ user.PasswordHash = PasswordModifications.GeneratePasswordHash(registerModel.Password);
// Make sure the default role exists
if (!await this._roleRepository.DoesNameExist(Role.DefaultRole))
@@ -135,6 +134,7 @@ namespace DevHive.Services.Services
public async Task<UserServiceModel> UpdateUser(UpdateUserServiceModel updateUserServiceModel)
{
+ //Method: ValidateUserOnUpdate
if (!await this._userRepository.DoesUserExistAsync(updateUserServiceModel.Id))
throw new ArgumentException("User does not exist!");
@@ -144,6 +144,7 @@ namespace DevHive.Services.Services
await this.ValidateUserCollections(updateUserServiceModel);
+ //Method: Insert collections to user
HashSet<Language> languages = new();
foreach (UpdateUserCollectionServiceModel lang in updateUserServiceModel.Languages)
languages.Add(await this._languageRepository.GetByNameAsync(lang.Name) ??
@@ -159,51 +160,35 @@ namespace DevHive.Services.Services
user.Languages = languages;
user.Technologies = technologies;
- bool success = await this._userRepository.EditAsync(user);
+ bool successful = await this._userRepository.EditAsync(user);
- if (!success)
+ if (!successful)
throw new InvalidOperationException("Unable to edit user!");
return this._userMapper.Map<UserServiceModel>(user); ;
}
- public async Task<UserServiceModel> PatchUser(Guid id, JsonPatchDocument<User> jsonPatch)
+ public async Task<UserServiceModel> PatchUser(Guid id, List<Patch> patchList)
{
User user = await this._userRepository.GetByIdAsync(id) ??
throw new ArgumentException("User does not exist!");
- object password = jsonPatch.Operations
- .Where(x => x.path == "/password")
- .Select(x => x.value)
- .FirstOrDefault();
-
- IEnumerable<object> friends = jsonPatch.Operations
- .Where(x => x.path == "/friends")
- .Select(x => x.value);
-
- if(password != null)
- {
- string passwordHash = this.GeneratePasswordHash(password.ToString());
- user.PasswordHash = passwordHash;
- }
+ UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map<UpdateUserServiceModel>(user);
- if (friends != null)
+ foreach (Patch patch in patchList)
{
- foreach (object friendObj in friends)
+ bool successful = patch.Action switch
{
- FriendServiceModel friendServiceModel =
- JsonConvert.DeserializeObject<FriendServiceModel>(friendObj.ToString());
-
- User amigo = await this._userRepository.GetByUsernameAsync(friendServiceModel.UserName)
- ?? throw new ArgumentException($"User {friendServiceModel.UserName} does not exist!");
-
- user.Friends.Add(amigo);
- }
+ "replace" => ReplacePatch(updateUserServiceModel, patch),
+ "add" => AddPatch(updateUserServiceModel, patch),
+ "remove" => RemovePatch(updateUserServiceModel, patch),
+ _ => throw new ArgumentException("Invalid patch operation!"),
+ };
+
+ if (!successful)
+ throw new ArgumentException("A problem occurred while applying patch");
}
- //Remove password and friends peace from the request patch before applying the rest
- // jsonPatch.ApplyTo(user);
-
bool success = await this._userRepository.EditAsync(user);
if (success)
{
@@ -326,6 +311,11 @@ namespace DevHive.Services.Services
}
}
+ private async Task ValidateUserOnUpdate(UpdateUserServiceModel updateUserServiceModel)
+ {
+
+ }
+
private string WriteJWTSecurityToken(Guid userId, HashSet<Role> roles)
{
byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret);
@@ -354,9 +344,23 @@ namespace DevHive.Services.Services
return tokenHandler.WriteToken(token);
}
- private string GeneratePasswordHash(string password)
+ private bool AddPatch(UpdateUserServiceModel updateUserServiceModel, Patch patch)
+ {
+ // Type type = typeof(UpdateUserServiceModel);
+ // PropertyInfo property = type.GetProperty(patch.Name);
+
+ // property.SetValue(updateUserServiceModel, patch.Value);
+ throw new NotImplementedException();
+ }
+
+ private bool RemovePatch(UpdateUserServiceModel updateUserServiceModel, Patch patch)
+ {
+ throw new NotImplementedException();
+ }
+
+ private bool ReplacePatch(UpdateUserServiceModel updateUserServiceModel, Patch patch)
{
- return string.Join(string.Empty, SHA512.HashData(Encoding.ASCII.GetBytes(password)));
+ throw new NotImplementedException();
}
#endregion
}