ProfileBase.cs 12 KB

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