Skip to content

Commit

Permalink
* Added an Authorize attribute for checking Sitecore user logged in a…
Browse files Browse the repository at this point in the history
…nd redirecting if not.

* Added an DisplayName attribute to use that uses the SC Dictionary for sourcing it's text.
  • Loading branch information
benmccallum committed Mar 22, 2016
1 parent 3d6d3dd commit 9f65327
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 4 deletions.
4 changes: 2 additions & 2 deletions CoreSitecore.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("8.1.3.0")]
[assembly: AssemblyFileVersion("8.1.3.0")]
[assembly: AssemblyVersion("8.1.4.0")]
[assembly: AssemblyFileVersion("8.1.4.0")]
3 changes: 3 additions & 0 deletions CoreSitecore/CoreSitecore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<Compile Include="Data\Items\ItemAdapter.cs" />
<Compile Include="Helpers\ValidationHelper.cs" />
<Compile Include="Links\UrlOptionsExtensions.cs" />
<Compile Include="Sys\ComponentModel\ScDisplayNameAttribute.cs" />
<Compile Include="Sys\ComponentModel\ScEmailAddressAttribute.cs" />
<Compile Include="Sys\ComponentModel\ScMinLengthAttribute.cs" />
<Compile Include="Sys\ComponentModel\ScRegularExpressionAttribute.cs" />
Expand All @@ -97,6 +98,8 @@
<Compile Include="Sys\ComponentModel\ScRequiredAttribute.cs" />
<Compile Include="Sys\StringExtensions.cs" />
<Compile Include="Helpers\UrlHelper.cs" />
<Compile Include="Sys\Web\Mvc\AuthorizeByDomainAttribute.cs" />
<Compile Include="Sys\Web\Mvc\AuthorizeExtranetAttribute.cs" />
<Compile Include="Sys\Web\Mvc\ValidateFormHandler.cs" />
<Compile Include="Web\UI\DynamicKeyPlaceholder.cs" />
<Compile Include="Web\UI\LayoutBase.cs">
Expand Down
4 changes: 2 additions & 2 deletions CoreSitecore/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("8.1.3.0")]
[assembly: AssemblyFileVersion("8.1.3.0")]
[assembly: AssemblyVersion("8.1.4.0")]
[assembly: AssemblyFileVersion("8.1.4.0")]
32 changes: 32 additions & 0 deletions CoreSitecore/Sys/ComponentModel/ScDisplayNameAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Sitecore.Globalization;
using System.ComponentModel;

namespace CoreSitecore.Sys.ComponentModel
{
/// <summary>
/// A DisplayName attribute that gets the text to use from the Sitecore Dictionary (localised to current language).
///
/// A better solution though is to use
/// </summary>
public class ScDisplayNameAttribute : DisplayNameAttribute
{
private readonly string _dictionaryKey;

/// <summary>
/// Initializes a DisplayName field attribute that gets its text from the Sitecore Dictionary using <paramref name="dictionaryKey"/>.
/// </summary>
/// <param name="dictionaryKey">Key to lookup in Sitecore Dictionary.</param>
public ScDisplayNameAttribute(string dictionaryKey)
{
_dictionaryKey = dictionaryKey;
}

public override string DisplayName
{
get
{
return Translate.Text(_dictionaryKey);
}
}
}
}
40 changes: 40 additions & 0 deletions CoreSitecore/Sys/Web/Mvc/AuthorizeByDomainAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Web;
using System.Web.Mvc;

namespace CoreSitecore.Sys.Web.Mvc
{
/// <summary>
/// An AuthorizeByDomain attribute implementation that checks the current Sitecore.Context.User is not null, authenticated and belongs to extranet domain.
/// </summary>
public class AuthorizeByDomainAttribute : AuthorizeAttribute
{
private readonly string _domainName;

/// <summary>
/// If specified, on an unauthorized request, user will be redirected to this url.
/// </summary>
public string RedirectToUrl { get; set; }

public AuthorizeByDomainAttribute(string domainName)
{
_domainName = domainName;
}

protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var user = Sitecore.Context.User;
return user != null && user.IsAuthenticated && user.GetDomainName().Equals(_domainName, StringComparison.OrdinalIgnoreCase);
}

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (!string.IsNullOrWhiteSpace(RedirectToUrl))
{
filterContext.Result = new RedirectResult(RedirectToUrl);
}

base.HandleUnauthorizedRequest(filterContext);
}
}
}
14 changes: 14 additions & 0 deletions CoreSitecore/Sys/Web/Mvc/AuthorizeExtranetAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace CoreSitecore.Sys.Web.Mvc
{
/// <summary>
/// An Authorize attribute implementation that checks the current Sitecore.Context.User is not null, authenticated and belongs to extranet domain.
/// </summary>
public class AuthorizeExtranetAttribute : AuthorizeByDomainAttribute
{
public AuthorizeExtranetAttribute()
: base("extranet")
{

}
}
}

0 comments on commit 9f65327

Please sign in to comment.