michaelfayez

UserProfile

Dec 13th, 2016
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.71 KB | None | 0 0
  1. using Sitecore;
  2. using Sitecore.Caching;
  3. using Sitecore.Caching.Generics;
  4. using Sitecore.Caching.UserProfile;
  5. using Sitecore.Collections;
  6. using Sitecore.Common;
  7. using Sitecore.Configuration;
  8. using Sitecore.Data;
  9. using Sitecore.Data.DataProviders;
  10. using Sitecore.Data.Fields;
  11. using Sitecore.Data.Items;
  12. using Sitecore.Diagnostics;
  13. using Sitecore.Events;
  14. using Sitecore.Globalization;
  15. using Sitecore.Security.Accounts;
  16. using Sitecore.Security.Domains;
  17. using Sitecore.SecurityModel;
  18. using Sitecore.StringExtensions;
  19. using System;
  20. using System.Collections;
  21. using System.Collections.Generic;
  22. using System.Collections.Specialized;
  23. using System.Configuration;
  24. using System.Globalization;
  25. using System.Linq;
  26. using System.Reflection;
  27. using System.Runtime.CompilerServices;
  28. using System.Web.Profile;
  29. using System.Web.Security;
  30. using Context=Sitecore.Context;
  31.  
  32. namespace UserProfile
  33. {
  34.     /// <summary>
  35.     /// Profile class
  36.     /// </summary>
  37.     public class UserProfile : Sitecore.Security.UserProfile
  38.     {
  39.         /// <summary>Lock object
  40.         /// </summary>
  41.         private readonly object propertiesLock = new object();
  42.  
  43.         /// <summary>User profile Comments
  44.         /// </summary>
  45.         private string comment;
  46.  
  47.         /// <summary>Custom properties collection
  48.         /// </summary>
  49.         private Dictionary<string, string> customProperties;
  50.  
  51.         /// <summary> Indicates wherever customProperties were changed
  52.         /// </summary>
  53.         private bool customPropertiesIsDirty;
  54.  
  55.         /// <summary>
  56.         /// The email.
  57.         /// </summary>
  58.         private string email;
  59.  
  60.         /// <summary>Indicates wherever email was changed
  61.         /// </summary>
  62.         private bool emailChanged;
  63.  
  64.         /// <summary>
  65.         /// The inner membership user.
  66.         /// </summary>
  67.         private Reference<MembershipUser> innerMembershipUser;
  68.  
  69.         /// <summary>
  70.         /// The profile user.
  71.         /// </summary>
  72.         private User profileUser;
  73.  
  74.         /// <summary>
  75.         /// Gets or sets the assignable roles.
  76.         /// </summary>
  77.         /// <value>The assignable roles.</value>
  78.         [Obsolete]
  79.         public override string AssignableRoles
  80.         {
  81.             get
  82.             {
  83.                 return this.GetPropertyValueCore("AssignableRoles") as string;
  84.             }
  85.             set
  86.             {
  87.                 this.SetPropertyValueCore("AssignableRoles", value);
  88.             }
  89.         }
  90.  
  91.         /// <summary>
  92.         /// Gets or sets the badges.
  93.         /// </summary>
  94.         /// <value>
  95.         /// The badges.
  96.         /// </value>
  97.         public virtual string Badges
  98.         {
  99.             get
  100.             {
  101.                 return StringUtil.GetString(this.GetPropertyValueCore("Badges"));
  102.             }
  103.             set
  104.             {
  105.                 Assert.ArgumentNotNull(value, "value");
  106.                 this.SetPropertyValueCore("Badges", value);
  107.             }
  108.         }
  109.  
  110.         /// <summary>
  111.         /// Gets the cache.
  112.         /// </summary>
  113.         /// <value>The cache.</value>
  114.         private UserProfileCache Cache
  115.         {
  116.             get
  117.             {
  118.                 UserProfileCache userProfileCache = CacheManager.GetUserProfileCache();
  119.                 Assert.IsNotNull(userProfileCache, "profileCache");
  120.                 return userProfileCache;
  121.             }
  122.         }
  123.  
  124.  
  125.         /// <summary>
  126.         /// Gets or sets the comment.
  127.         /// </summary>
  128.         /// <value>The comment.</value>
  129.         public virtual string Comment
  130.         {
  131.             get
  132.             {
  133.                 string comment = this.comment;
  134.                 if (comment != null)
  135.                 {
  136.                     return comment;
  137.                 }
  138.                 MembershipUser user = Membership.GetUser(base.UserName);
  139.                 if (user == null)
  140.                 {
  141.                     return null;
  142.                 }
  143.                 comment = user.Comment;
  144.                 this.comment = comment;
  145.                 return comment;
  146.             }
  147.             set
  148.             {
  149.                 Assert.ArgumentNotNull(value, "value");
  150.                 MembershipUser user = Membership.GetUser(base.UserName);
  151.                 Type type = typeof(MembershipUser);
  152.                 object[] userName = new object[] { base.UserName };
  153.                 Assert.IsNotNull(user, type, "Membership user \"{0}\" not found", userName);
  154.                 user.Comment = value;
  155.                 Membership.UpdateUser(user);
  156.                 this.comment = value;
  157.             }
  158.         }
  159.  
  160.  
  161.      
  162.  
  163.         /// <summary>
  164.         /// Gets or sets the current position.
  165.         /// </summary>
  166.         /// <value>
  167.         /// The current position.
  168.         /// </value>
  169.         public virtual string CurrentPosition
  170.         {
  171.             get
  172.             {
  173.                 return StringUtil.GetString(this.GetPropertyValueCore("CurrentPosition"));
  174.             }
  175.             set
  176.             {
  177.                 Assert.ArgumentNotNull(value, "value");
  178.                 this.SetPropertyValueCore("CurrentPosition", value);
  179.             }
  180.         }
  181.  
  182.         /// <summary>
  183.         /// Gets the custom properties.
  184.         /// </summary>
  185.         /// <value>The custom properties.</value>
  186.         private Dictionary<string, string> CustomProperties
  187.         {
  188.             get
  189.             {
  190.                 Dictionary<string, string> strs;
  191.                 Dictionary<string, string> serializedData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  192.                 serializedData = this.customProperties;
  193.                 if (serializedData != null)
  194.                 {
  195.                     return serializedData;
  196.                 }
  197.                 lock (this.propertiesLock)
  198.                 {
  199.                     serializedData = this.SerializedData as Dictionary<string, string>;
  200.                     if (serializedData == null)
  201.                     {
  202.                         NameValueCollection nameValueCollection = this.SerializedData as NameValueCollection;
  203.                         serializedData = (nameValueCollection == null ? new Dictionary<string, string>() : nameValueCollection.AllKeys.ToDictionary<string, string, string>((string key) => key, (string key) => nameValueCollection[key]));
  204.                         this.customProperties = serializedData;
  205.                         if (!base.IsAnonymous)
  206.                         {
  207.                             this.SerializedData = serializedData;
  208.                         }
  209.                         strs = serializedData;
  210.                     }
  211.                     else
  212.                     {
  213.                         this.customProperties = serializedData;
  214.                         strs = serializedData;
  215.                     }
  216.                 }
  217.                 return strs;
  218.             }
  219.         }
  220.  
  221.          
  222.  
  223.         /// <summary>
  224.         /// Gets or sets the engagement value.
  225.         /// </summary>
  226.         /// <value>
  227.         /// The engagement value.
  228.         /// </value>
  229.         public virtual string EngagementValue
  230.         {
  231.             get
  232.             {
  233.                 return StringUtil.GetString(this.GetPropertyValueCore("EngagementValue"));
  234.             }
  235.             set
  236.             {
  237.                 Assert.ArgumentNotNull(value, "value");
  238.                 this.SetPropertyValueCore("EngagementValue", value);
  239.             }
  240.         }
  241.  
  242.      
  243.  
  244.          
  245.  
  246.          
  247.  
  248.         /// <summary>
  249.         /// Gets or sets a profile property value indexed by the property name.
  250.         /// </summary>
  251.         /// <param name="propertyName">The name of the profile property.</param>
  252.         /// <returns>
  253.         /// The value of the specified profile property, typed as object.
  254.         /// </returns>
  255.         /// <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>
  256.         /// <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>
  257.         /// <exception cref="T:System.Configuration.SettingsPropertyIsReadOnlyException">An attempt was made to set a property value that was marked as read-only.</exception>
  258.         /// <exception cref="T:System.Configuration.SettingsPropertyWrongTypeException">An attempt was made to assign a value to a property using an incompatible type.</exception>
  259.         public new virtual string this[string propertyName]
  260.         {
  261.             get
  262.             {
  263.                 if (!UserProfile.IsCoreProperty(propertyName))
  264.                 {
  265.                     return this.GetCustomProperty(propertyName);
  266.                 }
  267.                 object propertyValueCore = this.GetPropertyValueCore(propertyName);
  268.                 if (propertyValueCore == null)
  269.                 {
  270.                     return string.Empty;
  271.                 }
  272.                 return propertyValueCore.ToString();
  273.             }
  274.             set
  275.             {
  276.                 if (UserProfile.IsCoreProperty(propertyName))
  277.                 {
  278.                     this.SetPropertyValueCore(propertyName, value);
  279.                     return;
  280.                 }
  281.                 this.SetCustomProperty(propertyName, value);
  282.             }
  283.         }
  284.  
  285.      
  286.  
  287.          
  288.  
  289.         /// <summary>
  290.         /// Gets the membership user.
  291.         /// </summary>
  292.         internal MembershipUser MembershipUser
  293.         {
  294.             get
  295.             {
  296.                 if (this.innerMembershipUser == null)
  297.                 {
  298.                     this.innerMembershipUser = new Reference<MembershipUser>(this.GetUser());
  299.                 }
  300.                 return this.innerMembershipUser.Value;
  301.             }
  302.         }
  303.  
  304.          
  305.  
  306.         /// <summary>
  307.         /// Gets or sets the notifications.
  308.         /// </summary>
  309.         /// <value>
  310.         /// The notifications.
  311.         /// </value>
  312.         public virtual string Notifications
  313.         {
  314.             get
  315.             {
  316.                 return StringUtil.GetString(this.GetPropertyValueCore("Notifications"));
  317.             }
  318.             set
  319.             {
  320.                 Assert.ArgumentNotNull(value, "value");
  321.                 this.SetPropertyValueCore("Notifications", value);
  322.             }
  323.         }
  324.  
  325.        
  326.          
  327.  
  328.         /// <summary>
  329.         /// Gets or sets the user.
  330.         /// </summary>
  331.         /// <value>
  332.         /// The user.
  333.         /// </value>
  334.         public virtual User ProfileUser
  335.         {
  336.             get
  337.             {
  338.                 if (this.profileUser == null && Membership.GetUser(base.UserName) != null)
  339.                 {
  340.                     this.profileUser = User.FromName(base.UserName, false);
  341.                 }
  342.                 return this.profileUser;
  343.             }
  344.             set
  345.             {
  346.                 this.profileUser = value;
  347.             }
  348.         }
  349.  
  350.          
  351.  
  352.          
  353.  
  354.         /// <summary>
  355.         /// Gets the state.
  356.         /// </summary>
  357.         /// <value>
  358.         /// The state.
  359.         /// </value>
  360.         public override string State
  361.         {
  362.             get
  363.             {
  364.                 MembershipUser membershipUser = this.MembershipUser;
  365.                 if (membershipUser == null)
  366.                 {
  367.                     return string.Empty;
  368.                 }
  369.                 string empty = string.Empty;
  370.                 if (!membershipUser.IsApproved)
  371.                 {
  372.                     if (!string.IsNullOrEmpty(empty))
  373.                     {
  374.                         empty = string.Concat(empty, ", ");
  375.                     }
  376.                     empty = string.Concat(empty, Translate.Text("Disabled"));
  377.                 }
  378.                 if (membershipUser.IsLockedOut)
  379.                 {
  380.                     if (!string.IsNullOrEmpty(empty))
  381.                     {
  382.                         empty = string.Concat(empty, ", ");
  383.                     }
  384.                     empty = string.Concat(empty, Translate.Text("Locked Out"));
  385.                 }
  386.                 return empty;
  387.             }
  388.         }
  389.  
  390.         public UserProfile()
  391.         {
  392.         }
  393.  
  394.      
  395.  
  396.         /// <summary>
  397.         /// Gets a custom property.
  398.         /// </summary>
  399.         /// <param name="propertyName">Name of the property.</param>
  400.         /// <returns>
  401.         /// Custom property value
  402.         /// </returns>
  403.         public override string GetCustomProperty(string propertyName)
  404.         {
  405.             string str;
  406.             Assert.ArgumentNotNull(propertyName, "propertyName");
  407.             this.CustomProperties.TryGetValue(propertyName, out str);
  408.             if (str != null)
  409.             {
  410.                 return str;
  411.             }
  412.             return this.GetPropertyValueFromProfileItem(propertyName);
  413.         }
  414.  
  415.         /// <summary>
  416.         /// Gets the keys of all custom properties.
  417.         /// </summary>
  418.         /// <returns>
  419.         /// List of custom properties names
  420.         /// </returns>
  421.         public override List<string> GetCustomPropertyNames()
  422.         {
  423.             List<string> strs = new List<string>();
  424.             Dictionary<string, string> customProperties = this.CustomProperties;
  425.             lock (customProperties)
  426.             {
  427.                 foreach (string key in customProperties.Keys)
  428.                 {
  429.                     strs.Add(key);
  430.                 }
  431.             }
  432.             return strs;
  433.         }
  434.  
  435.         /// <summary>
  436.         /// Gets the profile item.
  437.         /// </summary>
  438.         /// <returns>
  439.         /// Profile item
  440.         /// </returns>
  441.         protected override Item GetProfileItem()
  442.         {
  443.             ID d;
  444.             Item item;
  445.             string profileItemId = this.ProfileItemId;
  446.             if (string.IsNullOrEmpty(profileItemId))
  447.             {
  448.                 return null;
  449.             }
  450.             if (!ID.TryParse(profileItemId, out d))
  451.             {
  452.                 return null;
  453.             }
  454.             using (SecurityDisabler securityDisabler = new SecurityDisabler())
  455.             {
  456.                 Database database = Factory.GetDatabase(Settings.ProfileItemDatabase, false);
  457.                 if (database != null)
  458.                 {
  459.                     item = database.GetItem(d);
  460.                 }
  461.                 else
  462.                 {
  463.                     object[] profileItemDatabase = new object[] { Settings.ProfileItemDatabase };
  464.                     Log.SingleWarn("Cannot retrieve user profile item. Profile item database '{0}' was not found".FormatWith(profileItemDatabase), this);
  465.                     item = null;
  466.                 }
  467.             }
  468.             return item;
  469.         }
  470.  
  471.         /// <summary>
  472.         /// Gets a named property value.
  473.         /// </summary>
  474.         /// <param name="propertyName">
  475.         /// Name of the property.
  476.         /// </param>
  477.         /// <returns>
  478.         /// The value.
  479.         /// </returns>
  480.         protected override object GetPropertyValueCore(string propertyName)
  481.         {
  482.             object propertyValue;
  483.             Assert.ArgumentNotNullOrEmpty(propertyName, "name");
  484.             UserProfileCacheRecord record = this.Cache.GetRecord(base.UserName, propertyName);
  485.             if (record != null)
  486.             {
  487.                 return record.Value;
  488.             }
  489.             try
  490.             {
  491.                 propertyValue = base.GetPropertyValue(propertyName);
  492.             }
  493.             catch
  494.             {
  495.                 propertyValue = null;
  496.             }
  497.             if ((propertyValue == null || propertyValue is string && propertyValue.ToString() == string.Empty) && !propertyName.Equals("ProfileItemId", StringComparison.OrdinalIgnoreCase))
  498.             {
  499.                 propertyValue = this.GetPropertyValueFromProfileItem(propertyName);
  500.             }
  501.             this.Cache.AddRecord(base.UserName, propertyName, propertyValue);
  502.             return propertyValue;
  503.         }
  504.  
  505.         /// <summary>
  506.         /// Gets the property value from profile item.
  507.         /// </summary>
  508.         /// <param name="propertyName">Name of the property.</param>
  509.         /// <returns>
  510.         /// Value from Profile item
  511.         /// </returns>
  512.         private string GetPropertyValueFromProfileItem(string propertyName)
  513.         {
  514.             Assert.ArgumentNotNull(propertyName, "propertyName");
  515.             Item profileItem = this.GetProfileItem();
  516.             if (profileItem == null)
  517.             {
  518.                 return string.Empty;
  519.             }
  520.             Field item = profileItem.Fields[propertyName];
  521.             if (item == null)
  522.             {
  523.                 return string.Empty;
  524.             }
  525.             return item.Value;
  526.         }
  527.  
  528.         /// <summary>
  529.         /// The get user.
  530.         /// </summary>
  531.         /// <returns>
  532.         /// Returns Membership user
  533.         /// </returns>
  534.         private MembershipUser GetUser()
  535.         {
  536.             MembershipUser user = null;
  537.             if (!base.UserName.Contains(","))
  538.             {
  539.                 user = Membership.GetUser(base.UserName);
  540.             }
  541.             return user;
  542.         }
  543.  
  544.         /// <summary>
  545.         /// Indicates wherever property is the code one
  546.         /// </summary>
  547.         /// <param name="propertyName">The property name.</param>
  548.         /// <returns>
  549.         /// Returns true if property should be stored in code, false otherwise
  550.         /// </returns>
  551.         private static bool IsCoreProperty(string propertyName)
  552.         {
  553.             bool flag;
  554.             Assert.ArgumentNotNull(propertyName, "propertyName");
  555.             IEnumerator enumerator = ProfileBase.Properties.GetEnumerator();
  556.             try
  557.             {
  558.                 while (enumerator.MoveNext())
  559.                 {
  560.                     SettingsProperty current = (SettingsProperty)enumerator.Current;
  561.                     if (string.Compare(current.Name, propertyName, StringComparison.InvariantCultureIgnoreCase) != 0)
  562.                     {
  563.                         continue;
  564.                     }
  565.                     flag = true;
  566.                     return flag;
  567.                 }
  568.                 return false;
  569.             }
  570.             finally
  571.             {
  572.                 IDisposable disposable = enumerator as IDisposable;
  573.                 if (disposable != null)
  574.                 {
  575.                     disposable.Dispose();
  576.                 }
  577.             }
  578.             return flag;
  579.         }
  580.  
  581.          
  582.  
  583.         /// <summary>
  584.         /// The raise user updated event.
  585.         /// </summary>
  586.         private void RaiseUserUpdatedEvent()
  587.         {
  588.             MembershipUser user = Membership.GetUser(base.UserName);
  589.             if (user != null)
  590.             {
  591.                 object[] objArray = new object[] { user };
  592.                 Event.RaiseEvent("user:updated", objArray);
  593.             }
  594.         }
  595.  
  596.         /// <summary>
  597.         /// Reloads all profile data for the user.
  598.         /// </summary>
  599.         public override void Reload()
  600.         {
  601.             this.Cache.RemoveUser(base.UserName);
  602.             this.customProperties = null;
  603.             this.customPropertiesIsDirty = false;
  604.             this.innerMembershipUser = null;
  605.         }
  606.  
  607.         /// <summary>
  608.         /// Removes a named custom property.
  609.         /// </summary>
  610.         /// <param name="propertyName">
  611.         /// The property name.
  612.         /// </param>
  613.         public override void RemoveCustomProperty(string propertyName)
  614.         {
  615.             Assert.ArgumentNotNullOrEmpty(propertyName, "propertyName");
  616.             Dictionary<string, string> customProperties = this.CustomProperties;
  617.             lock (customProperties)
  618.             {
  619.                 customProperties.Remove(propertyName);
  620.             }
  621.             this.customPropertiesIsDirty = true;
  622.         }
  623.  
  624.         /// <summary>
  625.         /// Updates the profile data source with changed profile property values.
  626.         /// </summary>
  627.         public override void Save()
  628.         {
  629.             this.innerMembershipUser = null;
  630.             this.SaveUserProperties();
  631.             this.SerializeCustomProperties();
  632.             base.Save();
  633.             this.RaiseUserUpdatedEvent();
  634.         }
  635.  
  636.         /// <summary>
  637.         /// Saves the user properties.
  638.         /// </summary>
  639.         private void SaveUserProperties()
  640.         {
  641.             if (!this.emailChanged)
  642.             {
  643.                 return;
  644.             }
  645.             MembershipUser user = Membership.GetUser(base.UserName);
  646.             if (user == null)
  647.             {
  648.                 return;
  649.             }
  650.             user.Email = this.email;
  651.             Membership.UpdateUser(user);
  652.         }
  653.  
  654.         /// <summary>
  655.         /// Serializes the custom properties.
  656.         /// </summary>
  657.         private void SerializeCustomProperties()
  658.         {
  659.             if (!this.customPropertiesIsDirty)
  660.             {
  661.                 return;
  662.             }
  663.             this.SerializedData = this.CustomProperties;
  664.         }
  665.  
  666.         /// <summary>
  667.         /// Sets the value of a custom property.
  668.         /// </summary>
  669.         /// <param name="propertyName">Name of the property.</param>
  670.         /// <param name="value">The value.</param>
  671.         public override void SetCustomProperty(string propertyName, string value)
  672.         {
  673.             Assert.ArgumentNotNull(propertyName, "propertyName");
  674.             Assert.ArgumentNotNull(value, "value");
  675.             Dictionary<string, string> customProperties = this.CustomProperties;
  676.             lock (customProperties)
  677.             {
  678.                 customProperties[propertyName] = value;
  679.             }
  680.             this.customPropertiesIsDirty = true;
  681.         }
  682.  
  683.         /// <summary>
  684.         /// Sets a named property value.
  685.         /// </summary>
  686.         /// <param name="propertyName">
  687.         /// Name of the property.
  688.         /// </param>
  689.         /// <param name="value">
  690.         /// The value.
  691.         /// </param>
  692.         protected override void SetPropertyValueCore(string propertyName, object value)
  693.         {
  694.             Assert.ArgumentNotNullOrEmpty(propertyName, "name");
  695.             base.SetPropertyValue(propertyName, value);
  696.             this.SetPropertyValueToCache(propertyName, value);
  697.         }
  698.  
  699.         /// <summary>
  700.         /// Sets the property value to cache.
  701.         /// </summary>
  702.         /// <param name="propertyName">
  703.         /// Name of the property.
  704.         /// </param>
  705.         /// <param name="propertyValue">
  706.         /// The property value.
  707.         /// </param>
  708.         private void SetPropertyValueToCache(string propertyName, object propertyValue)
  709.         {
  710.             Assert.IsNotNullOrEmpty(propertyName, "propertyName");
  711.             UserProfileCache userProfileCache = CacheManager.GetUserProfileCache();
  712.             Assert.IsNotNull(userProfileCache, "profileCache");
  713.             userProfileCache.AddRecord(base.UserName, propertyName, propertyValue);
  714.         }
  715.     }
  716. }
Advertisement
Add Comment
Please, Sign In to add comment