aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Controllers
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-10-23 20:41:19 +0800
committer杨宇千 <crupest@outlook.com>2019-10-23 20:41:19 +0800
commitb67a26248d5dde4c3909c29b92b8a182248bdcc1 (patch)
treeb005aa3d8bc34d8e710ce7fae30236c62dcbe712 /Timeline/Controllers
parent9c9762b4ecbd816be98ee0dd606fe15cc253b415 (diff)
downloadtimeline-b67a26248d5dde4c3909c29b92b8a182248bdcc1.tar.gz
timeline-b67a26248d5dde4c3909c29b92b8a182248bdcc1.tar.bz2
timeline-b67a26248d5dde4c3909c29b92b8a182248bdcc1.zip
...
Diffstat (limited to 'Timeline/Controllers')
-rw-r--r--Timeline/Controllers/Testing/TestingAuthController.cs2
-rw-r--r--Timeline/Controllers/UserAvatarController.cs6
-rw-r--r--Timeline/Controllers/UserController.cs44
3 files changed, 20 insertions, 32 deletions
diff --git a/Timeline/Controllers/Testing/TestingAuthController.cs b/Timeline/Controllers/Testing/TestingAuthController.cs
index 488a3cff..67b5b2ef 100644
--- a/Timeline/Controllers/Testing/TestingAuthController.cs
+++ b/Timeline/Controllers/Testing/TestingAuthController.cs
@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
-using Timeline.Authenticate;
+using Timeline.Authentication;
namespace Timeline.Controllers.Testing
{
diff --git a/Timeline/Controllers/UserAvatarController.cs b/Timeline/Controllers/UserAvatarController.cs
index e77076ca..5cba1d93 100644
--- a/Timeline/Controllers/UserAvatarController.cs
+++ b/Timeline/Controllers/UserAvatarController.cs
@@ -6,7 +6,7 @@ using Microsoft.Net.Http.Headers;
using System;
using System.Linq;
using System.Threading.Tasks;
-using Timeline.Authenticate;
+using Timeline.Authentication;
using Timeline.Filters;
using Timeline.Models.Http;
using Timeline.Services;
@@ -106,7 +106,7 @@ namespace Timeline.Controllers
return BadRequest(new CommonResponse(ErrorCodes.Put_Content_TooBig,
"Content can't be bigger than 10MB."));
- if (!User.IsAdmin() && User.Identity.Name != username)
+ if (!User.IsAdministrator() && User.Identity.Name != username)
{
_logger.LogInformation($"Attempt to put a avatar of other user as a non-admin failed. Operator Username: {User.Identity.Name} ; Username To Put Avatar: {username} .");
return StatusCode(StatusCodes.Status403Forbidden,
@@ -152,7 +152,7 @@ namespace Timeline.Controllers
[Authorize]
public async Task<IActionResult> Delete([FromRoute] string username)
{
- if (!User.IsAdmin() && User.Identity.Name != username)
+ if (!User.IsAdministrator() && User.Identity.Name != username)
{
_logger.LogInformation($"Attempt to delete a avatar of other user as a non-admin failed. Operator Username: {User.Identity.Name} ; Username To Put Avatar: {username} .");
return StatusCode(StatusCodes.Status403Forbidden,
diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs
index b8d1d659..1771dc85 100644
--- a/Timeline/Controllers/UserController.cs
+++ b/Timeline/Controllers/UserController.cs
@@ -3,10 +3,11 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
-using Timeline.Authenticate;
+using Timeline.Authentication;
using Timeline.Helpers;
using Timeline.Models;
using Timeline.Models.Http;
+using Timeline.Models.Validation;
using Timeline.Services;
using static Timeline.Resources.Controllers.UserController;
@@ -23,11 +24,6 @@ namespace Timeline
public const int NotExist = 10020101; // dd = 01
}
- public static class Put // cc = 02
- {
- public const int BadUsername = 10020201; // dd = 01
- }
-
public static class Patch // cc = 03
{
public const int NotExist = 10020301; // dd = 01
@@ -78,7 +74,7 @@ namespace Timeline.Controllers
}
[HttpGet("users/{username}"), AdminAuthorize]
- public async Task<ActionResult<UserInfo>> Get([FromRoute] string username)
+ public async Task<ActionResult<UserInfo>> Get([FromRoute][Username] string username)
{
var user = await _userService.GetUser(username);
if (user == null)
@@ -90,32 +86,24 @@ namespace Timeline.Controllers
}
[HttpPut("users/{username}"), AdminAuthorize]
- public async Task<ActionResult<CommonPutResponse>> Put([FromBody] UserPutRequest request, [FromRoute] string username)
+ public async Task<ActionResult<CommonPutResponse>> Put([FromBody] UserPutRequest request, [FromRoute][Username] string username)
{
- try
- {
- var result = await _userService.PutUser(username, request.Password, request.Administrator!.Value);
- switch (result)
- {
- case PutResult.Create:
- _logger.LogInformation(Log.Format(LogPutCreate, ("Username", username)));
- return CreatedAtAction("Get", new { username }, CommonPutResponse.Create(_localizerFactory));
- case PutResult.Modify:
- _logger.LogInformation(Log.Format(LogPutModify, ("Username", username)));
- return Ok(CommonPutResponse.Modify(_localizerFactory));
- default:
- throw new InvalidBranchException();
- }
- }
- catch (UsernameBadFormatException e)
+ var result = await _userService.PutUser(username, request.Password, request.Administrator!.Value);
+ switch (result)
{
- _logger.LogInformation(e, Log.Format(LogPutBadUsername, ("Username", username)));
- return BadRequest(new CommonResponse(ErrorCodes.Http.User.Put.BadUsername, _localizer["ErrorPutBadUsername"]));
+ case PutResult.Create:
+ _logger.LogInformation(Log.Format(LogPutCreate, ("Username", username)));
+ return CreatedAtAction("Get", new { username }, CommonPutResponse.Create(_localizerFactory));
+ case PutResult.Modify:
+ _logger.LogInformation(Log.Format(LogPutModify, ("Username", username)));
+ return Ok(CommonPutResponse.Modify(_localizerFactory));
+ default:
+ throw new InvalidBranchException();
}
}
[HttpPatch("users/{username}"), AdminAuthorize]
- public async Task<ActionResult> Patch([FromBody] UserPatchRequest request, [FromRoute] string username)
+ public async Task<ActionResult> Patch([FromBody] UserPatchRequest request, [FromRoute][Username] string username)
{
try
{
@@ -130,7 +118,7 @@ namespace Timeline.Controllers
}
[HttpDelete("users/{username}"), AdminAuthorize]
- public async Task<ActionResult<CommonDeleteResponse>> Delete([FromRoute] string username)
+ public async Task<ActionResult<CommonDeleteResponse>> Delete([FromRoute][Username] string username)
{
try
{