Skip to content
This repository has been archived by the owner on Dec 6, 2019. It is now read-only.

Commit

Permalink
Merge pull request #262 from Sitecore/feature/Authentication
Browse files Browse the repository at this point in the history
Refactored redirect logic for Authentication
  • Loading branch information
eldblom authored Dec 28, 2016
2 parents f062b0b + 1e356ed commit da481a4
Show file tree
Hide file tree
Showing 66 changed files with 1,355 additions and 481 deletions.
126 changes: 17 additions & 109 deletions src/Feature/Accounts/Tests/AccountsControllerTests.cs

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AccountTrackerServiceTests.cs" />
<Compile Include="Attributes\AccountsRedirectAuthenticatedAttributeTests.cs" />
<Compile Include="Attributes\RedirectAuthenticatedAttributeTests.cs" />
<Compile Include="Attributes\RedirectUnauthenticatedTests.cs" />
<Compile Include="Attributes\ValidateModelTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<settings>
<setting name="Sitecore.Feature.Accounts.LoginGoalId" value="{66722F52-2D13-4DCC-90FC-EA7117CF2298}" />
<setting name="Sitecore.Feature.Accounts.RegisterGoalId" value="{8FFB183B-DA1A-4C74-8F3A-9729E9FCFF6A}" />
</settings>
<pipelines>
<initialize>
<processor type="Sitecore.Feature.Accounts.Pipelines.RegisterWebApiRoutes, Sitecore.Feature.Accounts"
patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" />
</initialize>
</pipelines>
</sitecore>
<sitecore>
<settings>
<setting name="Sitecore.Feature.Accounts.LoginGoalId" value="{66722F52-2D13-4DCC-90FC-EA7117CF2298}" />
<setting name="Sitecore.Feature.Accounts.RegisterGoalId" value="{8FFB183B-DA1A-4C74-8F3A-9729E9FCFF6A}" />
</settings>
<pipelines>
<initialize>
<processor type="Sitecore.Feature.Accounts.Pipelines.RegisterWebApiRoutes, Sitecore.Feature.Accounts" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" />
</initialize>
</pipelines>
</sitecore>
</configuration>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
namespace Sitecore.Feature.Accounts.Attributes
{
using System;
using System.Web.Mvc;
using Sitecore.Foundation.SitecoreExtensions.Extensions;
using System.Web.Mvc;
using Sitecore.Feature.Accounts.Services;

public class RedirectAuthenticatedAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
public class RedirectAuthenticatedAttribute : ActionFilterAttribute
{
if (!Context.PageMode.IsNormal)
return;
if (!Context.User.IsAuthenticated)
return;
var link = this.GetRedirectUrl(filterContext);
if (filterContext.HttpContext.Request.RawUrl.Equals(link, StringComparison.InvariantCultureIgnoreCase))
{
link = this.RedirectUrl;
}
private readonly IGetRedirectUrlService getRedirectUrlService;

filterContext.Result = new RedirectResult(link);
}
public RedirectAuthenticatedAttribute() : this(new GetRedirectUrlService())
{
}

protected virtual string GetRedirectUrl(ActionExecutingContext filterContext)
{
return this.RedirectUrl;
}
public RedirectAuthenticatedAttribute(IGetRedirectUrlService getRedirectUrlService)
{
this.getRedirectUrlService = getRedirectUrlService;
}

private string RedirectUrl => Context.Site.GetRootItem().Url();
}

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!Context.PageMode.IsNormal)
return;
if (!Context.User.IsAuthenticated)
return;
var link = this.getRedirectUrlService.GetRedirectUrl(AuthenticationStatus.Authenticated);
filterContext.Result = new RedirectResult(link);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
namespace Sitecore.Feature.Accounts.Attributes
{
using System.Web.Mvc;
using Sitecore.Foundation.SitecoreExtensions.Extensions;
using System;
using System.Web.Mvc;
using Sitecore.Feature.Accounts.Services;
using Sitecore.Foundation.SitecoreExtensions.Extensions;

public class RedirectUnauthenticatedAttribute : ActionFilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
public class RedirectUnauthenticatedAttribute : ActionFilterAttribute, IAuthorizationFilter
{
if (!Context.User.IsAuthenticated)
{
filterContext.Result = new RedirectResult(Context.Site.GetRootItem().Url());
}
private readonly IGetRedirectUrlService getRedirectUrlService;

public RedirectUnauthenticatedAttribute() : this(new GetRedirectUrlService())
{
}

private RedirectUnauthenticatedAttribute(IGetRedirectUrlService getRedirectUrlService)
{
this.getRedirectUrlService = getRedirectUrlService;
}

public void OnAuthorization(AuthorizationContext context)
{
if (Context.User.IsAuthenticated)
return;
var link = this.getRedirectUrlService.GetRedirectUrl(AuthenticationStatus.Unauthenticated, context.HttpContext.Request.RawUrl);
context.Result = new RedirectResult(link);
}
}
}
}
36 changes: 25 additions & 11 deletions src/Feature/Accounts/code/Controllers/AccountsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,26 @@ public class AccountsController : Controller
private readonly IAccountRepository accountRepository;
private readonly INotificationService notificationService;
private readonly IAccountsSettingsService accountsSettingsService;
private readonly IGetRedirectUrlService getRedirectUrlService;
private readonly IUserProfileService userProfileService;
private readonly IContactProfileService contactProfileService;

