diff options
author | crupest <crupest@outlook.com> | 2019-04-13 13:06:05 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-04-13 13:06:05 +0800 |
commit | 962ad53360fb559eaed1ee5a45ef361e6f580bd7 (patch) | |
tree | 8ecd711d2b1c97754b321ef0e0bc7de45d303cbd /Timeline/Controllers | |
parent | 72890735ced2edc8ccecfed811393e951de5c091 (diff) | |
parent | 1d184c3f41da806803c1ee792395eabcd155077d (diff) | |
download | timeline-962ad53360fb559eaed1ee5a45ef361e6f580bd7.tar.gz timeline-962ad53360fb559eaed1ee5a45ef361e6f580bd7.tar.bz2 timeline-962ad53360fb559eaed1ee5a45ef361e6f580bd7.zip |
Merge branch '6-user' into separate
Diffstat (limited to 'Timeline/Controllers')
-rw-r--r-- | Timeline/Controllers/UserController.cs | 52 | ||||
-rw-r--r-- | Timeline/Controllers/UserTestController.cs | 4 |
2 files changed, 43 insertions, 13 deletions
diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs index b9e0979d..3b4e7b4f 100644 --- a/Timeline/Controllers/UserController.cs +++ b/Timeline/Controllers/UserController.cs @@ -1,6 +1,8 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; +using System; +using System.Threading.Tasks; using Timeline.Entities; using Timeline.Services; @@ -16,23 +18,22 @@ namespace Timeline.Controllers } private readonly IUserService _userService; - private readonly IJwtService _jwtService; private readonly ILogger<UserController> _logger; - public UserController(IUserService userService, IJwtService jwtService, ILogger<UserController> logger) + public UserController(IUserService userService, ILogger<UserController> logger) { _userService = userService; - _jwtService = jwtService; _logger = logger; } [HttpPost("[action]")] [AllowAnonymous] - public ActionResult<CreateTokenResponse> CreateToken([FromBody] CreateTokenRequest request) + public async Task<ActionResult<CreateTokenResponse>> CreateToken([FromBody] CreateTokenRequest request) { - var user = _userService.Authenticate(request.Username, request.Password); + var result = await _userService.CreateToken(request.Username, request.Password); - if (user == null) { + if (result == null) + { _logger.LogInformation(LoggingEventIds.LogInFailed, "Attemp to login with username: {} and password: {} failed.", request.Username, request.Password); return Ok(new CreateTokenResponse { @@ -45,17 +46,46 @@ namespace Timeline.Controllers return Ok(new CreateTokenResponse { Success = true, - Token = _jwtService.GenerateJwtToken(user), - UserInfo = user.GetUserInfo() + Token = result.Token, + UserInfo = result.UserInfo }); } [HttpPost("[action]")] [AllowAnonymous] - public ActionResult<TokenValidationResponse> ValidateToken([FromBody] TokenValidationRequest request) + public async Task<ActionResult<TokenValidationResponse>> ValidateToken([FromBody] TokenValidationRequest request) { - var result = _jwtService.ValidateJwtToken(request.Token); - return Ok(result); + var result = await _userService.VerifyToken(request.Token); + + if (result == null) + { + return Ok(new TokenValidationResponse + { + IsValid = false, + }); + } + + return Ok(new TokenValidationResponse + { + IsValid = true, + UserInfo = result + }); + } + + [HttpPost("[action]")] + [Authorize(Roles = "admin")] + public async Task<ActionResult<CreateUserResponse>> CreateUser([FromBody] CreateUserRequest request) + { + var result = await _userService.CreateUser(request.Username, request.Password, request.Roles); + switch (result) + { + case CreateUserResult.Success: + return Ok(new CreateUserResponse { ReturnCode = CreateUserResponse.SuccessCode }); + case CreateUserResult.AlreadyExists: + return Ok(new CreateUserResponse { ReturnCode = CreateUserResponse.AlreadyExistsCode }); + default: + throw new Exception("Unreachable code."); + } } } } diff --git a/Timeline/Controllers/UserTestController.cs b/Timeline/Controllers/UserTestController.cs index 1c230667..f1edb0d5 100644 --- a/Timeline/Controllers/UserTestController.cs +++ b/Timeline/Controllers/UserTestController.cs @@ -14,14 +14,14 @@ namespace Timeline.Controllers } [HttpGet("[action]")] - [Authorize(Roles = "User,Admin")] + [Authorize(Roles = "user,admin")] public ActionResult BothUserAndAdmin() { return Ok(); } [HttpGet("[action]")] - [Authorize(Roles = "Admin")] + [Authorize(Roles = "admin")] public ActionResult OnlyAdmin() { return Ok(); |