Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue branch 340 #2199

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions AllReadyApp/Web-App/AllReady/Constants/ControllerNames.cs
Original file line number Diff line number Diff line change
@@ -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";

/// <summary>
/// Use to take the Controller name without the "Controller" suffix. For example
/// NameOf(DashboarHubController) == "DashboardHub".
/// Useful for the url redirect use case and similar.
/// </summary>
/// <param name="controllerType"></param>
/// <returns></returns>
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);
}
}
}
}
21 changes: 13 additions & 8 deletions AllReadyApp/Web-App/AllReady/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ public async Task<IActionResult> 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");
}
Expand Down Expand Up @@ -143,7 +144,7 @@ public async Task<IActionResult> 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
});
Expand Down Expand Up @@ -204,7 +205,7 @@ public async Task<IActionResult> ConfirmEmail(string userId, string token)
}
}

return View(result.Succeeded ? "ConfirmEmail" : "Error");
return View(result.Succeeded ? nameof(AccountController.ConfirmEmail) : "Error");
}

// GET: /Account/ForgotPassword
Expand All @@ -231,7 +232,7 @@ public async Task<IActionResult> 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 });

Expand Down Expand Up @@ -381,7 +382,7 @@ public async Task<IActionResult> 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
});
Expand Down Expand Up @@ -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");
Expand Down
33 changes: 33 additions & 0 deletions AllReadyApp/Web-App/AllReady/Controllers/ControllerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.AspNetCore.Mvc;
using System;

namespace AllReady.Controllers
{
public static class ControllerExtensions
{
/// <summary>
/// Use to take the Controller name without the "Controller" suffix. For example
/// NameOf(DashboarHubController) == "DashboardHub".
/// Useful for the url redirect use case and similar.
/// </summary>
/// <param name="controller"></param>
/// <returns></returns>
public static string Name(this Controller controller)
{
return Constants.ControllerNames.NameOf(controller.GetType());
}

/// <summary>
/// Use to take the Controller name without the "Controller" suffix. For example
/// NameOf(DashboarHubController) == "DashboardHub".
/// Useful for the url redirect use case and similar.
/// </summary>
/// <param name="_"></param>
/// <param name="otherControllerType">The type of the other controller that we want to reference</param>
/// <returns></returns>
public static string Name(this Controller _, Type otherControllerType)
{
return Constants.ControllerNames.NameOf(otherControllerType);
}
}
}