From 5ffd962dc03da64b4ec56848b4d68a6dcfa8c791 Mon Sep 17 00:00:00 2001 From: Georgi Parlakov Date: Sat, 6 Jan 2018 21:02:47 +0200 Subject: [PATCH 1/2] Add a NameOf method to extract the name of a controller sans the suffix. Also add a couple of convenience extension methods that use it. --- .../AllReady/Constants/ControllerNames.cs | 24 +++++++++++++++++-- .../AllReady/Controllers/AccountController.cs | 23 +++++++++++------- .../Controllers/ControllerExtensions.cs | 18 ++++++++++++++ 3 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 AllReadyApp/Web-App/AllReady/Controllers/ControllerExtensions.cs diff --git a/AllReadyApp/Web-App/AllReady/Constants/ControllerNames.cs b/AllReadyApp/Web-App/AllReady/Constants/ControllerNames.cs index 997577d75..2ece21c6f 100644 --- a/AllReadyApp/Web-App/AllReady/Constants/ControllerNames.cs +++ b/AllReadyApp/Web-App/AllReady/Constants/ControllerNames.cs @@ -1,7 +1,27 @@ -namespace AllReady.Constants +using System; + +namespace AllReady.Constants { public static class ControllerNames { public const string Admin = "Admin"; + private const string ControllerSuffix = "Controller"; + + /// + /// Use to take the Controller name without the "Controller" suffix. For example + /// NameOf(DashboarHubController) == "DashboardHub". + /// Useful for the url redirect use case and similar. + /// + /// + /// + public static string NameOf(Type controllerType) + { + if (controllerType == null) + { + throw new ArgumentNullException(nameof(controllerType), "Passed in null type. Expected controller type."); + } + + return controllerType.Name.Replace(ControllerSuffix, string.Empty); + } } -} \ No newline at end of file +} diff --git a/AllReadyApp/Web-App/AllReady/Controllers/AccountController.cs b/AllReadyApp/Web-App/AllReady/Controllers/AccountController.cs index 24100f3d3..dcf029fdb 100644 --- a/AllReadyApp/Web-App/AllReady/Controllers/AccountController.cs +++ b/AllReadyApp/Web-App/AllReady/Controllers/AccountController.cs @@ -44,7 +44,7 @@ IRedirectAccountControllerRequests redirectAccountControllerRequests _generalSettings = generalSettings; _mediator = mediator; _externalUserInformationProviderFactory = externalUserInformationProviderFactory; - _redirectAccountControllerRequests = redirectAccountControllerRequests; + _redirectAccountControllerRequests = redirectAccountControllerRequests; } // GET: /Account/Login @@ -75,8 +75,9 @@ public async Task Login(LoginViewModel model, string returnUrl = var isAdminUser = user.IsUserType(UserType.OrgAdmin) || user.IsUserType(UserType.SiteAdmin); if (isAdminUser && !await _userManager.IsEmailConfirmedAsync(user)) { - //TODO: Showing the error page here makes for a bad experience for the user. - //It would be better if we redirected to a specific page prompting the user to check their email for a confirmation email and providing an option to resend the confirmation email. + // TODO: Showing the error page here makes for a bad experience for the user. + // It would be better if we redirected to a specific page prompting the user to check their + // email for a confirmation email and providing an option to resend the confirmation email. ViewData["Message"] = "You must have a confirmed email to log on."; return View("Error"); } @@ -143,7 +144,7 @@ public async Task Register(RegisterViewModel model) var callbackUrl = Url.Action(new UrlActionContext { Action = nameof(ConfirmEmail), - Controller = "Account", + Controller = this.Name(), Values = new { userId = user.Id, token = emailConfirmationToken }, Protocol = HttpContext.Request.Scheme }); @@ -204,7 +205,7 @@ public async Task ConfirmEmail(string userId, string token) } } - return View(result.Succeeded ? "ConfirmEmail" : "Error"); + return View(result.Succeeded ? nameof(AccountController.ConfirmEmail) : "Error"); } // GET: /Account/ForgotPassword @@ -231,7 +232,7 @@ public async Task ForgotPassword(ForgotPasswordViewModel model) } var code = await _userManager.GeneratePasswordResetTokenAsync(user); - var callbackUrl = Url.Action(new UrlActionContext { Action = nameof(ResetPassword), Controller = "Account", Values = new { userId = user.Id, code }, + var callbackUrl = Url.Action(new UrlActionContext { Action = nameof(ResetPassword), Controller = this.Name(), Values = new { userId = user.Id, code }, Protocol = HttpContext.Request.Scheme }); await _mediator.SendAsync(new SendResetPasswordEmail { Email = model.Email, CallbackUrl = callbackUrl }); @@ -381,7 +382,7 @@ public async Task ExternalLoginConfirmation(ExternalLoginConfirma var callbackUrl = Url.Action(new UrlActionContext { Action = nameof(ConfirmEmail), - Controller = "Account", + Controller = this.Name(), Values = new { userId = user.Id, token = emailConfirmationToken }, Protocol = HttpContext.Request.Scheme }); @@ -437,12 +438,16 @@ public IActionResult RedirectToLocal(string returnUrl, ApplicationUser user) if (user.IsUserType(UserType.SiteAdmin)) { - return new RedirectToActionResult(nameof(SiteController.Index), "Site", new { area = AreaNames.Admin }); + return new RedirectToActionResult(nameof(SiteController.Index), + ControllerNames.NameOf(typeof(SiteController)), + new { area = AreaNames.Admin }); } if (user.IsUserType(UserType.OrgAdmin)) { - return new RedirectToActionResult(nameof(Areas.Admin.Controllers.CampaignController.Index), "Campaign", new { area = AreaNames.Admin }); + return new RedirectToActionResult(nameof(Areas.Admin.Controllers.CampaignController.Index), + ControllerNames.NameOf(typeof(CampaignController)), + new { area = AreaNames.Admin }); } return new RedirectToPageResult("/Index"); diff --git a/AllReadyApp/Web-App/AllReady/Controllers/ControllerExtensions.cs b/AllReadyApp/Web-App/AllReady/Controllers/ControllerExtensions.cs new file mode 100644 index 000000000..53226321a --- /dev/null +++ b/AllReadyApp/Web-App/AllReady/Controllers/ControllerExtensions.cs @@ -0,0 +1,18 @@ +using Microsoft.AspNetCore.Mvc; +using System; + +namespace AllReady.Controllers +{ + public static class ControllerExtensions + { + public static string Name(this Controller controller) + { + return Constants.ControllerNames.NameOf(controller.GetType()); + } + + public static string Name(this Controller _, Type otherConrrollerType) + { + return Constants.ControllerNames.NameOf(otherConrrollerType); + } + } +} From 6c96b1ac842d396e693d825c7559210910a0d433 Mon Sep 17 00:00:00 2001 From: Georgi Parlakov Date: Sat, 6 Jan 2018 21:10:08 +0200 Subject: [PATCH 2/2] Add some comments to the extension methods Rollback whitespace --- .../AllReady/Controllers/AccountController.cs | 2 +- .../Controllers/ControllerExtensions.cs | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/AllReadyApp/Web-App/AllReady/Controllers/AccountController.cs b/AllReadyApp/Web-App/AllReady/Controllers/AccountController.cs index dcf029fdb..31fac7b83 100644 --- a/AllReadyApp/Web-App/AllReady/Controllers/AccountController.cs +++ b/AllReadyApp/Web-App/AllReady/Controllers/AccountController.cs @@ -44,7 +44,7 @@ IRedirectAccountControllerRequests redirectAccountControllerRequests _generalSettings = generalSettings; _mediator = mediator; _externalUserInformationProviderFactory = externalUserInformationProviderFactory; - _redirectAccountControllerRequests = redirectAccountControllerRequests; + _redirectAccountControllerRequests = redirectAccountControllerRequests; } // GET: /Account/Login diff --git a/AllReadyApp/Web-App/AllReady/Controllers/ControllerExtensions.cs b/AllReadyApp/Web-App/AllReady/Controllers/ControllerExtensions.cs index 53226321a..5365322d0 100644 --- a/AllReadyApp/Web-App/AllReady/Controllers/ControllerExtensions.cs +++ b/AllReadyApp/Web-App/AllReady/Controllers/ControllerExtensions.cs @@ -5,14 +5,29 @@ namespace AllReady.Controllers { public static class ControllerExtensions { + /// + /// Use to take the Controller name without the "Controller" suffix. For example + /// NameOf(DashboarHubController) == "DashboardHub". + /// Useful for the url redirect use case and similar. + /// + /// + /// public static string Name(this Controller controller) { return Constants.ControllerNames.NameOf(controller.GetType()); } - public static string Name(this Controller _, Type otherConrrollerType) + /// + /// Use to take the Controller name without the "Controller" suffix. For example + /// NameOf(DashboarHubController) == "DashboardHub". + /// Useful for the url redirect use case and similar. + /// + /// + /// The type of the other controller that we want to reference + /// + public static string Name(this Controller _, Type otherControllerType) { - return Constants.ControllerNames.NameOf(otherConrrollerType); + return Constants.ControllerNames.NameOf(otherControllerType); } } }