Skip to content

Commit

Permalink
* Upgraded ref DLLs to 8.1.1.
Browse files Browse the repository at this point in the history
* Added a bunch of Sitecore.Analytics.Tracking.Contact extension methods.
* Added a RedirectExtranetUsers attribute to force them to say their profile page if they try to hit a login/register page already signed in.
  • Loading branch information
benmccallum committed Apr 4, 2016
1 parent e0bede1 commit 6a86dc8
Show file tree
Hide file tree
Showing 15 changed files with 1,965 additions and 227 deletions.
Binary file not shown.
Binary file added CoreSitecore.Dependencies/Sitecore.Analytics.dll
Binary file not shown.
Binary file modified CoreSitecore.Dependencies/Sitecore.Kernel.dll
Binary file not shown.
1,876 changes: 1,678 additions & 198 deletions CoreSitecore.Dependencies/Sitecore.Kernel.xml

Large diffs are not rendered by default.

Binary file added CoreSitecore.Dependencies/Sitecore.Social.dll
Binary file not shown.
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.10.0")]
[assembly: AssemblyFileVersion("8.1.10.0")]
[assembly: AssemblyVersion("8.1.11.0")]
[assembly: AssemblyFileVersion("8.1.11.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Sitecore.Analytics.Model.Framework;

public static class IElementDictionaryExtensions
{
public static TElement GetSafely<TElement>(this IElementDictionary<TElement> dict, string key)
where TElement : class, Sitecore.Analytics.Model.Framework.IElement
{
return dict.Contains(key) ? dict[key] : null;
}

}
155 changes: 155 additions & 0 deletions CoreSitecore/Analytics/Tracking/ContactExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
using Sitecore.Analytics.Model.Entities;
using Sitecore.Analytics.Tracking;
using Sitecore.Social.Connector.Facets.Contact.SocialProfile;
using System;

public static class ContactExtensions
{
#region Sitecore - Social Profiles

public const string FacebookNetworkKey = "Facebook";
public const string GoogleNetworkKey = "Google";
public const string TwitterNetworkKey = "Twitter";

public static ISocialProfileFacet GetSocialProfile(this Contact contact)
{
return contact.GetFacet<ISocialProfileFacet>("SocialProfile"); // Key must match Sitecore's configuration
}

/// <summary>
/// Gets the first or default, so should only be used for registration social additional info page.
/// </summary>
/// <param name="contact"></param>
/// <returns></returns>
public static INetworkElement GetSocialProfileNetwork(this Contact contact, string providerKey)
{
var socialProfile = contact.GetSocialProfile();
return socialProfile.Networks.GetSafely(providerKey);
}

public static INetworkElement GetFacebookNetwork(this Contact contact)
{
return contact.GetSocialProfileNetwork(FacebookNetworkKey);
}

public static INetworkElement GetGoogleNetwork(this Contact contact)
{
return contact.GetSocialProfileNetwork(GoogleNetworkKey);
}

public static INetworkElement GetTwitterNetwork(this Contact contact)
{
return contact.GetSocialProfileNetwork(TwitterNetworkKey);
}

#endregion


#region Sitecore - Personal Info

public static IContactPersonalInfo GetPersonalInfo(this Contact contact)
{
return contact.GetFacet<IContactPersonalInfo>("Personal"); // Key must match Sitecore's configuration
}

public static string GetFirstName(this Contact contact)
{
return (contact == null) ? String.Empty : contact.GetPersonalInfo().FirstName;
}

public static string GetSurname(this Contact contact)
{
return (contact == null) ? String.Empty : contact.GetPersonalInfo().Surname;
}

public static string GetFirstNameInitial(this Contact contact)
{
return GetInitial(contact.GetFirstName());
}

public static string GetLastNameInitial(this Contact contact)
{
return GetInitial(contact.GetFirstName());
}

public static string GetInitial(string name)
{
return string.IsNullOrWhiteSpace(name) ? String.Empty : name[0].ToString().ToUpper();
}

public static string GetInitials(this Contact contact)
{
return contact == null ? String.Empty : GetInitials(contact.GetFirstName(), contact.GetSurname());
}

public static string GetInitials(string firstName, string lastName)
{
return GetInitial(firstName) + GetInitial(lastName);
}

#endregion


#region Sitecore - Addresses

public static IContactAddresses GetAddresses(this Contact contact)
{
return contact.GetFacet<IContactAddresses>("Addresses"); // Key must match Sitecore's configuration
}

public static IAddress GetAddress(this Contact contact, string addressKey, bool createIfNotExists = false, bool makePreferred = false)
{
var addresses = contact.GetAddresses();

IAddress address = null;
if (addresses.Entries.Contains(addressKey))
{
address = addresses.Entries[addressKey];
}
else if (createIfNotExists)
{
address = addresses.Entries.Create(addressKey);
}

if (makePreferred)
{
addresses.Preferred = addressKey;
}

return address;
}

#endregion


#region Sitecore - Emails

public static IContactEmailAddresses GetEmails(this Contact contact)
{
return contact.GetFacet<IContactEmailAddresses>("Emails"); // Key must match Sitecore's configuration
}

public static IEmailAddress GetEmail(this Contact contact, string emailKey, bool createIfNotExists = false, bool makePreferred = false)
{
var emails = contact.GetEmails();

IEmailAddress email = null;
if (emails.Entries.Contains(emailKey))
{
email = emails.Entries[emailKey];
}
else if (createIfNotExists)
{
email = emails.Entries.Create(emailKey);
}

if (makePreferred)
{
emails.Preferred = emailKey;
}

return email;
}

#endregion
}
9 changes: 9 additions & 0 deletions CoreSitecore/App_Config/CoreSitecore.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
-->
<setting name="CoreSitecore.Authorization.DefaultUnauthorizedRedirectUrl" value="{FB5506F8-F8E4-4E66-AA1D-EE9B73D675BA}" />

<!--
When using the [RedirectExtranetUsers] attribute in CoreSitecore,
use this setting to control the default redirection on a request where the user is authorized and in the extranet domain
to force them somewhere else.
Can be an Item ID otherwise will be assumed to be a URL.
-->
<setting name="CoreSitecore.ActionFilters.DefaultRedirectExtranetUrl" value="{00000000-0000-1111-2222-333333333333}" />

<!--
The querystring key to use for specifying JpegCompressionLevelQueryKey.
Expand Down
14 changes: 14 additions & 0 deletions CoreSitecore/CoreSitecore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,19 @@
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Sitecore.Analytics">
<HintPath>..\CoreSitecore.Dependencies\Sitecore.Analytics.dll</HintPath>
</Reference>
<Reference Include="Sitecore.Analytics.Model">
<HintPath>..\CoreSitecore.Dependencies\Sitecore.Analytics.Model.dll</HintPath>
</Reference>
<Reference Include="Sitecore.Kernel, Version=7.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\CoreSitecore.Dependencies\Sitecore.Kernel.dll</HintPath>
</Reference>
<Reference Include="Sitecore.Social">
<HintPath>..\CoreSitecore.Dependencies\Sitecore.Social.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
Expand Down Expand Up @@ -86,7 +95,11 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Analytics\Model\Framework\IElementDictionaryExtensions.cs" />
<Compile Include="Helpers\RedirectHelper.cs" />
<Compile Include="Social\Connector\Facets\Contact\SocialProfile\IFieldElementExtensions.cs" />
<Compile Include="Caching\CacheManagerEx.cs" />
<Compile Include="Analytics\Tracking\ContactExtensions.cs" />
<Compile Include="Data\ItemPathExtensions.cs" />
<Compile Include="Data\Items\ItemAdapter.cs" />
<Compile Include="Helpers\ValidationHelper.cs" />
Expand All @@ -105,6 +118,7 @@
<Compile Include="Helpers\UrlHelper.cs" />
<Compile Include="Sys\Web\Mvc\AuthorizeByDomainAttribute.cs" />
<Compile Include="Sys\Web\Mvc\AuthorizeExtranetAttribute.cs" />
<Compile Include="Sys\Web\Mvc\RedirectExtranetUsers.cs" />
<Compile Include="Sys\Web\Mvc\ValidateFormHandler.cs" />
<Compile Include="Web\UI\DynamicKeyPlaceholder.cs" />
<Compile Include="Web\UI\LayoutBase.cs">
Expand Down
41 changes: 41 additions & 0 deletions CoreSitecore/Helpers/RedirectHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Sitecore.Configuration;
using Sitecore.Data;

namespace CoreSitecore.Helpers
{
public static class RedirectHelper
{
/// <summary>
/// Gets a redirect url from a Sitecore setting,
/// whereby the setting value can be the ID of a Sitecore item to redirect to or a url to redirect to.
/// </summary>
/// <returns></returns>
public static string GetRedirectUrlFromSetting(string redirectUrlSettingName, string defaultRedirectUrlSettingName)
{
var redirectionUrl = string.Empty;

var settingName = string.IsNullOrWhiteSpace(redirectUrlSettingName) ? defaultRedirectUrlSettingName : redirectUrlSettingName;
var settingValue = Settings.GetSetting(settingName);

if (!string.IsNullOrWhiteSpace(settingValue))
{
// If ID, lookup item by ID and figure out it's URL
if (ID.IsID(settingValue))
{
var itemToRedirectTo = Sitecore.Context.Database.GetItem(new ID(settingValue));
if (itemToRedirectTo != null)
{
redirectionUrl = itemToRedirectTo.GetItemUrl();
}
}
else
{
// Assume is url and just use that
redirectionUrl = settingValue;
}
}

return redirectionUrl;
}
}
}
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.10.0")]
[assembly: AssemblyFileVersion("8.1.10.0")]
[assembly: AssemblyVersion("8.1.11.0")]
[assembly: AssemblyFileVersion("8.1.11.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Sitecore.Social.Connector.Facets.Contact.SocialProfile;

public static class IFieldElementExtensions
{
public static string GetValueOrDefault(this IFieldElement fieldEle)
{
return fieldEle == null ? null : fieldEle.Value;
}
}
29 changes: 4 additions & 25 deletions CoreSitecore/Sys/Web/Mvc/AuthorizeByDomainAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Sitecore.Configuration;
using CoreSitecore.Helpers;
using Sitecore.Configuration;
using Sitecore.Data;
using System.Linq;
using System.Web;
Expand All @@ -21,7 +22,7 @@ public class AuthorizeByDomainAttribute : AuthorizeAttribute
/// If this setting is an Sitecore.Data.ID, the url to that item will be used.
/// Else, it will be assumed to be a URL that can be redirected to as is.
///
/// Default: CoreSitecore.AuthorizeByDomainAttribute.DefaultRedirectUrl
/// Default: "CoreSitecore.Authorization.DefaultUnauthorizedRedirectUrl"
/// </summary>
public string RedirectUrlSettingName { get; set; }

Expand Down Expand Up @@ -59,28 +60,7 @@ protected override bool AuthorizeCore(HttpContextBase httpContext)

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
var redirectionUrl = string.Empty;

var settingName = string.IsNullOrWhiteSpace(RedirectUrlSettingName) ? DefaultRedirectUrlSettingName : RedirectUrlSettingName;
var settingValue = Settings.GetSetting(settingName);

if (!string.IsNullOrWhiteSpace(settingValue))
{
// If ID, lookup item by ID and figure out it's URL
if (ID.IsID(settingValue))
{
var itemToRedirectTo = Sitecore.Context.Database.GetItem(new ID(settingValue));
if (itemToRedirectTo != null)
{
redirectionUrl = itemToRedirectTo.GetItemUrl();
}
}
else
{
// Assume is url and just use that
redirectionUrl = settingValue;
}
}
var redirectionUrl = RedirectHelper.GetRedirectUrlFromSetting(RedirectUrlSettingName, DefaultRedirectUrlSettingName);

if (!string.IsNullOrWhiteSpace(redirectionUrl))
{
Expand All @@ -95,7 +75,6 @@ protected override void HandleUnauthorizedRequest(AuthorizationContext filterCon
}
else
{
// Note: This seems to just stop the view from rendering, not actually do a redirect to loginUrl in Sitecore's implementation.
base.HandleUnauthorizedRequest(filterContext);
}
}
Expand Down
40 changes: 40 additions & 0 deletions CoreSitecore/Sys/Web/Mvc/RedirectExtranetUsers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using CoreSitecore.Helpers;
using System.Web.Mvc;

namespace CoreSitecore.Sys.Web.Mvc
{
/// <summary>
/// An action filter attribute that checks if a user is authenticated and authenticated under the extranet domain.
/// If so, it will enforce a redirection to a configured url/item.
/// </summary>
public class RedirectExtranetUsers : ActionFilterAttribute
{
private const string DefaultRedirectUrlSettingName = "CoreSitecore.ActionFilters.DefaultRedirectExtranetUrl";

/// <summary>
/// Extranet users (i.e. IsAuthenticated and under extranet domain) will be redirected to a url found by inspecting this setting.
///
/// If this setting is an Sitecore.Data.ID, the url to that item will be used.
/// Else, it will be assumed to be a URL that can be redirected to as is.
///
/// Default: "CoreSitecore.ActionFilters.DefaultRedirectExtranetUrl"
/// </summary>
public string RedirectUrlSettingName { get; set; }

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (Sitecore.Context.User.IsAuthenticated && Sitecore.Context.User.GetDomainName() == "extranet")
{
var redirectionUrl = RedirectHelper.GetRedirectUrlFromSetting(RedirectUrlSettingName, DefaultRedirectUrlSettingName);

if (!string.IsNullOrWhiteSpace(redirectionUrl))
{
filterContext.Result = new RedirectResult(redirectionUrl);
return;
}
}

base.OnActionExecuting(filterContext);
}
}
}

0 comments on commit 6a86dc8

Please sign in to comment.