Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Sitecore;
- using Sitecore.Caching;
- using Sitecore.Caching.Generics;
- using Sitecore.Caching.UserProfile;
- using Sitecore.Collections;
- using Sitecore.Common;
- using Sitecore.Configuration;
- using Sitecore.Data;
- using Sitecore.Data.DataProviders;
- using Sitecore.Data.Fields;
- using Sitecore.Data.Items;
- using Sitecore.Diagnostics;
- using Sitecore.Events;
- using Sitecore.Globalization;
- using Sitecore.Security.Accounts;
- using Sitecore.Security.Domains;
- using Sitecore.SecurityModel;
- using Sitecore.StringExtensions;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.Configuration;
- using System.Globalization;
- using System.Linq;
- using System.Reflection;
- using System.Runtime.CompilerServices;
- using System.Web.Profile;
- using System.Web.Security;
- using Context=Sitecore.Context;
- namespace UserProfile
- {
- /// <summary>
- /// Profile class
- /// </summary>
- public class UserProfile : Sitecore.Security.UserProfile
- {
- /// <summary>Lock object
- /// </summary>
- private readonly object propertiesLock = new object();
- /// <summary>User profile Comments
- /// </summary>
- private string comment;
- /// <summary>Custom properties collection
- /// </summary>
- private Dictionary<string, string> customProperties;
- /// <summary> Indicates wherever customProperties were changed
- /// </summary>
- private bool customPropertiesIsDirty;
- /// <summary>
- /// The email.
- /// </summary>
- private string email;
- /// <summary>Indicates wherever email was changed
- /// </summary>
- private bool emailChanged;
- /// <summary>
- /// The inner membership user.
- /// </summary>
- private Reference<MembershipUser> innerMembershipUser;
- /// <summary>
- /// The profile user.
- /// </summary>
- private User profileUser;
- /// <summary>
- /// Gets or sets the assignable roles.
- /// </summary>
- /// <value>The assignable roles.</value>
- [Obsolete]
- public override string AssignableRoles
- {
- get
- {
- return this.GetPropertyValueCore("AssignableRoles") as string;
- }
- set
- {
- this.SetPropertyValueCore("AssignableRoles", value);
- }
- }
- /// <summary>
- /// Gets or sets the badges.
- /// </summary>
- /// <value>
- /// The badges.
- /// </value>
- public virtual string Badges
- {
- get
- {
- return StringUtil.GetString(this.GetPropertyValueCore("Badges"));
- }
- set
- {
- Assert.ArgumentNotNull(value, "value");
- this.SetPropertyValueCore("Badges", value);
- }
- }
- /// <summary>
- /// Gets the cache.
- /// </summary>
- /// <value>The cache.</value>
- private UserProfileCache Cache
- {
- get
- {
- UserProfileCache userProfileCache = CacheManager.GetUserProfileCache();
- Assert.IsNotNull(userProfileCache, "profileCache");
- return userProfileCache;
- }
- }
- /// <summary>
- /// Gets or sets the comment.
- /// </summary>
- /// <value>The comment.</value>
- public virtual string Comment
- {
- get
- {
- string comment = this.comment;
- if (comment != null)
- {
- return comment;
- }
- MembershipUser user = Membership.GetUser(base.UserName);
- if (user == null)
- {
- return null;
- }
- comment = user.Comment;
- this.comment = comment;
- return comment;
- }
- set
- {
- Assert.ArgumentNotNull(value, "value");
- MembershipUser user = Membership.GetUser(base.UserName);
- Type type = typeof(MembershipUser);
- object[] userName = new object[] { base.UserName };
- Assert.IsNotNull(user, type, "Membership user \"{0}\" not found", userName);
- user.Comment = value;
- Membership.UpdateUser(user);
- this.comment = value;
- }
- }
- /// <summary>
- /// Gets or sets the current position.
- /// </summary>
- /// <value>
- /// The current position.
- /// </value>
- public virtual string CurrentPosition
- {
- get
- {
- return StringUtil.GetString(this.GetPropertyValueCore("CurrentPosition"));
- }
- set
- {
- Assert.ArgumentNotNull(value, "value");
- this.SetPropertyValueCore("CurrentPosition", value);
- }
- }
- /// <summary>
- /// Gets the custom properties.
- /// </summary>
- /// <value>The custom properties.</value>
- private Dictionary<string, string> CustomProperties
- {
- get
- {
- Dictionary<string, string> strs;
- Dictionary<string, string> serializedData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
- serializedData = this.customProperties;
- if (serializedData != null)
- {
- return serializedData;
- }
- lock (this.propertiesLock)
- {
- serializedData = this.SerializedData as Dictionary<string, string>;
- if (serializedData == null)
- {
- NameValueCollection nameValueCollection = this.SerializedData as NameValueCollection;
- serializedData = (nameValueCollection == null ? new Dictionary<string, string>() : nameValueCollection.AllKeys.ToDictionary<string, string, string>((string key) => key, (string key) => nameValueCollection[key]));
- this.customProperties = serializedData;
- if (!base.IsAnonymous)
- {
- this.SerializedData = serializedData;
- }
- strs = serializedData;
- }
- else
- {
- this.customProperties = serializedData;
- strs = serializedData;
- }
- }
- return strs;
- }
- }
- /// <summary>
- /// Gets or sets the engagement value.
- /// </summary>
- /// <value>
- /// The engagement value.
- /// </value>
- public virtual string EngagementValue
- {
- get
- {
- return StringUtil.GetString(this.GetPropertyValueCore("EngagementValue"));
- }
- set
- {
- Assert.ArgumentNotNull(value, "value");
- this.SetPropertyValueCore("EngagementValue", value);
- }
- }
- /// <summary>
- /// Gets or sets a profile property value indexed by the property name.
- /// </summary>
- /// <param name="propertyName">The name of the profile property.</param>
- /// <returns>
- /// The value of the specified profile property, typed as object.
- /// </returns>
- /// <exception cref="T:System.Configuration.SettingsPropertyNotFoundException">There are no properties defined for the current profile.- or -The specified profile property name does not exist in the current profile.- or -The provider for the specified profile property did not recognize the specified property.</exception>
- /// <exception cref="T:System.Configuration.Provider.ProviderException">An attempt was made to set a property value on an anonymous profile where the property's allowAnonymous attribute is false.</exception>
- /// <exception cref="T:System.Configuration.SettingsPropertyIsReadOnlyException">An attempt was made to set a property value that was marked as read-only.</exception>
- /// <exception cref="T:System.Configuration.SettingsPropertyWrongTypeException">An attempt was made to assign a value to a property using an incompatible type.</exception>
- public new virtual string this[string propertyName]
- {
- get
- {
- if (!UserProfile.IsCoreProperty(propertyName))
- {
- return this.GetCustomProperty(propertyName);
- }
- object propertyValueCore = this.GetPropertyValueCore(propertyName);
- if (propertyValueCore == null)
- {
- return string.Empty;
- }
- return propertyValueCore.ToString();
- }
- set
- {
- if (UserProfile.IsCoreProperty(propertyName))
- {
- this.SetPropertyValueCore(propertyName, value);
- return;
- }
- this.SetCustomProperty(propertyName, value);
- }
- }
- /// <summary>
- /// Gets the membership user.
- /// </summary>
- internal MembershipUser MembershipUser
- {
- get
- {
- if (this.innerMembershipUser == null)
- {
- this.innerMembershipUser = new Reference<MembershipUser>(this.GetUser());
- }
- return this.innerMembershipUser.Value;
- }
- }
- /// <summary>
- /// Gets or sets the notifications.
- /// </summary>
- /// <value>
- /// The notifications.
- /// </value>
- public virtual string Notifications
- {
- get
- {
- return StringUtil.GetString(this.GetPropertyValueCore("Notifications"));
- }
- set
- {
- Assert.ArgumentNotNull(value, "value");
- this.SetPropertyValueCore("Notifications", value);
- }
- }
- /// <summary>
- /// Gets or sets the user.
- /// </summary>
- /// <value>
- /// The user.
- /// </value>
- public virtual User ProfileUser
- {
- get
- {
- if (this.profileUser == null && Membership.GetUser(base.UserName) != null)
- {
- this.profileUser = User.FromName(base.UserName, false);
- }
- return this.profileUser;
- }
- set
- {
- this.profileUser = value;
- }
- }
- /// <summary>
- /// Gets the state.
- /// </summary>
- /// <value>
- /// The state.
- /// </value>
- public override string State
- {
- get
- {
- MembershipUser membershipUser = this.MembershipUser;
- if (membershipUser == null)
- {
- return string.Empty;
- }
- string empty = string.Empty;
- if (!membershipUser.IsApproved)
- {
- if (!string.IsNullOrEmpty(empty))
- {
- empty = string.Concat(empty, ", ");
- }
- empty = string.Concat(empty, Translate.Text("Disabled"));
- }
- if (membershipUser.IsLockedOut)
- {
- if (!string.IsNullOrEmpty(empty))
- {
- empty = string.Concat(empty, ", ");
- }
- empty = string.Concat(empty, Translate.Text("Locked Out"));
- }
- return empty;
- }
- }
- public UserProfile()
- {
- }
- /// <summary>
- /// Gets a custom property.
- /// </summary>
- /// <param name="propertyName">Name of the property.</param>
- /// <returns>
- /// Custom property value
- /// </returns>
- public override string GetCustomProperty(string propertyName)
- {
- string str;
- Assert.ArgumentNotNull(propertyName, "propertyName");
- this.CustomProperties.TryGetValue(propertyName, out str);
- if (str != null)
- {
- return str;
- }
- return this.GetPropertyValueFromProfileItem(propertyName);
- }
- /// <summary>
- /// Gets the keys of all custom properties.
- /// </summary>
- /// <returns>
- /// List of custom properties names
- /// </returns>
- public override List<string> GetCustomPropertyNames()
- {
- List<string> strs = new List<string>();
- Dictionary<string, string> customProperties = this.CustomProperties;
- lock (customProperties)
- {
- foreach (string key in customProperties.Keys)
- {
- strs.Add(key);
- }
- }
- return strs;
- }
- /// <summary>
- /// Gets the profile item.
- /// </summary>
- /// <returns>
- /// Profile item
- /// </returns>
- protected override Item GetProfileItem()
- {
- ID d;
- Item item;
- string profileItemId = this.ProfileItemId;
- if (string.IsNullOrEmpty(profileItemId))
- {
- return null;
- }
- if (!ID.TryParse(profileItemId, out d))
- {
- return null;
- }
- using (SecurityDisabler securityDisabler = new SecurityDisabler())
- {
- Database database = Factory.GetDatabase(Settings.ProfileItemDatabase, false);
- if (database != null)
- {
- item = database.GetItem(d);
- }
- else
- {
- object[] profileItemDatabase = new object[] { Settings.ProfileItemDatabase };
- Log.SingleWarn("Cannot retrieve user profile item. Profile item database '{0}' was not found".FormatWith(profileItemDatabase), this);
- item = null;
- }
- }
- return item;
- }
- /// <summary>
- /// Gets a named property value.
- /// </summary>
- /// <param name="propertyName">
- /// Name of the property.
- /// </param>
- /// <returns>
- /// The value.
- /// </returns>
- protected override object GetPropertyValueCore(string propertyName)
- {
- object propertyValue;
- Assert.ArgumentNotNullOrEmpty(propertyName, "name");
- UserProfileCacheRecord record = this.Cache.GetRecord(base.UserName, propertyName);
- if (record != null)
- {
- return record.Value;
- }
- try
- {
- propertyValue = base.GetPropertyValue(propertyName);
- }
- catch
- {
- propertyValue = null;
- }
- if ((propertyValue == null || propertyValue is string && propertyValue.ToString() == string.Empty) && !propertyName.Equals("ProfileItemId", StringComparison.OrdinalIgnoreCase))
- {
- propertyValue = this.GetPropertyValueFromProfileItem(propertyName);
- }
- this.Cache.AddRecord(base.UserName, propertyName, propertyValue);
- return propertyValue;
- }
- /// <summary>
- /// Gets the property value from profile item.
- /// </summary>
- /// <param name="propertyName">Name of the property.</param>
- /// <returns>
- /// Value from Profile item
- /// </returns>
- private string GetPropertyValueFromProfileItem(string propertyName)
- {
- Assert.ArgumentNotNull(propertyName, "propertyName");
- Item profileItem = this.GetProfileItem();
- if (profileItem == null)
- {
- return string.Empty;
- }
- Field item = profileItem.Fields[propertyName];
- if (item == null)
- {
- return string.Empty;
- }
- return item.Value;
- }
- /// <summary>
- /// The get user.
- /// </summary>
- /// <returns>
- /// Returns Membership user
- /// </returns>
- private MembershipUser GetUser()
- {
- MembershipUser user = null;
- if (!base.UserName.Contains(","))
- {
- user = Membership.GetUser(base.UserName);
- }
- return user;
- }
- /// <summary>
- /// Indicates wherever property is the code one
- /// </summary>
- /// <param name="propertyName">The property name.</param>
- /// <returns>
- /// Returns true if property should be stored in code, false otherwise
- /// </returns>
- private static bool IsCoreProperty(string propertyName)
- {
- bool flag;
- Assert.ArgumentNotNull(propertyName, "propertyName");
- IEnumerator enumerator = ProfileBase.Properties.GetEnumerator();
- try
- {
- while (enumerator.MoveNext())
- {
- SettingsProperty current = (SettingsProperty)enumerator.Current;
- if (string.Compare(current.Name, propertyName, StringComparison.InvariantCultureIgnoreCase) != 0)
- {
- continue;
- }
- flag = true;
- return flag;
- }
- return false;
- }
- finally
- {
- IDisposable disposable = enumerator as IDisposable;
- if (disposable != null)
- {
- disposable.Dispose();
- }
- }
- return flag;
- }
- /// <summary>
- /// The raise user updated event.
- /// </summary>
- private void RaiseUserUpdatedEvent()
- {
- MembershipUser user = Membership.GetUser(base.UserName);
- if (user != null)
- {
- object[] objArray = new object[] { user };
- Event.RaiseEvent("user:updated", objArray);
- }
- }
- /// <summary>
- /// Reloads all profile data for the user.
- /// </summary>
- public override void Reload()
- {
- this.Cache.RemoveUser(base.UserName);
- this.customProperties = null;
- this.customPropertiesIsDirty = false;
- this.innerMembershipUser = null;
- }
- /// <summary>
- /// Removes a named custom property.
- /// </summary>
- /// <param name="propertyName">
- /// The property name.
- /// </param>
- public override void RemoveCustomProperty(string propertyName)
- {
- Assert.ArgumentNotNullOrEmpty(propertyName, "propertyName");
- Dictionary<string, string> customProperties = this.CustomProperties;
- lock (customProperties)
- {
- customProperties.Remove(propertyName);
- }
- this.customPropertiesIsDirty = true;
- }
- /// <summary>
- /// Updates the profile data source with changed profile property values.
- /// </summary>
- public override void Save()
- {
- this.innerMembershipUser = null;
- this.SaveUserProperties();
- this.SerializeCustomProperties();
- base.Save();
- this.RaiseUserUpdatedEvent();
- }
- /// <summary>
- /// Saves the user properties.
- /// </summary>
- private void SaveUserProperties()
- {
- if (!this.emailChanged)
- {
- return;
- }
- MembershipUser user = Membership.GetUser(base.UserName);
- if (user == null)
- {
- return;
- }
- user.Email = this.email;
- Membership.UpdateUser(user);
- }
- /// <summary>
- /// Serializes the custom properties.
- /// </summary>
- private void SerializeCustomProperties()
- {
- if (!this.customPropertiesIsDirty)
- {
- return;
- }
- this.SerializedData = this.CustomProperties;
- }
- /// <summary>
- /// Sets the value of a custom property.
- /// </summary>
- /// <param name="propertyName">Name of the property.</param>
- /// <param name="value">The value.</param>
- public override void SetCustomProperty(string propertyName, string value)
- {
- Assert.ArgumentNotNull(propertyName, "propertyName");
- Assert.ArgumentNotNull(value, "value");
- Dictionary<string, string> customProperties = this.CustomProperties;
- lock (customProperties)
- {
- customProperties[propertyName] = value;
- }
- this.customPropertiesIsDirty = true;
- }
- /// <summary>
- /// Sets a named property value.
- /// </summary>
- /// <param name="propertyName">
- /// Name of the property.
- /// </param>
- /// <param name="value">
- /// The value.
- /// </param>
- protected override void SetPropertyValueCore(string propertyName, object value)
- {
- Assert.ArgumentNotNullOrEmpty(propertyName, "name");
- base.SetPropertyValue(propertyName, value);
- this.SetPropertyValueToCache(propertyName, value);
- }
- /// <summary>
- /// Sets the property value to cache.
- /// </summary>
- /// <param name="propertyName">
- /// Name of the property.
- /// </param>
- /// <param name="propertyValue">
- /// The property value.
- /// </param>
- private void SetPropertyValueToCache(string propertyName, object propertyValue)
- {
- Assert.IsNotNullOrEmpty(propertyName, "propertyName");
- UserProfileCache userProfileCache = CacheManager.GetUserProfileCache();
- Assert.IsNotNull(userProfileCache, "profileCache");
- userProfileCache.AddRecord(base.UserName, propertyName, propertyValue);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment