ProfileBase.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.Security;
  34. using System.Web.Configuration;
  35. namespace System.Web.Profile
  36. {
  37. public class ProfileBase : SettingsBase
  38. {
  39. bool _propertiyValuesLoaded = false;
  40. bool _dirty = false;
  41. DateTime _lastActivityDate = DateTime.MinValue;
  42. DateTime _lastUpdatedDate = DateTime.MinValue;
  43. SettingsContext _settingsContext = null;
  44. SettingsPropertyValueCollection _propertiyValues = null;
  45. const string Profiles_SettingsPropertyCollection = "Profiles.SettingsPropertyCollection";
  46. #if TARGET_J2EE
  47. static SettingsPropertyCollection _properties
  48. {
  49. get
  50. {
  51. object o = AppDomain.CurrentDomain.GetData (Profiles_SettingsPropertyCollection);
  52. return (SettingsPropertyCollection) o;
  53. }
  54. set
  55. {
  56. AppDomain.CurrentDomain.SetData (Profiles_SettingsPropertyCollection, value);
  57. }
  58. }
  59. #else
  60. static SettingsPropertyCollection _properties = null;
  61. #endif
  62. static void InitProperties ()
  63. {
  64. SettingsPropertyCollection properties = new SettingsPropertyCollection ();
  65. ProfileSection config = (ProfileSection) WebConfigurationManager.GetSection ("system.web/profile");
  66. RootProfilePropertySettingsCollection ps = config.PropertySettings;
  67. for (int i = 0; i < ps.GroupSettings.Count; i++) {
  68. ProfileGroupSettings pgs = ps.GroupSettings [i];
  69. ProfilePropertySettingsCollection ppsc = pgs.PropertySettings;
  70. for (int s = 0; s < ppsc.Count; s++) {
  71. SettingsProperty settingsProperty = CreateSettingsPropery (pgs, ppsc [s]);
  72. ValidateProperty (settingsProperty, ppsc [i].ElementInformation);
  73. properties.Add (settingsProperty);
  74. }
  75. }
  76. for (int s = 0; s < ps.Count; s++) {
  77. SettingsProperty settingsProperty = CreateSettingsPropery (null, ps [s]);
  78. ValidateProperty (settingsProperty, ps [s].ElementInformation);
  79. properties.Add (settingsProperty);
  80. }
  81. properties.SetReadOnly ();
  82. lock (Profiles_SettingsPropertyCollection) {
  83. if (_properties == null)
  84. _properties = properties;
  85. }
  86. }
  87. public ProfileBase ()
  88. {
  89. }
  90. public static ProfileBase Create (string username)
  91. {
  92. return Create (username, true);
  93. }
  94. public static ProfileBase Create (string username, bool isAuthenticated)
  95. {
  96. ProfileBase profile = null;
  97. Type profileType = ProfileParser.GetProfileCommonType (HttpContext.Current);
  98. if (profileType != null)
  99. profile = (ProfileBase) Activator.CreateInstance (profileType);
  100. else
  101. profile = (ProfileBase) new DefaultProfile ();
  102. profile.Initialize (username, isAuthenticated);
  103. return profile;
  104. }
  105. public ProfileGroupBase GetProfileGroup (string groupName)
  106. {
  107. ProfileGroupBase group = null;
  108. Type groupType = ProfileParser.GetProfileGroupType (HttpContext.Current, groupName);
  109. if (groupType != null)
  110. group = (ProfileGroupBase) Activator.CreateInstance (groupType);
  111. else
  112. throw new ProviderException ("Group '" + groupName + "' not found");
  113. group.Init (this, groupName);
  114. return group;
  115. }
  116. public object GetPropertyValue (string propertyName)
  117. {
  118. if (!_propertiyValuesLoaded)
  119. InitPropertiesValues ();
  120. _lastActivityDate = DateTime.UtcNow;
  121. return ((SettingsPropertyValue) _propertiyValues [propertyName]).PropertyValue;
  122. }
  123. public void SetPropertyValue (string propertyName, object propertyValue)
  124. {
  125. if (!_propertiyValuesLoaded)
  126. InitPropertiesValues ();
  127. if (_propertiyValues [propertyName] == null)
  128. throw new SettingsPropertyNotFoundException ("The settings property '" + propertyName + "' was not found.");
  129. ((SettingsPropertyValue) _propertiyValues [propertyName]).PropertyValue = propertyValue;
  130. _dirty = true;
  131. _lastActivityDate = DateTime.UtcNow;
  132. _lastUpdatedDate = _lastActivityDate;
  133. }
  134. public override object this [string propertyName]
  135. {
  136. get
  137. {
  138. return GetPropertyValue (propertyName);
  139. }
  140. set
  141. {
  142. SetPropertyValue (propertyName, value);
  143. }
  144. }
  145. void InitPropertiesValues ()
  146. {
  147. if (!_propertiyValuesLoaded) {
  148. _propertiyValues = ProfileManager.Provider.GetPropertyValues (_settingsContext, Properties);
  149. _propertiyValuesLoaded = true;
  150. }
  151. }
  152. static Type GetPropertyType (ProfileGroupSettings pgs, ProfilePropertySettings pps)
  153. {
  154. Type type = Type.GetType (pps.Type);
  155. if (type != null)
  156. return type;
  157. Type profileType = null;
  158. if (pgs == null)
  159. profileType = ProfileParser.GetProfileCommonType (HttpContext.Current);
  160. else
  161. profileType = ProfileParser.GetProfileGroupType (HttpContext.Current, pgs.Name);
  162. if (profileType == null)
  163. return null;
  164. System.Reflection.PropertyInfo pi = profileType.GetProperty (pps.Name);
  165. if (pi != null)
  166. return pi.PropertyType;
  167. return null;
  168. }
  169. static void ValidateProperty (SettingsProperty settingsProperty, ElementInformation elementInfo)
  170. {
  171. string exceptionMessage = string.Empty;
  172. if (!AnonymousIdentificationModule.Enabled &&
  173. (bool) settingsProperty.Attributes ["AllowAnonymous"])
  174. exceptionMessage = "Profile property '{0}' allows anonymous users to store data. " +
  175. "This requires that the AnonymousIdentification feature be enabled.";
  176. if (settingsProperty.PropertyType == null)
  177. exceptionMessage = "The type specified for a profile property '{0}' could not be found.";
  178. if (settingsProperty.SerializeAs == SettingsSerializeAs.Binary &&
  179. !settingsProperty.PropertyType.IsSerializable)
  180. exceptionMessage = "The type for the property '{0}' cannot be serialized " +
  181. "using the binary serializer, since the type is not marked as serializable.";
  182. if (exceptionMessage.Length > 0)
  183. throw new ConfigurationErrorsException (string.Format (exceptionMessage, settingsProperty.Name),
  184. elementInfo.Source, elementInfo.LineNumber);
  185. }
  186. static SettingsProperty CreateSettingsPropery (ProfileGroupSettings pgs, ProfilePropertySettings pps)
  187. {
  188. string name = ((pgs == null) ? "" : pgs.Name + ".") + pps.Name;
  189. SettingsProperty sp = new SettingsProperty (name);
  190. sp.Attributes.Add ("AllowAnonymous", pps.AllowAnonymous);
  191. sp.DefaultValue = pps.DefaultValue;
  192. sp.IsReadOnly = pps.ReadOnly;
  193. sp.Provider = ProfileManager.Provider;
  194. sp.ThrowOnErrorDeserializing = false;
  195. sp.ThrowOnErrorSerializing = true;
  196. if (pps.Type.Length == 0 || pps.Type == "string")
  197. sp.PropertyType = typeof (string);
  198. else
  199. sp.PropertyType = GetPropertyType (pgs, pps);
  200. switch (pps.SerializeAs) {
  201. case SerializationMode.Binary:
  202. sp.SerializeAs = SettingsSerializeAs.Binary;
  203. break;
  204. case SerializationMode.ProviderSpecific:
  205. sp.SerializeAs = SettingsSerializeAs.ProviderSpecific;
  206. break;
  207. case SerializationMode.String:
  208. sp.SerializeAs = SettingsSerializeAs.String;
  209. break;
  210. case SerializationMode.Xml:
  211. sp.SerializeAs = SettingsSerializeAs.Xml;
  212. break;
  213. }
  214. return sp;
  215. }
  216. public void Initialize (string username, bool isAuthenticated)
  217. {
  218. _settingsContext = new SettingsContext ();
  219. _settingsContext.Add ("UserName", username);
  220. _settingsContext.Add ("IsAuthenticated", isAuthenticated);
  221. }
  222. public override void Save ()
  223. {
  224. if (IsDirty) {
  225. ProfileManager.Provider.SetPropertyValues (_settingsContext, _propertiyValues);
  226. }
  227. }
  228. public bool IsAnonymous {
  229. get {
  230. return !(bool) _settingsContext ["IsAuthenticated"];
  231. }
  232. }
  233. public bool IsDirty {
  234. get {
  235. return _dirty;
  236. }
  237. }
  238. public DateTime LastActivityDate {
  239. get {
  240. return _lastActivityDate;
  241. }
  242. }
  243. public DateTime LastUpdatedDate {
  244. get {
  245. return _lastUpdatedDate;
  246. }
  247. }
  248. public static SettingsPropertyCollection Properties {
  249. get {
  250. if (_properties == null)
  251. InitProperties ();
  252. return _properties;
  253. }
  254. }
  255. public string UserName {
  256. get {
  257. return (string) _settingsContext ["UserName"];
  258. }
  259. }
  260. }
  261. }
  262. #endif