public AccountsController() : this(new AccountRepository(new AccountTrackerService(new AccountsSettingsService(), new TrackerService())), new NotificationService(new AccountsSettingsService()), new AccountsSettingsService(), new UserProfileService(), new ContactProfileService())
public AccountsController() : this(new AccountRepository(new AccountTrackerService(new AccountsSettingsService(), new TrackerService())), new NotificationService(new AccountsSettingsService()), new AccountsSettingsService(), new GetRedirectUrlService(), new UserProfileService(), new ContactProfileService())
{
}

public AccountsController(IAccountRepository accountRepository, INotificationService notificationService, IAccountsSettingsService accountsSettingsService, IUserProfileService userProfileService, IContactProfileService contactProfileService)
public AccountsController(IAccountRepository accountRepository, INotificationService notificationService, IAccountsSettingsService accountsSettingsService, IGetRedirectUrlService getRedirectUrlService, IUserProfileService userProfileService, IContactProfileService contactProfileService)
{
this.accountRepository = accountRepository;
this.notificationService = notificationService;
this.accountsSettingsService = accountsSettingsService;
this.getRedirectUrlService = getRedirectUrlService;
this.userProfileService = userProfileService;
this.contactProfileService = contactProfileService;
}


[AccountsRedirectAuthenticated]
[RedirectAuthenticated]
public ActionResult Register()
{
return this.View();
Expand All @@ -47,7 +49,7 @@ public ActionResult Register()

[HttpPost]
[ValidateModel]
[AccountsRedirectAuthenticated]
[RedirectAuthenticated]
[ValidateRenderingId]
public ActionResult Register(RegistrationInfo registrationInfo)
{
Expand All @@ -63,7 +65,7 @@ public ActionResult Register(RegistrationInfo registrationInfo)
this.accountRepository.RegisterUser(registrationInfo.Email, registrationInfo.Password, this.userProfileService.GetUserDefaultProfileId());
this.contactProfileService?.SetPreferredEmail(registrationInfo.Email);

var link = this.accountsSettingsService.GetPageLinkOrDefault(Context.Item, Templates.AccountsSettings.Fields.AfterLoginPage, Context.Site.GetRootItem());
var link = this.getRedirectUrlService.GetRedirectUrl(AuthenticationStatus.Authenticated);
return this.Redirect(link);
}
catch (MembershipCreateUserException ex)
Expand All @@ -75,10 +77,14 @@ public ActionResult Register(RegistrationInfo registrationInfo)
}
}

[AccountsRedirectAuthenticated]
public ActionResult Login()
[RedirectAuthenticated]
public ActionResult Login(string returnUrl = null)
{
return this.View();
var loginInfo = new LoginInfo
{
ReturnUrl = returnUrl
};
return this.View(loginInfo);
}

public ActionResult LoginTeaser()
Expand Down Expand Up @@ -112,7 +118,7 @@ protected virtual ActionResult Login(LoginInfo loginInfo, Func<string, ActionRes
var redirectUrl = loginInfo.ReturnUrl;
if (string.IsNullOrEmpty(redirectUrl))
{
redirectUrl = this.accountsSettingsService.GetPageLinkOrDefault(Context.Item, Templates.AccountsSettings.Fields.AfterLoginPage, Context.Site.GetRootItem());
redirectUrl = this.getRedirectUrlService.GetRedirectUrl(AuthenticationStatus.Authenticated);
}

return redirectAction(redirectUrl);
Expand Down Expand Up @@ -145,7 +151,7 @@ public ActionResult Logout()

private static string ForgotPasswordEmailNotConfigured => DictionaryPhraseRepository.Current.Get("/Accounts/Forgot Password/Email Not Configured", "The Forgot Password E-mail has not been configured");

[AccountsRedirectAuthenticated]
[RedirectAuthenticated]
public ActionResult ForgotPassword()
{
try
Expand All @@ -164,7 +170,7 @@ public ActionResult ForgotPassword()

[HttpPost]
[ValidateModel]
[AccountsRedirectAuthenticated]
[RedirectAuthenticated]
public ActionResult ForgotPassword(PasswordResetInfo model)
{
if (!this.accountRepository.Exists(model.Email))
Expand Down Expand Up @@ -229,6 +235,14 @@ public virtual ActionResult EditProfile(EditProfile profile)
return this.Redirect(this.Request.RawUrl);
}


[HttpGet]
[RedirectUnauthenticated]
public ActionResult Unauthorized()
{
return this.RedirectPermanent("/hello");
}

private ViewResult ProfileMismatchMessage
{
get
Expand Down
Loading

0 comments on commit da481a4

Please sign in to comment.