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..31fac7b83 100644 --- a/AllReadyApp/Web-App/AllReady/Controllers/AccountController.cs +++ b/AllReadyApp/Web-App/AllReady/Controllers/AccountController.cs @@ -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..5365322d0 --- /dev/null +++ b/AllReadyApp/Web-App/AllReady/Controllers/ControllerExtensions.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Mvc; +using System; + +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()); + } + + /// + /// 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(otherControllerType); + } + } +}