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