ProfileBase.cs 12 KB

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