aboutsummaryrefslogtreecommitdiff
path: root/src/Web/DevHive.Web/Controllers/UserController.cs
diff options
context:
space:
mode:
authorSyndamia <kamen.d.mladenov@protonmail.com>2021-03-13 15:07:51 +0200
committerSyndamia <kamen.d.mladenov@protonmail.com>2021-03-13 15:07:51 +0200
commit75d57f305f2ed1ecf8b82bd62d4bb8f17c06303b (patch)
treeb721fe307d22a7a89f8ee13b3e557df4a2e2bbab /src/Web/DevHive.Web/Controllers/UserController.cs
parent503a23c04355624b133161c9356b139f2e4500f6 (diff)
parent4add0831649f6e534d3883aa0e0e7f380d8042c7 (diff)
downloadDevHive-75d57f305f2ed1ecf8b82bd62d4bb8f17c06303b.tar
DevHive-75d57f305f2ed1ecf8b82bd62d4bb8f17c06303b.tar.gz
DevHive-75d57f305f2ed1ecf8b82bd62d4bb8f17c06303b.zip
Merge branch 'dev' of github.com:Team-Kaleidoscope/DevHive into unit_test_refactoring
Diffstat (limited to 'src/Web/DevHive.Web/Controllers/UserController.cs')
-rw-r--r--src/Web/DevHive.Web/Controllers/UserController.cs79
1 files changed, 56 insertions, 23 deletions
diff --git a/src/Web/DevHive.Web/Controllers/UserController.cs b/src/Web/DevHive.Web/Controllers/UserController.cs
index 214fba7..4d01447 100644
--- a/src/Web/DevHive.Web/Controllers/UserController.cs
+++ b/src/Web/DevHive.Web/Controllers/UserController.cs
@@ -7,26 +7,40 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using DevHive.Common.Models.Identity;
using DevHive.Services.Interfaces;
+using DevHive.Common.Jwt.Interfaces;
+using NSwag.Annotations;
namespace DevHive.Web.Controllers
{
+ /// <summary>
+ /// All endpoints for integration with the User
+ /// </summary>
[ApiController]
[Route("/api/[controller]")]
+ [OpenApiController("User Controller")]
public class UserController : ControllerBase
{
private readonly IUserService _userService;
private readonly IMapper _userMapper;
+ private readonly IJwtService _jwtService;
- public UserController(IUserService userService, IMapper mapper)
+ public UserController(IUserService userService, IMapper mapper, IJwtService jwtService)
{
this._userService = userService;
this._userMapper = mapper;
+ this._jwtService = jwtService;
}
#region Authentication
+ /// <summary>
+ /// Login endpoint for the DevHive Social Platform
+ /// </summary>
+ /// <param name="loginModel">Login model with username and password</param>
+ /// <returns>A JWT Token for further validation</returns>
[HttpPost]
- [Route("Login")]
[AllowAnonymous]
+ [Route("Login")]
+ [OpenApiTags("Authorization")]
public async Task<IActionResult> Login([FromBody] LoginWebModel loginModel)
{
LoginServiceModel loginServiceModel = this._userMapper.Map<LoginServiceModel>(loginModel);
@@ -37,9 +51,15 @@ namespace DevHive.Web.Controllers
return new OkObjectResult(tokenWebModel);
}
+ /// <summary>
+ /// Register a new User in the DevHive Social Platform
+ /// </summary>
+ /// <param name="registerModel">Register model with the new data to provide</param>
+ /// <returns>A JWT Token for further validation</returns>
[HttpPost]
- [Route("Register")]
[AllowAnonymous]
+ [Route("Register")]
+ [OpenApiTag("Authorization")]
public async Task<IActionResult> Register([FromBody] RegisterWebModel registerModel)
{
RegisterServiceModel registerServiceModel = this._userMapper.Map<RegisterServiceModel>(registerModel);
@@ -52,11 +72,17 @@ namespace DevHive.Web.Controllers
#endregion
#region Read
+ /// <summary>
+ /// Get a User's information using the Guid
+ /// </summary>
+ /// <param name="id">User's Id</param>
+ /// <param name="authorization">The JWT Token, contained in the header and used for validation</param>
+ /// <returns>A full User's read model</returns>
[HttpGet]
[Authorize(Roles = "User,Admin")]
public async Task<IActionResult> GetById(Guid id, [FromHeader] string authorization)
{
- if (!await this._userService.ValidJWT(id, authorization))
+ if (!this._jwtService.ValidateToken(id, authorization))
return new UnauthorizedResult();
UserServiceModel userServiceModel = await this._userService.GetUserById(id);
@@ -65,6 +91,11 @@ namespace DevHive.Web.Controllers
return new OkObjectResult(userWebModel);
}
+ /// <summary>
+ /// Get a User's profile using his username. Does NOT require authorization
+ /// </summary>
+ /// <param name="username">User's username</param>
+ /// <returns>A trimmed version of the full User's read model</returns>
[HttpGet]
[Route("GetUser")]
[AllowAnonymous]
@@ -78,11 +109,18 @@ namespace DevHive.Web.Controllers
#endregion
#region Update
+ /// <summary>
+ /// Full update on User's data. A PUSTINQK can only edit his account
+ /// </summary>
+ /// <param name="id">The User's Id</param>
+ /// <param name="updateUserWebModel">A full User update model</param>
+ /// <param name="authorization">The JWT Token, contained in the header and used for validation</param>
+ /// <returns>A full User's read model</returns>
[HttpPut]
[Authorize(Roles = "User,Admin")]
public async Task<IActionResult> Update(Guid id, [FromBody] UpdateUserWebModel updateUserWebModel, [FromHeader] string authorization)
{
- if (!await this._userService.ValidJWT(id, authorization))
+ if (!this._jwtService.ValidateToken(id, authorization))
return new UnauthorizedResult();
UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map<UpdateUserServiceModel>(updateUserWebModel);
@@ -93,31 +131,20 @@ namespace DevHive.Web.Controllers
return new AcceptedResult("UpdateUser", userWebModel);
}
-
- [HttpPut]
- [Route("ProfilePicture")]
- [Authorize(Roles = "User,Admin")]
- public async Task<IActionResult> UpdateProfilePicture(Guid userId, [FromForm] UpdateProfilePictureWebModel updateProfilePictureWebModel, [FromHeader] string authorization)
- {
- if (!await this._userService.ValidJWT(userId, authorization))
- return new UnauthorizedResult();
-
- UpdateProfilePictureServiceModel updateProfilePictureServiceModel = this._userMapper.Map<UpdateProfilePictureServiceModel>(updateProfilePictureWebModel);
- updateProfilePictureServiceModel.UserId = userId;
-
- ProfilePictureServiceModel profilePictureServiceModel = await this._userService.UpdateProfilePicture(updateProfilePictureServiceModel);
- ProfilePictureWebModel profilePictureWebModel = this._userMapper.Map<ProfilePictureWebModel>(profilePictureServiceModel);
-
- return new AcceptedResult("UpdateProfilePicture", profilePictureWebModel);
- }
#endregion
#region Delete
+ /// <summary>
+ /// Delete a User with his Id. A PUSTINQK can only delete his account. An Admin can delete all accounts
+ /// </summary>
+ /// <param name="id">The User's Id</param>
+ /// <param name="authorization">The JWT Token, contained in the header and used for validation</param>
+ /// <returns>Ok, BadRequest or Unauthorized</returns>
[HttpDelete]
[Authorize(Roles = "User,Admin")]
public async Task<IActionResult> Delete(Guid id, [FromHeader] string authorization)
{
- if (!await this._userService.ValidJWT(id, authorization))
+ if (!this._jwtService.ValidateToken(id, authorization))
return new UnauthorizedResult();
bool result = await this._userService.DeleteUser(id);
@@ -128,7 +155,13 @@ namespace DevHive.Web.Controllers
}
#endregion
+ /// <summary>
+ /// We don't talk about that, NIGGA!
+ /// </summary>
+ /// <param name="userId"></param>
+ /// <returns></returns>
[HttpPost]
+ [OpenApiIgnore]
[Authorize(Roles = "User,Admin")]
[Route("SuperSecretPromotionToAdmin")]
public async Task<IActionResult> SuperSecretPromotionToAdmin(Guid userId)