aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Filters
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2020-02-01 00:26:35 +0800
committerGitHub <noreply@github.com>2020-02-01 00:26:35 +0800
commitd703269e06d4c9e254fe2d5589ff04cdd6a9b366 (patch)
treef02f8d57440c777d4732bc4439f82e8b25c6732c /Timeline/Filters
parent631731e5c2253116a53fdc435afca184251a34fc (diff)
parentbddf1d6eaac782672071df6527c40c81c3123f3a (diff)
downloadtimeline-d703269e06d4c9e254fe2d5589ff04cdd6a9b366.tar.gz
timeline-d703269e06d4c9e254fe2d5589ff04cdd6a9b366.tar.bz2
timeline-d703269e06d4c9e254fe2d5589ff04cdd6a9b366.zip
Merge pull request #56 from crupest/dev
Refactor API to be RESTful.
Diffstat (limited to 'Timeline/Filters')
-rw-r--r--Timeline/Filters/Header.cs57
-rw-r--r--Timeline/Filters/Timeline.cs26
-rw-r--r--Timeline/Filters/User.cs88
3 files changed, 5 insertions, 166 deletions
diff --git a/Timeline/Filters/Header.cs b/Timeline/Filters/Header.cs
index f5fb16aa..0db11faf 100644
--- a/Timeline/Filters/Header.cs
+++ b/Timeline/Filters/Header.cs
@@ -1,72 +1,22 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Timeline.Models.Http;
-using static Timeline.Resources.Filters;
-
-namespace Timeline
-{
- public static partial class ErrorCodes
- {
- public static partial class Http
- {
- public static partial class Filter // bxx = 1xx
- {
- public static partial class Header // bbb = 100
- {
- public static class ContentType // cc = 01
- {
- public const int Missing = 11000101; // dd = 01
- }
-
- public static class ContentLength // cc = 02
- {
- public const int Missing = 11000201; // dd = 01
- public const int Zero = 11000202; // dd = 02
- }
- }
- }
-
- }
- }
-}
namespace Timeline.Filters
{
public class RequireContentTypeAttribute : ActionFilterAttribute
{
- internal static CommonResponse CreateResponse()
- {
- return new CommonResponse(
- ErrorCodes.Http.Filter.Header.ContentType.Missing,
- MessageHeaderContentTypeMissing);
- }
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods")]
public override void OnActionExecuting(ActionExecutingContext context)
{
if (context.HttpContext.Request.ContentType == null)
{
- context.Result = new BadRequestObjectResult(CreateResponse());
+ context.Result = new BadRequestObjectResult(ErrorResponse.Common.Header.ContentType_Missing());
}
}
}
public class RequireContentLengthAttribute : ActionFilterAttribute
{
- internal static CommonResponse CreateMissingResponse()
- {
- return new CommonResponse(
- ErrorCodes.Http.Filter.Header.ContentLength.Missing,
- MessageHeaderContentLengthMissing);
- }
-
- internal static CommonResponse CreateZeroResponse()
- {
- return new CommonResponse(
- ErrorCodes.Http.Filter.Header.ContentLength.Zero,
- MessageHeaderContentLengthZero);
- }
-
public RequireContentLengthAttribute()
: this(true)
{
@@ -80,18 +30,17 @@ namespace Timeline.Filters
public bool RequireNonZero { get; set; }
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods")]
public override void OnActionExecuting(ActionExecutingContext context)
{
if (context.HttpContext.Request.ContentLength == null)
{
- context.Result = new BadRequestObjectResult(CreateMissingResponse());
+ context.Result = new BadRequestObjectResult(ErrorResponse.Common.Header.ContentLength_Missing());
return;
}
if (RequireNonZero && context.HttpContext.Request.ContentLength.Value == 0)
{
- context.Result = new BadRequestObjectResult(CreateZeroResponse());
+ context.Result = new BadRequestObjectResult(ErrorResponse.Common.Header.ContentLength_Zero());
return;
}
}
diff --git a/Timeline/Filters/Timeline.cs b/Timeline/Filters/Timeline.cs
index 7859d409..ed78e645 100644
--- a/Timeline/Filters/Timeline.cs
+++ b/Timeline/Filters/Timeline.cs
@@ -2,44 +2,22 @@
using Microsoft.AspNetCore.Mvc.Filters;
using Timeline.Models.Http;
using Timeline.Services;
-using static Timeline.Resources.Filters;
-
-namespace Timeline
-{
- public static partial class ErrorCodes
- {
- public static partial class Http
- {
- public static partial class Filter // bxx = 1xx
- {
- public static class Timeline // bbb = 102
- {
- public const int UserNotExist = 11020101;
- public const int NameNotExist = 11020102;
- }
- }
- }
- }
-}
namespace Timeline.Filters
{
public class CatchTimelineNotExistExceptionAttribute : ExceptionFilterAttribute
{
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods")]
public override void OnException(ExceptionContext context)
{
if (context.Exception is TimelineNotExistException e)
{
if (e.InnerException is UserNotExistException)
{
- context.Result = new BadRequestObjectResult(
- new CommonResponse(ErrorCodes.Http.Filter.Timeline.UserNotExist, MessageTimelineNotExistUser));
+ context.Result = new NotFoundObjectResult(ErrorResponse.UserCommon.NotExist());
}
else
{
- context.Result = new BadRequestObjectResult(
- new CommonResponse(ErrorCodes.Http.Filter.Timeline.NameNotExist, MessageTimelineNotExist));
+ throw new System.NotImplementedException();
}
}
}
diff --git a/Timeline/Filters/User.cs b/Timeline/Filters/User.cs
deleted file mode 100644
index 16c76750..00000000
--- a/Timeline/Filters/User.cs
+++ /dev/null
@@ -1,88 +0,0 @@
-using Microsoft.AspNetCore.Http;
-using Microsoft.AspNetCore.Mvc;
-using Microsoft.AspNetCore.Mvc.Filters;
-using Microsoft.Extensions.DependencyInjection;
-using Microsoft.Extensions.Logging;
-using System;
-using Timeline.Auth;
-using Timeline.Models.Http;
-using Timeline.Services;
-using static Timeline.Resources.Filters;
-
-namespace Timeline
-{
- public static partial class ErrorCodes
- {
- public static partial class Http
- {
- public static partial class Filter // bxx = 1xx
- {
- public static class User // bbb = 101
- {
- public const int NotExist = 11010101;
-
- public const int NotSelfOrAdminForbid = 11010201;
- }
- }
- }
- }
-}
-
-namespace Timeline.Filters
-{
- public class SelfOrAdminAttribute : ActionFilterAttribute
- {
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods")]
- public override void OnActionExecuting(ActionExecutingContext context)
- {
- var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<SelfOrAdminAttribute>>();
-
- var user = context.HttpContext.User;
-
- if (user == null)
- {
- logger.LogError(LogSelfOrAdminNoUser);
- return;
- }
-
- if (context.ModelState.TryGetValue("username", out var model))
- {
- if (model.RawValue is string username)
- {
- if (!user.IsAdministrator() && user.Identity.Name != username)
- {
- context.Result = new ObjectResult(
- new CommonResponse(ErrorCodes.Http.Filter.User.NotSelfOrAdminForbid, MessageSelfOrAdminForbid))
- { StatusCode = StatusCodes.Status403Forbidden };
- }
- }
- else
- {
- logger.LogError(LogSelfOrAdminUsernameNotString);
- }
- }
- else
- {
- logger.LogError(LogSelfOrAdminNoUsername);
- }
- }
- }
-
- [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
- public class CatchUserNotExistExceptionAttribute : ExceptionFilterAttribute
- {
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "ASP.Net already checked.")]
- public override void OnException(ExceptionContext context)
- {
- if (context.Exception is UserNotExistException)
- {
- var body = new CommonResponse(ErrorCodes.Http.Filter.User.NotExist, MessageUserNotExist);
-
- if (context.HttpContext.Request.Method == "GET")
- context.Result = new NotFoundObjectResult(body);
- else
- context.Result = new BadRequestObjectResult(body);
- }
- }
- }
-}