Skip to content

Instantly share code, notes, and snippets.

@rollsch
Created December 5, 2017 02:21
Show Gist options
  • Save rollsch/e3af826b8f5b082683b18c0bf56b1824 to your computer and use it in GitHub Desktop.
Save rollsch/e3af826b8f5b082683b18c0bf56b1824 to your computer and use it in GitHub Desktop.
/// <summary>
/// Gets or the current customer if they are registered, if not registered it returns null
/// </summary>
[CanBeNull]
public virtual Customer CurrentRegisteredCustomer
{
get
{
//whether there is a cached value
if (_cachedCustomer != null)
return _cachedCustomer;
Customer customer = null;
if (customer == null || customer.Deleted || !customer.Active || customer.RequireReLogin)
{
//try to get registered user
customer = _authenticationService.GetAuthenticatedCustomer();
}
if (customer != null && !customer.Deleted && customer.Active && !customer.RequireReLogin)
{
//get impersonate user if required
var impersonatedCustomerId = customer.GetAttribute<int?>(SystemCustomerAttributeNames.ImpersonatedCustomerId);
if (impersonatedCustomerId.HasValue && impersonatedCustomerId.Value > 0)
{
var impersonatedCustomer = _customerService.GetCustomerById(impersonatedCustomerId.Value);
if (impersonatedCustomer != null && !impersonatedCustomer.Deleted && impersonatedCustomer.Active && !impersonatedCustomer.RequireReLogin)
{
//set impersonated customer
_originalCustomerIfImpersonated = customer;
customer = impersonatedCustomer;
}
}
}
if (customer == null || customer.Deleted || !customer.Active || customer.RequireReLogin)
{
return null;
}
if (!customer.Deleted && customer.Active && !customer.RequireReLogin)
{
//set customer cookie
SetCustomerCookie(customer.CustomerGuid);
//cache the found customer
_cachedCustomer = customer;
}
return _cachedCustomer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment