diff options
Diffstat (limited to 'src/DevHive.Services')
8 files changed, 190 insertions, 1 deletions
diff --git a/src/DevHive.Services/DevHive.Services.csproj b/src/DevHive.Services/DevHive.Services.csproj index 563e6f9..277a342 100644 --- a/src/DevHive.Services/DevHive.Services.csproj +++ b/src/DevHive.Services/DevHive.Services.csproj @@ -1,7 +1,14 @@ <Project Sdk="Microsoft.NET.Sdk">
-
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\DevHive.Data\DevHive.Data.csproj" />
+
+ <PackageReference Include="AutoMapper" Version="10.1.1" />
+ <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.0" />
+ </ItemGroup>
+
+
</Project>
diff --git a/src/DevHive.Services/Models/Identity/LoginServiceModel.cs b/src/DevHive.Services/Models/Identity/LoginServiceModel.cs new file mode 100644 index 0000000..d056f9a --- /dev/null +++ b/src/DevHive.Services/Models/Identity/LoginServiceModel.cs @@ -0,0 +1,8 @@ +namespace DevHive.Services.Models.Identity +{ + public class LoginServiceModel + { + public string UserName { get; set; } + public string Password { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/RegisterServiceModel.cs b/src/DevHive.Services/Models/Identity/RegisterServiceModel.cs new file mode 100644 index 0000000..45f42d2 --- /dev/null +++ b/src/DevHive.Services/Models/Identity/RegisterServiceModel.cs @@ -0,0 +1,11 @@ +namespace DevHive.Services.Models.Identity +{ + public class RegisterServiceModel + { + public string UserName { get; set; } + public string Email { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public string Password { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/UpdateUserServiceModel.cs b/src/DevHive.Services/Models/Identity/UpdateUserServiceModel.cs new file mode 100644 index 0000000..94600ae --- /dev/null +++ b/src/DevHive.Services/Models/Identity/UpdateUserServiceModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Services.Models.Identity +{ + public class UpdateUserServiceModel : UserServiceModel + { + public Guid Id { get; set; } + } +}
\ No newline at end of file diff --git a/src/DevHive.Services/Models/Identity/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/UserServiceModel.cs new file mode 100644 index 0000000..387afdd --- /dev/null +++ b/src/DevHive.Services/Models/Identity/UserServiceModel.cs @@ -0,0 +1,11 @@ +namespace DevHive.Services.Models.Identity +{ + public class UserServiceModel + { + public string UserName { get; set; } + public string Email { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public string Role { get; set;} + } +} diff --git a/src/DevHive.Services/Options/JWTOptions.cs b/src/DevHive.Services/Options/JWTOptions.cs new file mode 100644 index 0000000..95458f5 --- /dev/null +++ b/src/DevHive.Services/Options/JWTOptions.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.Options; + +namespace DevHive.Services.Options +{ + public class JWTOptions + { + public JWTOptions(string secret) + { + this.Secret = secret; + } + + public string Secret { get; init; } + } +} diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs new file mode 100644 index 0000000..202c611 --- /dev/null +++ b/src/DevHive.Services/Services/RoleService.cs @@ -0,0 +1,26 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Repositories; + +namespace DevHive.Services.Services +{ + public class RoleService + { + /* private readonly DevHiveContext _context; + + public RoleService(DevHiveContext context) + { + this._context = context; + } + + public Task<IActionResult> CreatePost(string name) + { + throw new NotImplementedException(); + } + + public Task<IActionResult> GetPostById(uint postId) + { + throw new NotImplementedException(); + }*/ + } +} diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs new file mode 100644 index 0000000..f06198c --- /dev/null +++ b/src/DevHive.Services/Services/UserService.cs @@ -0,0 +1,103 @@ +namespace DevHive.Services.Services +{ + public class UserService + { + /* private readonly UserRepository _userDbRepository; + private readonly IMapper _userMapper; + private readonly JWTOptions _jwtOptions; + + public UserService(DevHiveContext context, IMapper mapper, JWTOptions jwtOptions) + { + this._userDbRepository = new UserRepository(context); + this._userMapper = mapper; + this._jwtOptions = jwtOptions; + } + + public async Task<IActionResult> LoginUser(LoginDTO loginDTO) + { + User user = this._userDbRepository.FindByUsername(loginDTO.UserName); + + if (user == null) + return new NotFoundObjectResult("User does not exist!"); + + byte[] key = Encoding.ASCII.GetBytes(_jwtOptions.Secret); + + if (user.PasswordHash != GeneratePasswordHash(loginDTO.Password)) + return new BadRequestObjectResult("Incorrect password!"); + + // Create Jwt Token configuration + var tokenDescriptor = new SecurityTokenDescriptor + { + Subject = new ClaimsIdentity(new Claim[] + { + new Claim(ClaimTypes.Role, user.Role) // Authorize user by role + }), + Expires = DateTime.UtcNow.AddDays(7), + SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha512Signature) + }; + + var tokenHandler = new JwtSecurityTokenHandler(); + var token = tokenHandler.CreateToken(tokenDescriptor); + var tokenString = tokenHandler.WriteToken(token); + + return new OkObjectResult(new { Token = tokenString }); + } + + public async Task<IActionResult> RegisterUser(RegisterDTO registerDTO) + { + + if (this._userDbRepository.DoesUsernameExist(registerDTO.UserName)) + return new BadRequestObjectResult("Username already exists!"); + + User user = this._userMapper.Map<User>(registerDTO); + + user.Role = UserRoles.User; + user.PasswordHash = GeneratePasswordHash(registerDTO.Password); + + await this._userDbRepository.AddAsync(user); + + return new CreatedResult("CreateUser", user); + } + + private string GeneratePasswordHash(string password) + { + //TODO: Implement + return password; + } + + public async Task<IActionResult> GetUserById(Guid id) + { + User user = await this._userDbRepository.FindByIdAsync(id); + + if (user == null) + return new NotFoundObjectResult("User does not exist!"); + + return new OkObjectResult(user); + } + + public async Task<IActionResult> UpdateUser(Guid id, UserDTO userDTO) + { + if (!this._userDbRepository.DoesUserExist(id)) + return new NotFoundObjectResult("User does not exist!"); + + if (!this._userDbRepository.HasThisUsername(id, userDTO.UserName) + && this._userDbRepository.DoesUsernameExist(userDTO.UserName)) + return new BadRequestObjectResult("Username already exists!"); + + User user = this._userMapper.Map<User>(userDTO); + await this._userDbRepository.EditAsync(id, user); + + return new AcceptedResult("UpdateUser", user); + } + + public async Task<IActionResult> DeleteUser(Guid id) + { + if (!this._userDbRepository.DoesUserExist(id)) + return new NotFoundObjectResult("User does not exist!"); + + await this._userDbRepository.DeleteAsync(id); + + return new OkResult(); + }*/ + } +} |
