From 32fdf425e6b4f4edfb727fb3c0cbebe2c87fd663 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 20 Aug 2020 00:39:09 +0800 Subject: ... --- Timeline/Controllers/TokenController.cs | 20 +++++++++++ Timeline/Controllers/UserController.cs | 59 +++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) (limited to 'Timeline/Controllers') diff --git a/Timeline/Controllers/TokenController.cs b/Timeline/Controllers/TokenController.cs index cd67225c..7792b318 100644 --- a/Timeline/Controllers/TokenController.cs +++ b/Timeline/Controllers/TokenController.cs @@ -1,5 +1,6 @@ using AutoMapper; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using System; @@ -13,8 +14,12 @@ using static Timeline.Resources.Controllers.TokenController; namespace Timeline.Controllers { + /// + /// Operation about tokens. + /// [Route("token")] [ApiController] + [ProducesErrorResponseType(typeof(CommonResponse))] public class TokenController : Controller { private readonly IUserTokenManager _userTokenManager; @@ -23,6 +28,7 @@ namespace Timeline.Controllers private readonly IMapper _mapper; + /// public TokenController(IUserTokenManager userTokenManager, ILogger logger, IClock clock, IMapper mapper) { _userTokenManager = userTokenManager; @@ -31,8 +37,15 @@ namespace Timeline.Controllers _mapper = mapper; } + /// + /// Create a new token for a user. + /// + /// Succeed to create token. + /// Error code is 11010101 if user does not exist or password is wrong. [HttpPost("create")] [AllowAnonymous] + [ProducesResponseType(typeof(CreateTokenResponse), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] public async Task> Create([FromBody] CreateTokenRequest request) { void LogFailure(string reason, Exception? e = null) @@ -75,8 +88,15 @@ namespace Timeline.Controllers } } + /// + /// Verify a token. + /// + /// Token is valid. + /// Error code is 11010201 if token is of bad format (it may not be created by this server). Error code is 11010202 if user does not exist. Error code is 11010203 if token is of old version (user may have changed password). Error code is 11010204 if token is expired. [HttpPost("verify")] [AllowAnonymous] + [ProducesResponseType(typeof(VerifyTokenResponse), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] public async Task> Verify([FromBody] VerifyTokenRequest request) { void LogFailure(string reason, Exception? e = null, params (string, object?)[] otherProperties) diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs index 3986bb5b..fa2d37d8 100644 --- a/Timeline/Controllers/UserController.cs +++ b/Timeline/Controllers/UserController.cs @@ -17,7 +17,11 @@ using static Timeline.Resources.Messages; namespace Timeline.Controllers { + /// + /// Operations about users. + /// [ApiController] + [ProducesErrorResponseType(typeof(CommonResponse))] public class UserController : Controller { private readonly ILogger _logger; @@ -25,6 +29,7 @@ namespace Timeline.Controllers private readonly IUserDeleteService _userDeleteService; private readonly IMapper _mapper; + /// public UserController(ILogger logger, IUserService userService, IUserDeleteService userDeleteService, IMapper mapper) { _logger = logger; @@ -35,7 +40,12 @@ namespace Timeline.Controllers private UserInfo ConvertToUserInfo(User user) => _mapper.Map(user); + /// + /// Get all users. + /// + /// The user list. [HttpGet("users")] + [ProducesResponseType(typeof(UserInfo[]), StatusCodes.Status200OK)] public async Task> List() { var users = await _userService.GetUsers(); @@ -43,7 +53,13 @@ namespace Timeline.Controllers return Ok(result); } + /// + /// Get a user info. + /// + /// Username of the user. + /// The user info. [HttpGet("users/{username}")] + [ProducesResponseType(typeof(UserInfo), StatusCodes.Status200OK)] public async Task> Get([FromRoute][Username] string username) { try @@ -58,7 +74,20 @@ namespace Timeline.Controllers } } + /// + /// Change a user's property. You have to be administrator in some condition. + /// + /// + /// Username of the user to change. + /// Succeed to change the user and return the new user info. + /// You have not logged in. + /// You are not administrator. + /// The user to change does not exist. [HttpPatch("users/{username}"), Authorize] + [ProducesResponseType(typeof(UserInfo), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] + [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task> Patch([FromBody] UserPatchRequest body, [FromRoute][Username] string username) { if (this.IsAdministrator()) @@ -101,7 +130,17 @@ namespace Timeline.Controllers } } + /// + /// Delete a user and all his related data. You have to be administrator. + /// + /// Username of the user to delete. + /// Succeeded to delete or the user does not exist. + /// You have not logged in. + /// You are not administrator. [HttpDelete("users/{username}"), AdminAuthorize] + [ProducesResponseType(typeof(CommonDeleteResponse), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] public async Task> Delete([FromRoute][Username] string username) { var delete = await _userDeleteService.DeleteUser(username); @@ -111,7 +150,18 @@ namespace Timeline.Controllers return Ok(CommonDeleteResponse.NotExist()); } + /// + /// Create a new user. You have to be administrator. + /// + /// Succeeded to create a new user and return his user info. + /// Error code is 11020101 if a user with given username already exists. + /// You have not logged in. + /// You are not administrator. [HttpPost("userop/createuser"), AdminAuthorize] + [ProducesResponseType(typeof(UserInfo), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] public async Task> CreateUser([FromBody] CreateUserRequest body) { try @@ -125,7 +175,16 @@ namespace Timeline.Controllers } } + /// + /// Change password with old password. + /// + /// Succeeded to change password. + /// Error code is 11020201 if old password is wrong. + /// You have not logged in. [HttpPost("userop/changepassword"), Authorize] + [ProducesResponseType(typeof(void), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] public async Task ChangePassword([FromBody] ChangePasswordRequest request) { try -- cgit v1.2.3