From 5bd826d47c30f6e6ac6eded4fefb99f26786c3bc Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 30 Apr 2021 17:53:40 +0800 Subject: refactor: ... --- .../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 +-- 8 files changed, 236 insertions(+), 36 deletions(-) (limited to 'BackEnd/Timeline/Controllers') 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); } } } -- cgit v1.2.3