From 5bd826d47c30f6e6ac6eded4fefb99f26786c3bc Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 30 Apr 2021 17:53:40 +0800 Subject: refactor: ... --- .../Timeline.Tests/IntegratedTests/TokenTest.cs | 8 +- .../IntegratedTests/UserAvatarTest.cs | 6 +- .../IntegratedTests/UserPermissionTest.cs | 4 +- BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs | 4 +- .../ActionResultControllerExtensions.cs | 15 +- BackEnd/Timeline/Controllers/Resource.Designer.cs | 144 +++++++ BackEnd/Timeline/Controllers/Resource.resx | 48 +++ BackEnd/Timeline/Controllers/TimelineController.cs | 12 +- .../Timeline/Controllers/TimelinePostController.cs | 12 +- BackEnd/Timeline/Controllers/TokenController.cs | 12 +- .../Timeline/Controllers/UserAvatarController.cs | 12 +- BackEnd/Timeline/Controllers/UserController.cs | 17 +- BackEnd/Timeline/ErrorCodes.cs | 24 +- BackEnd/Timeline/Filters/Header.cs | 63 ---- .../Timeline/Filters/MaxContentLengthAttribute.cs | 26 ++ BackEnd/Timeline/Filters/MaxContentLengthFilter.cs | 43 +++ BackEnd/Timeline/Filters/Resource.Designer.cs | 9 + BackEnd/Timeline/Filters/Resource.resx | 3 + BackEnd/Timeline/Helpers/Cache/DataCacheHelper.cs | 2 +- .../Helpers/InvalidModelResponseFactory.cs | 2 +- BackEnd/Timeline/Models/Http/ErrorResponse.cs | 216 ----------- BackEnd/Timeline/Resources/Messages.Designer.cs | 414 --------------------- BackEnd/Timeline/Resources/Messages.resx | 237 ------------ BackEnd/Timeline/Routes/Resource.Designer.cs | 72 ++++ BackEnd/Timeline/Routes/Resource.resx | 123 ++++++ .../Timeline/Routes/UnknownEndpointMiddleware.cs | 4 +- BackEnd/Timeline/Timeline.csproj | 18 +- 27 files changed, 549 insertions(+), 1001 deletions(-) delete mode 100644 BackEnd/Timeline/Filters/Header.cs create mode 100644 BackEnd/Timeline/Filters/MaxContentLengthAttribute.cs create mode 100644 BackEnd/Timeline/Filters/MaxContentLengthFilter.cs delete mode 100644 BackEnd/Timeline/Models/Http/ErrorResponse.cs delete mode 100644 BackEnd/Timeline/Resources/Messages.Designer.cs delete mode 100644 BackEnd/Timeline/Resources/Messages.resx create mode 100644 BackEnd/Timeline/Routes/Resource.Designer.cs create mode 100644 BackEnd/Timeline/Routes/Resource.resx (limited to 'BackEnd') diff --git a/BackEnd/Timeline.Tests/IntegratedTests/TokenTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/TokenTest.cs index 08857dc1..ac96ef9d 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/TokenTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/TokenTest.cs @@ -53,7 +53,7 @@ namespace Timeline.Tests.IntegratedTests using var client = await CreateDefaultClient(); await client.TestPostAssertErrorAsync(CreateTokenUrl, new HttpCreateTokenRequest { Username = username, Password = password }, - errorCode: ErrorCodes.TokenController.Create_BadCredential); + errorCode: ErrorCodes.TokenController.CreateBadCredential); } [Fact] @@ -79,7 +79,7 @@ namespace Timeline.Tests.IntegratedTests using var client = await CreateDefaultClient(); await client.TestPostAssertErrorAsync(VerifyTokenUrl, new HttpVerifyTokenRequest { Token = "bad token hahaha" }, - errorCode: ErrorCodes.TokenController.Verify_BadFormat); + errorCode: ErrorCodes.TokenController.VerifyBadFormat); } [Fact] @@ -98,7 +98,7 @@ namespace Timeline.Tests.IntegratedTests await client.TestPostAssertErrorAsync(VerifyTokenUrl, new HttpVerifyTokenRequest { Token = token }, - errorCode: ErrorCodes.TokenController.Verify_OldVersion); + errorCode: ErrorCodes.TokenController.VerifyOldVersion); } [Fact] @@ -115,7 +115,7 @@ namespace Timeline.Tests.IntegratedTests await client.TestPostAssertErrorAsync(VerifyTokenUrl, new HttpVerifyTokenRequest { Token = token }, - errorCode: ErrorCodes.TokenController.Verify_UserNotExist); + errorCode: ErrorCodes.TokenController.VerifyUserNotExist); } //[Fact] diff --git a/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs index c2ccff64..52f04e52 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs @@ -99,9 +99,9 @@ namespace Timeline.Tests.IntegratedTests } { - await client.TestPutByteArrayAssertErrorAsync("users/user1/avatar", new[] { (byte)0x00 }, "image/png", errorCode: ErrorCodes.UserAvatar.BadFormat_CantDecode); - await client.TestPutByteArrayAssertErrorAsync("users/user1/avatar", mockAvatar.Data, "image/jpeg", errorCode: ErrorCodes.UserAvatar.BadFormat_UnmatchedFormat); - await client.TestPutByteArrayAssertErrorAsync("users/user1/avatar", ImageHelper.CreatePngWithSize(100, 200), "image/png", errorCode: ErrorCodes.UserAvatar.BadFormat_BadSize); + await client.TestPutByteArrayAssertErrorAsync("users/user1/avatar", new[] { (byte)0x00 }, "image/png", errorCode: ErrorCodes.Image.CantDecode); + await client.TestPutByteArrayAssertErrorAsync("users/user1/avatar", mockAvatar.Data, "image/jpeg", errorCode: ErrorCodes.Image.UnmatchedFormat); + await client.TestPutByteArrayAssertErrorAsync("users/user1/avatar", ImageHelper.CreatePngWithSize(100, 200), "image/png", errorCode: ErrorCodes.Image.BadSize); } { diff --git a/BackEnd/Timeline.Tests/IntegratedTests/UserPermissionTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/UserPermissionTest.cs index 83dc51f9..26714c24 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/UserPermissionTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/UserPermissionTest.cs @@ -41,10 +41,10 @@ namespace Timeline.Tests.IntegratedTests using var client = await CreateClientAsAdministrator(); await client.TestPutAssertErrorAsync($"users/admin/permissions/{permission}", - errorCode: ErrorCodes.UserController.ChangePermission_RootUser); + errorCode: ErrorCodes.UserController.InvalidOperationOnRootUser); await client.TestDeleteAssertErrorAsync($"users/admin/permissions/{permission}", - errorCode: ErrorCodes.UserController.ChangePermission_RootUser); + errorCode: ErrorCodes.UserController.InvalidOperationOnRootUser); } [Theory] diff --git a/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs index 7286e12e..d34bcaf9 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs @@ -183,7 +183,7 @@ namespace Timeline.Tests.IntegratedTests public async Task DeleteRootUser_Should_Error() { using var client = await CreateClientAsAdministrator(); - await client.TestDeleteAssertErrorAsync("users/admin", errorCode: ErrorCodes.UserController.Delete_RootUser); + await client.TestDeleteAssertErrorAsync("users/admin", errorCode: ErrorCodes.UserController.InvalidOperationOnRootUser); } [Fact] @@ -310,7 +310,7 @@ namespace Timeline.Tests.IntegratedTests public async Task Op_ChangePassword_BadOldPassword() { using var client = await CreateClientAsUser(); - await client.TestPostAssertErrorAsync(changePasswordUrl, new HttpChangePasswordRequest { OldPassword = "???", NewPassword = "???" }, errorCode: ErrorCodes.UserController.ChangePassword_BadOldPassword); + await client.TestPostAssertErrorAsync(changePasswordUrl, new HttpChangePasswordRequest { OldPassword = "???", NewPassword = "???" }, errorCode: ErrorCodes.UserController.ChangePasswordBadOldPassword); } [Fact] diff --git a/BackEnd/Timeline/Controllers/ActionResultControllerExtensions.cs b/BackEnd/Timeline/Controllers/ActionResultControllerExtensions.cs index 76a8b7ae..a3da73fa 100644 --- a/BackEnd/Timeline/Controllers/ActionResultControllerExtensions.cs +++ b/BackEnd/Timeline/Controllers/ActionResultControllerExtensions.cs @@ -1,11 +1,22 @@ -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; using Timeline.Models.Http; namespace Timeline.Controllers { public static class ActionResultControllerExtensions { - public static BadRequestObjectResult BadRequestWithCodeAndMessage(this ControllerBase controller, int code, string message) + public static ObjectResult StatusCodeWithCommonResponse(this ControllerBase controller, int statusCode, int code, string message) + { + return controller.StatusCode(statusCode, new CommonResponse(code, message)); + } + + public static ObjectResult ForbidWithMessage(this ControllerBase controller, string? message = null) + { + return controller.StatusCode(StatusCodes.Status403Forbidden, new CommonResponse(ErrorCodes.Common.Forbid, message ?? Resource.MessageForbid)); + } + + public static BadRequestObjectResult BadRequestWithCommonResponse(this ControllerBase controller, int code, string message) { return controller.BadRequest(new CommonResponse(code, message)); } diff --git a/BackEnd/Timeline/Controllers/Resource.Designer.cs b/BackEnd/Timeline/Controllers/Resource.Designer.cs index c2dfd3cd..f3d7264a 100644 --- a/BackEnd/Timeline/Controllers/Resource.Designer.cs +++ b/BackEnd/Timeline/Controllers/Resource.Designer.cs @@ -69,6 +69,150 @@ namespace Timeline.Controllers { } } + /// + /// Looks up a localized string similar to You have no permission to access this.. + /// + internal static string MessageForbid { + get { + return ResourceManager.GetString("MessageForbid", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to You can't do this unless you are administrator.. + /// + internal static string MessageForbidNotAdministrator { + get { + return ResourceManager.GetString("MessageForbidNotAdministrator", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to You can't do this unless you are administrator or resource owner.. + /// + internal static string MessageForbidNotAdministratorOrOwner { + get { + return ResourceManager.GetString("MessageForbidNotAdministratorOrOwner", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Image is not a square.. + /// + internal static string MessageImageBadSize { + get { + return ResourceManager.GetString("MessageImageBadSize", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Image decode failed.. + /// + internal static string MessageImageDecodeFailed { + get { + return ResourceManager.GetString("MessageImageDecodeFailed", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Specified image format does not match the actual one .. + /// + internal static string MessageImageFormatUnmatch { + get { + return ResourceManager.GetString("MessageImageFormatUnmatch", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Unknown error happened to image.. + /// + internal static string MessageImageUnknownError { + get { + return ResourceManager.GetString("MessageImageUnknownError", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to You can't do this because it is the root user.. + /// + internal static string MessageInvalidOperationOnRootUser { + get { + return ResourceManager.GetString("MessageInvalidOperationOnRootUser", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The old password is wrong.. + /// + internal static string MessageOldPasswordWrong { + get { + return ResourceManager.GetString("MessageOldPasswordWrong", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The user specified by query param "relate" does not exist.. + /// + internal static string MessageTimelineListQueryRelateNotExist { + get { + return ResourceManager.GetString("MessageTimelineListQueryRelateNotExist", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to '{0}' is an unkown visibility in the query parameter 'visibility'. . + /// + internal static string MessageTimelineListQueryVisibilityUnknown { + get { + return ResourceManager.GetString("MessageTimelineListQueryVisibilityUnknown", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Username or password is invalid.. + /// + internal static string MessageTokenCreateBadCredential { + get { + return ResourceManager.GetString("MessageTokenCreateBadCredential", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The token is of bad format. It might not be created by the server.. + /// + internal static string MessageTokenVerifyBadFormat { + get { + return ResourceManager.GetString("MessageTokenVerifyBadFormat", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Token has an old version. User might have update some info.. + /// + internal static string MessageTokenVerifyOldVersion { + get { + return ResourceManager.GetString("MessageTokenVerifyOldVersion", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The token is expired.. + /// + internal static string MessageTokenVerifyTimeExpired { + get { + return ResourceManager.GetString("MessageTokenVerifyTimeExpired", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to User does not exist. Administrator might have deleted this user.. + /// + internal static string MessageTokenVerifyUserNotExist { + get { + return ResourceManager.GetString("MessageTokenVerifyUserNotExist", resourceCulture); + } + } + /// /// Looks up a localized string similar to A user with given username already exists.. /// diff --git a/BackEnd/Timeline/Controllers/Resource.resx b/BackEnd/Timeline/Controllers/Resource.resx index 8939dfd8..90c6bdd6 100644 --- a/BackEnd/Timeline/Controllers/Resource.resx +++ b/BackEnd/Timeline/Controllers/Resource.resx @@ -120,6 +120,54 @@ Can't get user id. + + You have no permission to access this. + + + You can't do this unless you are administrator. + + + You can't do this unless you are administrator or resource owner. + + + Image is not a square. + + + Image decode failed. + + + Specified image format does not match the actual one . + + + Unknown error happened to image. + + + You can't do this because it is the root user. + + + The old password is wrong. + + + The user specified by query param "relate" does not exist. + + + '{0}' is an unkown visibility in the query parameter 'visibility'. + + + Username or password is invalid. + + + The token is of bad format. It might not be created by the server. + + + Token has an old version. User might have update some info. + + + The token is expired. + + + User does not exist. Administrator might have deleted this user. + A user with given username already exists. diff --git a/BackEnd/Timeline/Controllers/TimelineController.cs b/BackEnd/Timeline/Controllers/TimelineController.cs index bb770ea0..f04982dc 100644 --- a/BackEnd/Timeline/Controllers/TimelineController.cs +++ b/BackEnd/Timeline/Controllers/TimelineController.cs @@ -82,7 +82,7 @@ namespace Timeline.Controllers } else { - return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_QueryVisibilityUnknown, item)); + return this.BadRequestWithCommonResponse(ErrorCodes.Common.InvalidModel, string.Format(Resource.MessageTimelineListQueryVisibilityUnknown, visibility)); } } } @@ -100,7 +100,7 @@ namespace Timeline.Controllers } catch (EntityNotExistException) { - return BadRequest(ErrorResponse.TimelineController.QueryRelateNotExist()); + return this.BadRequestWithCommonResponse(ErrorCodes.TimelineController.QueryRelateNotExist, Resource.MessageTimelineListQueryRelateNotExist); } } @@ -143,7 +143,7 @@ namespace Timeline.Controllers if (!UserHasAllTimelineManagementPermission && !await _service.HasManagePermissionAsync(timelineId, this.GetUserId())) { - return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); + return this.ForbidWithMessage(); } await _service.ChangePropertyAsync(timelineId, _mapper.AutoMapperMap(body)); @@ -169,7 +169,7 @@ namespace Timeline.Controllers if (!UserHasAllTimelineManagementPermission && !(await _service.HasManagePermissionAsync(timelineId, this.GetUserId()))) { - return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); + return this.ForbidWithMessage(); } var userId = await _userService.GetUserIdByUsernameAsync(member); @@ -194,7 +194,7 @@ namespace Timeline.Controllers if (!UserHasAllTimelineManagementPermission && !(await _service.HasManagePermissionAsync(timelineId, this.GetUserId()))) { - return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); + return this.ForbidWithMessage(); } @@ -239,7 +239,7 @@ namespace Timeline.Controllers if (!UserHasAllTimelineManagementPermission && !(await _service.HasManagePermissionAsync(timelineId, this.GetUserId()))) { - return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); + return this.ForbidWithMessage(); } await _service.DeleteTimelineAsync(timelineId); diff --git a/BackEnd/Timeline/Controllers/TimelinePostController.cs b/BackEnd/Timeline/Controllers/TimelinePostController.cs index 70f672d1..21102400 100644 --- a/BackEnd/Timeline/Controllers/TimelinePostController.cs +++ b/BackEnd/Timeline/Controllers/TimelinePostController.cs @@ -69,7 +69,7 @@ namespace Timeline.Controllers if (!UserHasAllTimelineManagementPermission && !await _timelineService.HasReadPermissionAsync(timelineId, this.GetOptionalUserId())) { - return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); + return this.ForbidWithMessage(); } var posts = await _postService.GetPostsAsync(timelineId, modifiedSince, includeDeleted ?? false); @@ -94,7 +94,7 @@ namespace Timeline.Controllers if (!UserHasAllTimelineManagementPermission && !await _timelineService.HasReadPermissionAsync(timelineId, this.GetOptionalUserId())) { - return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); + return this.ForbidWithMessage(); } var post = await _postService.GetPostAsync(timelineId, postId); @@ -140,7 +140,7 @@ namespace Timeline.Controllers if (!UserHasAllTimelineManagementPermission && !await _timelineService.HasReadPermissionAsync(timelineId, this.GetOptionalUserId())) { - return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); + return this.ForbidWithMessage(); } return await DataCacheHelper.GenerateActionResult(this, @@ -176,7 +176,7 @@ namespace Timeline.Controllers if (!UserHasAllTimelineManagementPermission && !await _timelineService.IsMemberOfAsync(timelineId, userId)) { - return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); + return this.ForbidWithMessage(); } var createRequest = new TimelinePostCreateRequest() @@ -235,7 +235,7 @@ namespace Timeline.Controllers if (!UserHasAllTimelineManagementPermission && !await _postService.HasPostModifyPermissionAsync(timelineId, post, this.GetUserId(), true)) { - return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); + return this.ForbidWithMessage(); } var entity = await _postService.PatchPostAsync(timelineId, post, new TimelinePostPatchRequest { Time = body.Time, Color = body.Color }); @@ -262,7 +262,7 @@ namespace Timeline.Controllers if (!UserHasAllTimelineManagementPermission && !await _postService.HasPostModifyPermissionAsync(timelineId, post, this.GetUserId(), true)) { - return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); + return this.ForbidWithMessage(); } await _postService.DeletePostAsync(timelineId, post); diff --git a/BackEnd/Timeline/Controllers/TokenController.cs b/BackEnd/Timeline/Controllers/TokenController.cs index 915f710d..080a4dc4 100644 --- a/BackEnd/Timeline/Controllers/TokenController.cs +++ b/BackEnd/Timeline/Controllers/TokenController.cs @@ -57,11 +57,11 @@ namespace Timeline.Controllers } catch (EntityNotExistException) { - return BadRequest(ErrorResponse.TokenController.Create_BadCredential()); + return this.BadRequestWithCommonResponse(ErrorCodes.TokenController.CreateBadCredential, Resource.MessageTokenCreateBadCredential); } catch (BadPasswordException) { - return BadRequest(ErrorResponse.TokenController.Create_BadCredential()); + return this.BadRequestWithCommonResponse(ErrorCodes.TokenController.CreateBadCredential, Resource.MessageTokenCreateBadCredential); } } @@ -85,19 +85,19 @@ namespace Timeline.Controllers } catch (UserTokenTimeExpiredException) { - return BadRequest(ErrorResponse.TokenController.Verify_TimeExpired()); + return this.BadRequestWithCommonResponse(ErrorCodes.TokenController.VerifyTimeExpired, Resource.MessageTokenVerifyTimeExpired); } catch (UserTokenVersionExpiredException) { - return BadRequest(ErrorResponse.TokenController.Verify_OldVersion()); + return this.BadRequestWithCommonResponse(ErrorCodes.TokenController.VerifyOldVersion, Resource.MessageTokenVerifyOldVersion); } catch (UserTokenBadFormatException) { - return BadRequest(ErrorResponse.TokenController.Verify_BadFormat()); + return this.BadRequestWithCommonResponse(ErrorCodes.TokenController.VerifyBadFormat, Resource.MessageTokenVerifyBadFormat); } catch (UserTokenUserNotExistException) { - return BadRequest(ErrorResponse.TokenController.Verify_UserNotExist()); + return this.BadRequestWithCommonResponse(ErrorCodes.TokenController.VerifyUserNotExist, Resource.MessageTokenVerifyUserNotExist); } } } diff --git a/BackEnd/Timeline/Controllers/UserAvatarController.cs b/BackEnd/Timeline/Controllers/UserAvatarController.cs index 5d9becaa..05c73aa2 100644 --- a/BackEnd/Timeline/Controllers/UserAvatarController.cs +++ b/BackEnd/Timeline/Controllers/UserAvatarController.cs @@ -65,7 +65,7 @@ namespace Timeline.Controllers { if (!this.UserHasPermission(UserPermission.UserManagement) && User.Identity!.Name != username) { - return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); + return this.ForbidWithMessage(Resource.MessageForbidNotAdministratorOrOwner); } long id = await _userService.GetUserIdByUsernameAsync(username); @@ -82,10 +82,10 @@ namespace Timeline.Controllers { return BadRequest(e.Error switch { - ImageException.ErrorReason.CantDecode => ErrorResponse.UserAvatar.BadFormat_CantDecode(), - ImageException.ErrorReason.UnmatchedFormat => ErrorResponse.UserAvatar.BadFormat_UnmatchedFormat(), - ImageException.ErrorReason.BadSize => ErrorResponse.UserAvatar.BadFormat_BadSize(), - _ => throw new Exception() + ImageException.ErrorReason.CantDecode => new CommonResponse(ErrorCodes.Image.CantDecode, Resource.MessageImageDecodeFailed), + ImageException.ErrorReason.UnmatchedFormat => new CommonResponse(ErrorCodes.Image.UnmatchedFormat, Resource.MessageImageFormatUnmatch), + ImageException.ErrorReason.BadSize => new CommonResponse(ErrorCodes.Image.BadSize, Resource.MessageImageBadSize), + _ => new CommonResponse(ErrorCodes.Image.Unknown, Resource.MessageImageUnknownError) }); } } @@ -108,7 +108,7 @@ namespace Timeline.Controllers { if (!this.UserHasPermission(UserPermission.UserManagement) && User.Identity!.Name != username) { - return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); + return this.ForbidWithMessage(Resource.MessageForbidNotAdministratorOrOwner); } long id = await _userService.GetUserIdByUsernameAsync(username); diff --git a/BackEnd/Timeline/Controllers/UserController.cs b/BackEnd/Timeline/Controllers/UserController.cs index bdf9c0b7..ec732caa 100644 --- a/BackEnd/Timeline/Controllers/UserController.cs +++ b/BackEnd/Timeline/Controllers/UserController.cs @@ -102,16 +102,13 @@ namespace Timeline.Controllers else { if (User.Identity!.Name != username) - return StatusCode(StatusCodes.Status403Forbidden, - ErrorResponse.Common.CustomMessage_Forbid(Resources.Messages.Common_Forbid_NotSelf)); + return this.ForbidWithMessage(Resource.MessageForbidNotAdministratorOrOwner); if (body.Username != null) - return StatusCode(StatusCodes.Status403Forbidden, - ErrorResponse.Common.CustomMessage_Forbid(Resources.Messages.UserController_Patch_Forbid_Username)); + return this.ForbidWithMessage(Resource.MessageForbidNotAdministrator); if (body.Password != null) - return StatusCode(StatusCodes.Status403Forbidden, - ErrorResponse.Common.CustomMessage_Forbid(Resources.Messages.UserController_Patch_Forbid_Password)); + return this.ForbidWithMessage(Resource.MessageForbidNotAdministrator); var user = await _userService.ModifyUserAsync(this.GetUserId(), _mapper.AutoMapperMap(body)); return await _mapper.MapAsync(user, Url, User); @@ -140,7 +137,7 @@ namespace Timeline.Controllers } catch (InvalidOperationOnRootUserException) { - return BadRequest(ErrorResponse.UserController.Delete_RootUser()); + return this.BadRequestWithCommonResponse(ErrorCodes.UserController.InvalidOperationOnRootUser, Resource.MessageInvalidOperationOnRootUser); } } @@ -160,7 +157,7 @@ namespace Timeline.Controllers } catch (BadPasswordException) { - return BadRequest(ErrorResponse.UserController.ChangePassword_BadOldPassword()); + return this.BadRequestWithCommonResponse(ErrorCodes.UserController.ChangePasswordBadOldPassword, Resource.MessageOldPasswordWrong); } // User can't be non-existent or the token is bad. } @@ -181,7 +178,7 @@ namespace Timeline.Controllers } catch (InvalidOperationOnRootUserException) { - return BadRequest(ErrorResponse.UserController.ChangePermission_RootUser()); + return this.BadRequestWithCommonResponse(ErrorCodes.UserController.InvalidOperationOnRootUser, Resource.MessageInvalidOperationOnRootUser); } } @@ -201,7 +198,7 @@ namespace Timeline.Controllers } catch (InvalidOperationOnRootUserException) { - return BadRequest(ErrorResponse.UserController.ChangePermission_RootUser()); + return this.BadRequestWithCommonResponse(ErrorCodes.UserController.InvalidOperationOnRootUser, Resource.MessageInvalidOperationOnRootUser); } } } diff --git a/BackEnd/Timeline/ErrorCodes.cs b/BackEnd/Timeline/ErrorCodes.cs index 06952822..79054a6f 100644 --- a/BackEnd/Timeline/ErrorCodes.cs +++ b/BackEnd/Timeline/ErrorCodes.cs @@ -56,25 +56,25 @@ public static class TokenController { - public const int Create_BadCredential = 1_101_01_01; - public const int Verify_BadFormat = 1_101_02_01; - public const int Verify_UserNotExist = 1_101_02_02; - public const int Verify_OldVersion = 1_101_02_03; - public const int Verify_TimeExpired = 1_101_02_04; + public const int CreateBadCredential = 1_101_01_01; + public const int VerifyBadFormat = 1_101_02_01; + public const int VerifyUserNotExist = 1_101_02_02; + public const int VerifyOldVersion = 1_101_02_03; + public const int VerifyTimeExpired = 1_101_02_04; } public static class UserController { - public const int ChangePassword_BadOldPassword = 1_102_02_01; - public const int ChangePermission_RootUser = 1_102_03_01; - public const int Delete_RootUser = 1_102_04_01; + public const int ChangePasswordBadOldPassword = 1_102_01_01; + public const int InvalidOperationOnRootUser = 1_102_02_01; } - public static class UserAvatar + public static class Image { - public const int BadFormat_CantDecode = 1_103_00_01; - public const int BadFormat_UnmatchedFormat = 1_103_00_02; - public const int BadFormat_BadSize = 1_103_00_03; + public const int CantDecode = 1_103_00_01; + public const int UnmatchedFormat = 1_103_00_02; + public const int BadSize = 1_103_00_03; + public const int Unknown = 1_103_00_04; } public static class TimelineController diff --git a/BackEnd/Timeline/Filters/Header.cs b/BackEnd/Timeline/Filters/Header.cs deleted file mode 100644 index cc5ddd9f..00000000 --- a/BackEnd/Timeline/Filters/Header.cs +++ /dev/null @@ -1,63 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Filters; -using Timeline.Models.Http; - -namespace Timeline.Filters -{ - /// - /// Restrict max content length. - /// - public class MaxContentLengthFilter : IResourceFilter - { - /// - /// - /// - /// Max length. - public MaxContentLengthFilter(long maxByteLength) - { - MaxByteLength = maxByteLength; - } - - /// - /// Max length. - /// - public long MaxByteLength { get; set; } - - /// - public void OnResourceExecuted(ResourceExecutedContext context) - { - } - - /// - public void OnResourceExecuting(ResourceExecutingContext context) - { - var contentLength = context.HttpContext.Request.ContentLength; - if (contentLength != null && contentLength > MaxByteLength) - { - context.Result = new BadRequestObjectResult(ErrorResponse.Common.Content.TooBig(MaxByteLength + "B")); - } - } - } - - /// - /// Restrict max content length. - /// - public class MaxContentLengthAttribute : TypeFilterAttribute - { - /// - /// - /// - /// Max length. - public MaxContentLengthAttribute(long maxByteLength) - : base(typeof(MaxContentLengthFilter)) - { - MaxByteLength = maxByteLength; - Arguments = new object[] { maxByteLength }; - } - - /// - /// Max length. - /// - public long MaxByteLength { get; } - } -} diff --git a/BackEnd/Timeline/Filters/MaxContentLengthAttribute.cs b/BackEnd/Timeline/Filters/MaxContentLengthAttribute.cs new file mode 100644 index 00000000..507a6404 --- /dev/null +++ b/BackEnd/Timeline/Filters/MaxContentLengthAttribute.cs @@ -0,0 +1,26 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Timeline.Filters +{ + /// + /// Restrict max content length. + /// + public class MaxContentLengthAttribute : TypeFilterAttribute + { + /// + /// + /// + /// Max length. + public MaxContentLengthAttribute(long maxByteLength) + : base(typeof(MaxContentLengthFilter)) + { + MaxByteLength = maxByteLength; + Arguments = new object[] { maxByteLength }; + } + + /// + /// Max length. + /// + public long MaxByteLength { get; } + } +} diff --git a/BackEnd/Timeline/Filters/MaxContentLengthFilter.cs b/BackEnd/Timeline/Filters/MaxContentLengthFilter.cs new file mode 100644 index 00000000..0127971a --- /dev/null +++ b/BackEnd/Timeline/Filters/MaxContentLengthFilter.cs @@ -0,0 +1,43 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +using Timeline.Models.Http; + +namespace Timeline.Filters +{ + /// + /// Restrict max content length. + /// + public class MaxContentLengthFilter : IResourceFilter + { + /// + /// + /// + /// Max length. + public MaxContentLengthFilter(long maxByteLength) + { + MaxByteLength = maxByteLength; + } + + /// + /// Max length. + /// + public long MaxByteLength { get; set; } + + /// + public void OnResourceExecuted(ResourceExecutedContext context) + { + } + + /// + public void OnResourceExecuting(ResourceExecutingContext context) + { + var contentLength = context.HttpContext.Request.ContentLength; + if (contentLength != null && contentLength > MaxByteLength) + { + context.Result = new BadRequestObjectResult( + new CommonResponse(ErrorCodes.Common.Content.TooBig, + string.Format(Resource.MessageContentLengthTooBig, MaxByteLength + "B"))); + } + } + } +} diff --git a/BackEnd/Timeline/Filters/Resource.Designer.cs b/BackEnd/Timeline/Filters/Resource.Designer.cs index 8ae6d288..d5b533d4 100644 --- a/BackEnd/Timeline/Filters/Resource.Designer.cs +++ b/BackEnd/Timeline/Filters/Resource.Designer.cs @@ -60,6 +60,15 @@ namespace Timeline.Filters { } } + /// + /// Looks up a localized string similar to Content length is too big. It can't be bigger than {0}.. + /// + internal static string MessageContentLengthTooBig { + get { + return ResourceManager.GetString("MessageContentLengthTooBig", resourceCulture); + } + } + /// /// Looks up a localized string similar to The entity you want to create already exists. Entity type is {0}. Constraints are {1}.. /// diff --git a/BackEnd/Timeline/Filters/Resource.resx b/BackEnd/Timeline/Filters/Resource.resx index e9a5fbf9..d55e996c 100644 --- a/BackEnd/Timeline/Filters/Resource.resx +++ b/BackEnd/Timeline/Filters/Resource.resx @@ -117,6 +117,9 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + Content length is too big. It can't be bigger than {0}. + The entity you want to create already exists. Entity type is {0}. Constraints are {1}. diff --git a/BackEnd/Timeline/Helpers/Cache/DataCacheHelper.cs b/BackEnd/Timeline/Helpers/Cache/DataCacheHelper.cs index b7d86b18..1d1ab64e 100644 --- a/BackEnd/Timeline/Helpers/Cache/DataCacheHelper.cs +++ b/BackEnd/Timeline/Helpers/Cache/DataCacheHelper.cs @@ -48,7 +48,7 @@ namespace Timeline.Helpers.Cache { if (!EntityTagHeaderValue.TryParseList(ifNonMatchHeaderValue, out var eTagList)) { - return controller.BadRequest(ErrorResponse.Common.Header.IfNonMatch_BadFormat()); + return controller.BadRequest(new CommonResponse(ErrorCodes.Common.Header.IfNonMatch_BadFormat, "Header If-None-Match is of bad format.")); } if (eTagList.FirstOrDefault(e => e.Equals(eTag)) != null) diff --git a/BackEnd/Timeline/Helpers/InvalidModelResponseFactory.cs b/BackEnd/Timeline/Helpers/InvalidModelResponseFactory.cs index 9b253e7d..121aee6a 100644 --- a/BackEnd/Timeline/Helpers/InvalidModelResponseFactory.cs +++ b/BackEnd/Timeline/Helpers/InvalidModelResponseFactory.cs @@ -19,7 +19,7 @@ namespace Timeline.Helpers messageBuilder.AppendLine(error.ErrorMessage); } - return new BadRequestObjectResult(ErrorResponse.Common.CustomMessage_InvalidModel(messageBuilder.ToString())); + return new BadRequestObjectResult(new CommonResponse(ErrorCodes.Common.InvalidModel, $"Request format is bad. {messageBuilder}")); } } } diff --git a/BackEnd/Timeline/Models/Http/ErrorResponse.cs b/BackEnd/Timeline/Models/Http/ErrorResponse.cs deleted file mode 100644 index 7ab520a7..00000000 --- a/BackEnd/Timeline/Models/Http/ErrorResponse.cs +++ /dev/null @@ -1,216 +0,0 @@ -using static Timeline.Resources.Messages; - -namespace Timeline.Models.Http -{ - public static class ErrorResponse - { - public static class Common - { - public static CommonResponse InvalidModel(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.Common.InvalidModel, string.Format(Common_InvalidModel, formatArgs)); - } - - public static CommonResponse CustomMessage_InvalidModel(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.Common.InvalidModel, string.Format(message, formatArgs)); - } - - public static CommonResponse Forbid(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.Common.Forbid, string.Format(Common_Forbid, formatArgs)); - } - - public static CommonResponse CustomMessage_Forbid(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.Common.Forbid, string.Format(message, formatArgs)); - } - - public static CommonResponse UnknownEndpoint(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.Common.UnknownEndpoint, string.Format(Common_UnknownEndpoint, formatArgs)); - } - - public static CommonResponse CustomMessage_UnknownEndpoint(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.Common.UnknownEndpoint, string.Format(message, formatArgs)); - } - - public static class Header - { - public static CommonResponse IfNonMatch_BadFormat(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.Common.Header.IfNonMatch_BadFormat, string.Format(Common_Header_IfNonMatch_BadFormat, formatArgs)); - } - - public static CommonResponse CustomMessage_IfNonMatch_BadFormat(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.Common.Header.IfNonMatch_BadFormat, string.Format(message, formatArgs)); - } - - } - - public static class Content - { - public static CommonResponse TooBig(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.Common.Content.TooBig, string.Format(Common_Content_TooBig, formatArgs)); - } - - public static CommonResponse CustomMessage_TooBig(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.Common.Content.TooBig, string.Format(message, formatArgs)); - } - - } - - } - - public static class TokenController - { - public static CommonResponse Create_BadCredential(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.TokenController.Create_BadCredential, string.Format(TokenController_Create_BadCredential, formatArgs)); - } - - public static CommonResponse CustomMessage_Create_BadCredential(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.TokenController.Create_BadCredential, string.Format(message, formatArgs)); - } - - public static CommonResponse Verify_BadFormat(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.TokenController.Verify_BadFormat, string.Format(TokenController_Verify_BadFormat, formatArgs)); - } - - public static CommonResponse CustomMessage_Verify_BadFormat(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.TokenController.Verify_BadFormat, string.Format(message, formatArgs)); - } - - public static CommonResponse Verify_UserNotExist(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.TokenController.Verify_UserNotExist, string.Format(TokenController_Verify_UserNotExist, formatArgs)); - } - - public static CommonResponse CustomMessage_Verify_UserNotExist(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.TokenController.Verify_UserNotExist, string.Format(message, formatArgs)); - } - - public static CommonResponse Verify_OldVersion(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.TokenController.Verify_OldVersion, string.Format(TokenController_Verify_OldVersion, formatArgs)); - } - - public static CommonResponse CustomMessage_Verify_OldVersion(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.TokenController.Verify_OldVersion, string.Format(message, formatArgs)); - } - - public static CommonResponse Verify_TimeExpired(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.TokenController.Verify_TimeExpired, string.Format(TokenController_Verify_TimeExpired, formatArgs)); - } - - public static CommonResponse CustomMessage_Verify_TimeExpired(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.TokenController.Verify_TimeExpired, string.Format(message, formatArgs)); - } - - } - - public static class UserController - { - public static CommonResponse ChangePassword_BadOldPassword(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.UserController.ChangePassword_BadOldPassword, string.Format(UserController_ChangePassword_BadOldPassword, formatArgs)); - } - - public static CommonResponse CustomMessage_ChangePassword_BadOldPassword(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.UserController.ChangePassword_BadOldPassword, string.Format(message, formatArgs)); - } - - public static CommonResponse ChangePermission_RootUser(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.UserController.ChangePermission_RootUser, string.Format(UserController_ChangePermission_RootUser, formatArgs)); - } - - public static CommonResponse CustomMessage_ChangePermission_RootUser(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.UserController.ChangePermission_RootUser, string.Format(message, formatArgs)); - } - - public static CommonResponse Delete_RootUser(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.UserController.Delete_RootUser, string.Format(UserController_Delete_RootUser, formatArgs)); - } - - public static CommonResponse CustomMessage_Delete_RootUser(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.UserController.Delete_RootUser, string.Format(message, formatArgs)); - } - - } - - public static class UserAvatar - { - public static CommonResponse BadFormat_CantDecode(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.UserAvatar.BadFormat_CantDecode, string.Format(UserAvatar_BadFormat_CantDecode, formatArgs)); - } - - public static CommonResponse CustomMessage_BadFormat_CantDecode(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.UserAvatar.BadFormat_CantDecode, string.Format(message, formatArgs)); - } - - public static CommonResponse BadFormat_UnmatchedFormat(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.UserAvatar.BadFormat_UnmatchedFormat, string.Format(UserAvatar_BadFormat_UnmatchedFormat, formatArgs)); - } - - public static CommonResponse CustomMessage_BadFormat_UnmatchedFormat(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.UserAvatar.BadFormat_UnmatchedFormat, string.Format(message, formatArgs)); - } - - public static CommonResponse BadFormat_BadSize(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.UserAvatar.BadFormat_BadSize, string.Format(UserAvatar_BadFormat_BadSize, formatArgs)); - } - - public static CommonResponse CustomMessage_BadFormat_BadSize(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.UserAvatar.BadFormat_BadSize, string.Format(message, formatArgs)); - } - - } - - public static class TimelineController - { - public static CommonResponse QueryRelateNotExist(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.TimelineController.QueryRelateNotExist, string.Format(TimelineController_QueryRelateNotExist, formatArgs)); - } - - public static CommonResponse CustomMessage_QueryRelateNotExist(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.TimelineController.QueryRelateNotExist, string.Format(message, formatArgs)); - } - - public static CommonResponse PostNotExist(params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.NotExist.TimelinePost, string.Format(TimelineController_PostNotExist, formatArgs)); - } - - public static CommonResponse CustomMessage_PostNotExist(string message, params object?[] formatArgs) - { - return new CommonResponse(ErrorCodes.NotExist.TimelinePost, string.Format(message, formatArgs)); - } - } - - } - -} diff --git a/BackEnd/Timeline/Resources/Messages.Designer.cs b/BackEnd/Timeline/Resources/Messages.Designer.cs deleted file mode 100644 index c6b7d5e7..00000000 --- a/BackEnd/Timeline/Resources/Messages.Designer.cs +++ /dev/null @@ -1,414 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace Timeline.Resources { - using System; - - - /// - /// A strongly-typed resource class, for looking up localized strings, etc. - /// - // This class was auto-generated by the StronglyTypedResourceBuilder - // class via a tool like ResGen or Visual Studio. - // To add or remove a member, edit your .ResX file then rerun ResGen - // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class Messages { - - private static global::System.Resources.ResourceManager resourceMan; - - private static global::System.Globalization.CultureInfo resourceCulture; - - [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal Messages() { - } - - /// - /// Returns the cached ResourceManager instance used by this class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Resources.ResourceManager ResourceManager { - get { - if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Timeline.Resources.Messages", typeof(Messages).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - /// - /// Overrides the current thread's CurrentUICulture property for all - /// resource lookups using this strongly typed resource class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - - /// - /// Looks up a localized string similar to Body is too big. It can't be bigger than {0}.. - /// - internal static string Common_Content_TooBig { - get { - return ResourceManager.GetString("Common_Content_TooBig", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Actual body length is bigger than it in header.. - /// - internal static string Common_Content_UnmatchedLength_Bigger { - get { - return ResourceManager.GetString("Common_Content_UnmatchedLength_Bigger", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Actual body length is smaller than it in header.. - /// - internal static string Common_Content_UnmatchedLength_Smaller { - get { - return ResourceManager.GetString("Common_Content_UnmatchedLength_Smaller", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to You have no permission to do the operation.. - /// - internal static string Common_Forbid { - get { - return ResourceManager.GetString("Common_Forbid", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to You are not the resource owner.. - /// - internal static string Common_Forbid_NotSelf { - get { - return ResourceManager.GetString("Common_Forbid_NotSelf", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Header Content-Length is missing or of bad format.. - /// - internal static string Common_Header_ContentLength_Missing { - get { - return ResourceManager.GetString("Common_Header_ContentLength_Missing", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Header Content-Length must not be 0.. - /// - internal static string Common_Header_ContentLength_Zero { - get { - return ResourceManager.GetString("Common_Header_ContentLength_Zero", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Header Content-Type is missing.. - /// - internal static string Common_Header_ContentType_Missing { - get { - return ResourceManager.GetString("Common_Header_ContentType_Missing", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Header If-Non-Match is of bad format.. - /// - internal static string Common_Header_IfNonMatch_BadFormat { - get { - return ResourceManager.GetString("Common_Header_IfNonMatch_BadFormat", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Model is of bad format.. - /// - internal static string Common_InvalidModel { - get { - return ResourceManager.GetString("Common_InvalidModel", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to The api endpoint you request is unknown. You might get the wrong api entry.. - /// - internal static string Common_UnknownEndpoint { - get { - return ResourceManager.GetString("Common_UnknownEndpoint", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Unknown type of post content.. - /// - internal static string TimelineController_ContentUnknownType { - get { - return ResourceManager.GetString("TimelineController_ContentUnknownType", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Data field is not a valid base64 string in image content.. - /// - internal static string TimelineController_ImageContentDataNotBase64 { - get { - return ResourceManager.GetString("TimelineController_ImageContentDataNotBase64", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Data field is not a valid image after base64 decoding in image content.. - /// - internal static string TimelineController_ImageContentDataNotImage { - get { - return ResourceManager.GetString("TimelineController_ImageContentDataNotImage", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Data field is required for image content.. - /// - internal static string TimelineController_ImageContentDataRequired { - get { - return ResourceManager.GetString("TimelineController_ImageContentDataRequired", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to The user to set as member does not exist.. - /// - internal static string TimelineController_MemberPut_NotExist { - get { - return ResourceManager.GetString("TimelineController_MemberPut_NotExist", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to A timeline with given name already exists.. - /// - internal static string TimelineController_NameConflict { - get { - return ResourceManager.GetString("TimelineController_NameConflict", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to The timeline with given name does not exist.. - /// - internal static string TimelineController_NotExist { - get { - return ResourceManager.GetString("TimelineController_NotExist", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to The post of that type has no data.. - /// - internal static string TimelineController_PostNoData { - get { - return ResourceManager.GetString("TimelineController_PostNoData", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to The post to operate on does not exist.. - /// - internal static string TimelineController_PostNotExist { - get { - return ResourceManager.GetString("TimelineController_PostNotExist", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to The user specified by query param "relate" does not exist.. - /// - internal static string TimelineController_QueryRelateNotExist { - get { - return ResourceManager.GetString("TimelineController_QueryRelateNotExist", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to '{0}' is an unkown visibility in the query parameter 'visibility'. . - /// - internal static string TimelineController_QueryVisibilityUnknown { - get { - return ResourceManager.GetString("TimelineController_QueryVisibilityUnknown", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Text field is required for text content.. - /// - internal static string TimelineController_TextContentTextRequired { - get { - return ResourceManager.GetString("TimelineController_TextContentTextRequired", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Username or password is invalid.. - /// - internal static string TokenController_Create_BadCredential { - get { - return ResourceManager.GetString("TokenController_Create_BadCredential", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to The token is of bad format. It might not be created by the server.. - /// - internal static string TokenController_Verify_BadFormat { - get { - return ResourceManager.GetString("TokenController_Verify_BadFormat", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Token has an old version. User might have update some info.. - /// - internal static string TokenController_Verify_OldVersion { - get { - return ResourceManager.GetString("TokenController_Verify_OldVersion", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to The token is expired.. - /// - internal static string TokenController_Verify_TimeExpired { - get { - return ResourceManager.GetString("TokenController_Verify_TimeExpired", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to User does not exist. Administrator might have deleted this user.. - /// - internal static string TokenController_Verify_UserNotExist { - get { - return ResourceManager.GetString("TokenController_Verify_UserNotExist", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Image is not a square.. - /// - internal static string UserAvatar_BadFormat_BadSize { - get { - return ResourceManager.GetString("UserAvatar_BadFormat_BadSize", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Image decode failed.. - /// - internal static string UserAvatar_BadFormat_CantDecode { - get { - return ResourceManager.GetString("UserAvatar_BadFormat_CantDecode", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Image format does not match the one in header.. - /// - internal static string UserAvatar_BadFormat_UnmatchedFormat { - get { - return ResourceManager.GetString("UserAvatar_BadFormat_UnmatchedFormat", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to The user to operate on does not exist.. - /// - internal static string UserCommon_NotExist { - get { - return ResourceManager.GetString("UserCommon_NotExist", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Old password is wrong.. - /// - internal static string UserController_ChangePassword_BadOldPassword { - get { - return ResourceManager.GetString("UserController_ChangePassword_BadOldPassword", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to You can't change permission of root user.. - /// - internal static string UserController_ChangePermission_RootUser { - get { - return ResourceManager.GetString("UserController_ChangePermission_RootUser", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to You can't delete root user.. - /// - internal static string UserController_Delete_RootUser { - get { - return ResourceManager.GetString("UserController_Delete_RootUser", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to You can't set permission unless you are administrator.. - /// - internal static string UserController_Patch_Forbid_Administrator { - get { - return ResourceManager.GetString("UserController_Patch_Forbid_Administrator", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to You can't set password unless you are administrator. If you want to change password, use /userop/changepassword .. - /// - internal static string UserController_Patch_Forbid_Password { - get { - return ResourceManager.GetString("UserController_Patch_Forbid_Password", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to You can't set username unless you are administrator.. - /// - internal static string UserController_Patch_Forbid_Username { - get { - return ResourceManager.GetString("UserController_Patch_Forbid_Username", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to A user with given username already exists.. - /// - internal static string UserController_UsernameConflict { - get { - return ResourceManager.GetString("UserController_UsernameConflict", resourceCulture); - } - } - } -} diff --git a/BackEnd/Timeline/Resources/Messages.resx b/BackEnd/Timeline/Resources/Messages.resx deleted file mode 100644 index 2386d1eb..00000000 --- a/BackEnd/Timeline/Resources/Messages.resx +++ /dev/null @@ -1,237 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - Body is too big. It can't be bigger than {0}. - - - Actual body length is bigger than it in header. - - - Actual body length is smaller than it in header. - - - You have no permission to do the operation. - - - You are not the resource owner. - - - Header Content-Length is missing or of bad format. - - - Header Content-Length must not be 0. - - - Header Content-Type is missing. - - - Header If-Non-Match is of bad format. - - - Model is of bad format. - - - The api endpoint you request is unknown. You might get the wrong api entry. - - - Unknown type of post content. - - - Data field is not a valid base64 string in image content. - - - Data field is not a valid image after base64 decoding in image content. - - - Data field is required for image content. - - - The user to set as member does not exist. - - - A timeline with given name already exists. - - - The timeline with given name does not exist. - - - The post of that type has no data. - - - The post to operate on does not exist. - - - The user specified by query param "relate" does not exist. - - - '{0}' is an unkown visibility in the query parameter 'visibility'. - - - Text field is required for text content. - - - Username or password is invalid. - - - The token is of bad format. It might not be created by the server. - - - Token has an old version. User might have update some info. - - - The token is expired. - - - User does not exist. Administrator might have deleted this user. - - - Image is not a square. - - - Image decode failed. - - - Image format does not match the one in header. - - - The user to operate on does not exist. - - - Old password is wrong. - - - You can't change permission of root user. - - - You can't delete root user. - - - You can't set permission unless you are administrator. - - - You can't set password unless you are administrator. If you want to change password, use /userop/changepassword . - - - You can't set username unless you are administrator. - - - A user with given username already exists. - - \ No newline at end of file diff --git a/BackEnd/Timeline/Routes/Resource.Designer.cs b/BackEnd/Timeline/Routes/Resource.Designer.cs new file mode 100644 index 00000000..1fbbb704 --- /dev/null +++ b/BackEnd/Timeline/Routes/Resource.Designer.cs @@ -0,0 +1,72 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Timeline.Routes { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resource { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resource() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Timeline.Routes.Resource", typeof(Resource).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to The api endpoint you request is unknown. You might get the wrong api entry.. + /// + internal static string MessageUnknownEndpoint { + get { + return ResourceManager.GetString("MessageUnknownEndpoint", resourceCulture); + } + } + } +} diff --git a/BackEnd/Timeline/Routes/Resource.resx b/BackEnd/Timeline/Routes/Resource.resx new file mode 100644 index 00000000..f7e2aec9 --- /dev/null +++ b/BackEnd/Timeline/Routes/Resource.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + The api endpoint you request is unknown. You might get the wrong api entry. + + \ No newline at end of file diff --git a/BackEnd/Timeline/Routes/UnknownEndpointMiddleware.cs b/BackEnd/Timeline/Routes/UnknownEndpointMiddleware.cs index 25ec563c..d3f78fd9 100644 --- a/BackEnd/Timeline/Routes/UnknownEndpointMiddleware.cs +++ b/BackEnd/Timeline/Routes/UnknownEndpointMiddleware.cs @@ -24,7 +24,9 @@ namespace Timeline.Routes context.Response.StatusCode = StatusCodes.Status400BadRequest; context.Response.ContentType = MediaTypeNames.Application.Json; - var body = JsonSerializer.SerializeToUtf8Bytes(ErrorResponse.Common.UnknownEndpoint(), new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); + var body = JsonSerializer.SerializeToUtf8Bytes( + new CommonResponse(ErrorCodes.Common.UnknownEndpoint, Resource.MessageUnknownEndpoint), + new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); context.Response.ContentLength = body.Length; await context.Response.Body.WriteAsync(body); diff --git a/BackEnd/Timeline/Timeline.csproj b/BackEnd/Timeline/Timeline.csproj index fb111cef..96f65301 100644 --- a/BackEnd/Timeline/Timeline.csproj +++ b/BackEnd/Timeline/Timeline.csproj @@ -68,11 +68,6 @@ True DataCacheHelper.resx - - True - True - Messages.resx - True True @@ -93,6 +88,11 @@ True Validator.resx + + True + True + Resource.resx + True True @@ -157,10 +157,6 @@ ResXFileCodeGenerator DataCacheHelper.Designer.cs - - ResXFileCodeGenerator - Messages.Designer.cs - ResXFileCodeGenerator Resource.Designer.cs @@ -177,6 +173,10 @@ ResXFileCodeGenerator Validator.Designer.cs + + ResXFileCodeGenerator + Resource.Designer.cs + ResXFileCodeGenerator Resource.Designer.cs -- cgit v1.2.3