ProfileBase.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //
  2. // System.Web.UI.WebControls.ProfileBase.cs
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. // Vladimir Krasnov ([email protected])
  7. //
  8. // (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.Configuration;
  32. using System.Configuration.Provider;
  33. using System.Web.Configuration;
  34. namespace System.Web.Profile
  35. {
  36. public class ProfileBase : SettingsBase
  37. {
  38. bool _propertiyValuesLoaded = false;
  39. bool _dirty = false;
  40. DateTime _lastActivityDate = DateTime.MinValue;
  41. DateTime _lastUpdatedDate = DateTime.MinValue;
  42. SettingsContext _settingsContext = null;
  43. SettingsPropertyValueCollection _propertiyValues = null;
  44. #if TARGET_J2EE
  45. const string Profiles_SettingsPropertyCollection = "Profiles.SettingsPropertyCollection";
  46. static SettingsPropertyCollection _properties
  47. {
  48. get
  49. {
  50. object o = AppDomain.CurrentDomain.GetData (Profiles_SettingsPropertyCollection);
  51. return (SettingsPropertyCollection) o;
  52. }
  53. set
  54. {
  55. AppDomain.CurrentDomain.SetData (Profiles_SettingsPropertyCollection, value);
  56. }
  57. }
  58. #else
  59. static SettingsPropertyCollection _properties = null;
  60. #endif
  61. static void InitProperties ()
  62. {
  63. _properties = new SettingsPropertyCollection ();
  64. ProfileSection config = (ProfileSection) WebConfigurationManager.GetSection ("system.web/profile");
  65. RootProfilePropertySettingsCollection ps = config.PropertySettings;
  66. for (int i = 0; i < ps.GroupSettings.Count; i++) {
  67. ProfileGroupSettings pgs = ps.GroupSettings [i];
  68. ProfilePropertySettingsCollection ppsc = pgs.PropertySettings;
  69. for (int s = 0; s < ppsc.Count; s++) {
  70. _properties.Add (CreateSettingsPropery (pgs, ppsc [s]));
  71. }
  72. }
  73. for (int s = 0; s < ps.Count; s++)
  74. _properties.Add (CreateSettingsPropery (null, ps [s]));
  75. _properties.SetReadOnly ();
  76. }
  77. public ProfileBase ()
  78. {
  79. }
  80. public static ProfileBase Create (string username)
  81. {
  82. return Create (username, true);
  83. }
  84. public static ProfileBase Create (string username, bool isAuthenticated)
  85. {
  86. ProfileBase profile = null;
  87. Type profileType = ProfileParser.GetProfileCommonType (HttpContext.Current);
  88. if (profileType != null)
  89. profile = (ProfileBase) Activator.CreateInstance (profileType);
  90. else
  91. profile = (ProfileBase) new DefaultProfile ();
  92. profile.Initialize (username, isAuthenticated);
  93. return profile;
  94. }
  95. public ProfileGroupBase GetProfileGroup (string groupName)
  96. {
  97. ProfileGroupBase group = null;
  98. Type groupType = ProfileParser.GetProfileGroupType (HttpContext.Current, "ProfileGroup" + groupName);
  99. if (groupType != null)
  100. group = (ProfileGroupBase) Activator.CreateInstance (groupType);
  101. else
  102. throw new ProviderException ("Group " + groupName + " not found");
  103. group.Init (this, groupName);
  104. return group;
  105. }
  106. public object GetPropertyValue (string propertyName)
  107. {
  108. if (!_propertiyValuesLoaded)
  109. InitPropertiesValues ();
  110. _lastActivityDate = DateTime.UtcNow;
  111. return ((SettingsPropertyValue) _propertiyValues [propertyName]).PropertyValue;
  112. }
  113. public void SetPropertyValue (string propertyName, object propertyValue)
  114. {
  115. if (!_propertiyValuesLoaded)
  116. InitPropertiesValues ();
  117. if (_propertiyValues [propertyName] == null)
  118. throw new SettingsPropertyNotFoundException ("The settings property '" + propertyName + "' was not found.");
  119. ((SettingsPropertyValue) _propertiyValues [propertyName]).PropertyValue = propertyValue;
  120. _dirty = true;
  121. _lastActivityDate = DateTime.UtcNow;
  122. _lastUpdatedDate = _lastActivityDate;
  123. }
  124. public override object this [string propertyName]
  125. {
  126. get
  127. {
  128. return GetPropertyValue (propertyName);
  129. }
  130. set
  131. {
  132. SetPropertyValue (propertyName, value);
  133. }
  134. }
  135. void InitPropertiesValues ()
  136. {
  137. if (!_propertiyValuesLoaded) {
  138. _propertiyValues = ProfileManager.Provider.GetPropertyValues (_settingsContext, Properties);
  139. _propertiyValuesLoaded = true;
  140. }
  141. }
  142. static SettingsProperty CreateSettingsPropery (ProfileGroupSettings pgs, ProfilePropertySettings pps)
  143. {
  144. string name = ((pgs == null) ? "" : pgs.Name + ".") + pps.Name;
  145. SettingsProperty sp = new SettingsProperty (name);
  146. sp.Attributes.Add ("AllowAnonymous", pps.AllowAnonymous);
  147. sp.DefaultValue = pps.DefaultValue;
  148. sp.IsReadOnly = pps.ReadOnly;
  149. sp.Provider = ProfileManager.Provider;
  150. sp.ThrowOnErrorDeserializing = false;
  151. sp.ThrowOnErrorSerializing = true;
  152. if (pps.Type.Length == 0 || pps.Type == "string")
  153. sp.PropertyType = typeof (string);
  154. else
  155. sp.PropertyType = Type.GetType (pps.Type);
  156. switch (pps.SerializeAs) {
  157. case SerializationMode.Binary:
  158. sp.SerializeAs = SettingsSerializeAs.Binary;
  159. break;
  160. case SerializationMode.ProviderSpecific:
  161. sp.SerializeAs = SettingsSerializeAs.ProviderSpecific;
  162. break;
  163. case SerializationMode.String:
  164. sp.SerializeAs = SettingsSerializeAs.String;
  165. break;
  166. case SerializationMode.Xml:
  167. sp.SerializeAs = SettingsSerializeAs.Xml;
  168. break;
  169. }
  170. return sp;
  171. }
  172. public void Initialize (string username, bool isAuthenticated)
  173. {
  174. _settingsContext = new SettingsContext ();
  175. _settingsContext.Add ("UserName", username);
  176. _settingsContext.Add ("IsAuthenticated", isAuthenticated);
  177. }
  178. public override void Save ()
  179. {
  180. if (IsDirty) {
  181. ProfileManager.Provider.SetPropertyValues (_settingsContext, _propertiyValues);
  182. }
  183. }
  184. public bool IsAnonymous {
  185. get {
  186. return !(bool) _settingsContext ["IsAuthenticated"];
  187. }
  188. }
  189. public bool IsDirty {
  190. get {
  191. return _dirty;
  192. }
  193. }
  194. public DateTime LastActivityDate {
  195. get {
  196. return _lastActivityDate;
  197. }
  198. }
  199. public DateTime LastUpdatedDate {
  200. get {
  201. return _lastUpdatedDate;
  202. }
  203. }
  204. public static SettingsPropertyCollection Properties {
  205. get {
  206. if (_properties == null)
  207. InitProperties ();
  208. return _properties;
  209. }
  210. }
  211. public string UserName {
  212. get {
  213. return (string) _settingsContext ["UserName"];
  214. }
  215. }
  216. }
  217. }
  218. #endif