ProfileBase.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 = CreateSettingsPropery (pgs, ppsc [s]);
  73. ValidateProperty (settingsProperty, ppsc [i].ElementInformation);
  74. properties.Add (settingsProperty);
  75. }
  76. }
  77. for (int s = 0; s < ps.Count; s++) {
  78. SettingsProperty settingsProperty = CreateSettingsPropery (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 (CreateSettingsPropery (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 = Type.GetType (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 CreateSettingsPropery (PropertyInfo property)
  207. {
  208. SettingsProperty sp = new SettingsProperty (property.Name);
  209. Attribute [] attributes = (Attribute [])property.GetCustomAttributes (typeof (SettingsSerializeAsAttribute), false);
  210. SettingsAttributeDictionary attDict = new SettingsAttributeDictionary();
  211. sp.SerializeAs = SettingsSerializeAs.ProviderSpecific;
  212. sp.PropertyType = property.PropertyType;
  213. sp.IsReadOnly = false;
  214. sp.ThrowOnErrorDeserializing = false;
  215. sp.ThrowOnErrorSerializing = true;
  216. for (int i = 0; i < attributes.Length; i++) {
  217. if (attributes [i] is DefaultSettingValueAttribute)
  218. sp.DefaultValue = ((DefaultSettingValueAttribute) attributes [i]).Value;
  219. else if (attributes [i] is SettingsProviderAttribute) {
  220. Type providerType = Type.GetType (((SettingsProviderAttribute) attributes [i]).ProviderTypeName);
  221. sp.Provider = (SettingsProvider) Activator.CreateInstance (providerType);
  222. sp.Provider.Initialize (null, null);
  223. }
  224. else if (attributes [i] is SettingsSerializeAsAttribute)
  225. sp.SerializeAs = ((SettingsSerializeAsAttribute) attributes [i]).SerializeAs;
  226. else if (
  227. attributes [i] is SettingsAllowAnonymousAttribute ||
  228. attributes [i] is ApplicationScopedSettingAttribute ||
  229. attributes [i] is UserScopedSettingAttribute ||
  230. attributes [i] is SettingsDescriptionAttribute ||
  231. attributes [i] is SettingAttribute)
  232. attDict.Add (attributes [i].GetType (), attributes [i]);
  233. }
  234. if (sp.Provider == null)
  235. sp.Provider = ProfileManager.Provider;
  236. if (sp.Attributes ["AllowAnonymous"] == null)
  237. sp.Attributes ["AllowAnonymous"] = false;
  238. return sp;
  239. }
  240. static SettingsProperty CreateSettingsPropery (ProfileGroupSettings pgs, ProfilePropertySettings pps)
  241. {
  242. string name = ((pgs == null) ? "" : pgs.Name + ".") + pps.Name;
  243. SettingsProperty sp = new SettingsProperty (name);
  244. sp.Attributes.Add ("AllowAnonymous", pps.AllowAnonymous);
  245. sp.DefaultValue = pps.DefaultValue;
  246. sp.IsReadOnly = pps.ReadOnly;
  247. sp.Provider = ProfileManager.Provider;
  248. sp.ThrowOnErrorDeserializing = false;
  249. sp.ThrowOnErrorSerializing = true;
  250. if (pps.Type.Length == 0 || pps.Type == "string")
  251. sp.PropertyType = typeof (string);
  252. else
  253. sp.PropertyType = GetPropertyType (pgs, pps);
  254. switch (pps.SerializeAs) {
  255. case SerializationMode.Binary:
  256. sp.SerializeAs = SettingsSerializeAs.Binary;
  257. break;
  258. case SerializationMode.ProviderSpecific:
  259. sp.SerializeAs = SettingsSerializeAs.ProviderSpecific;
  260. break;
  261. case SerializationMode.String:
  262. sp.SerializeAs = SettingsSerializeAs.String;
  263. break;
  264. case SerializationMode.Xml:
  265. sp.SerializeAs = SettingsSerializeAs.Xml;
  266. break;
  267. }
  268. return sp;
  269. }
  270. public void Initialize (string username, bool isAuthenticated)
  271. {
  272. _settingsContext = new SettingsContext ();
  273. _settingsContext.Add ("UserName", username);
  274. _settingsContext.Add ("IsAuthenticated", isAuthenticated);
  275. SettingsProviderCollection spc = new SettingsProviderCollection();
  276. spc.Add (ProfileManager.Provider);
  277. base.Initialize (Context, ProfileBase.Properties, spc);
  278. }
  279. public override void Save ()
  280. {
  281. if (IsDirty) {
  282. ProfileManager.Provider.SetPropertyValues (_settingsContext, _propertiyValues);
  283. }
  284. }
  285. public bool IsAnonymous {
  286. get {
  287. return !(bool) _settingsContext ["IsAuthenticated"];
  288. }
  289. }
  290. public bool IsDirty {
  291. get {
  292. return _dirty;
  293. }
  294. }
  295. public DateTime LastActivityDate {
  296. get {
  297. return _lastActivityDate;
  298. }
  299. }
  300. public DateTime LastUpdatedDate {
  301. get {
  302. return _lastUpdatedDate;
  303. }
  304. }
  305. public new static SettingsPropertyCollection Properties {
  306. get {
  307. if (_properties == null)
  308. InitProperties ();
  309. return _properties;
  310. }
  311. }
  312. public string UserName {
  313. get {
  314. return (string) _settingsContext ["UserName"];
  315. }
  316. }
  317. }
  318. }
  319. #endif