[asp.net] Fixes bug #676008. HtmlForm 'name' attribute rendering fixes.
[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 = CreateSettingsProperty (pgs, ppsc [s]);
80                                         ValidateProperty (settingsProperty, ppsc [s].ElementInformation);
81                                         properties.Add (settingsProperty);
82                                 }
83                         }
84
85                         for (int s = 0; s < ps.Count; s++) {
86                                 SettingsProperty settingsProperty = CreateSettingsProperty (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 (CreateSettingsProperty (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 = HttpApplication.LoadType (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 CreateSettingsProperty (PropertyInfo property)
244                 {
245                         SettingsProperty sp = new SettingsProperty (property.Name);
246                         Attribute [] attributes = (Attribute [])property.GetCustomAttributes (false);
247                         SettingsAttributeDictionary attDict = new SettingsAttributeDictionary();
248                         bool defaultAssigned = false;
249                         
250                         sp.SerializeAs = SettingsSerializeAs.ProviderSpecific;
251                         sp.PropertyType = property.PropertyType;
252                         sp.IsReadOnly = false;
253                         sp.ThrowOnErrorDeserializing = false;
254                         sp.ThrowOnErrorSerializing = true;
255
256                         for (int i = 0; i < attributes.Length; i++) {
257                                 if (attributes [i] is DefaultSettingValueAttribute) {
258                                         sp.DefaultValue = ((DefaultSettingValueAttribute) attributes [i]).Value;
259                                         defaultAssigned = true;
260                                 } else if (attributes [i] is SettingsProviderAttribute) {
261                                         Type providerType = HttpApplication.LoadType (((SettingsProviderAttribute) attributes [i]).ProviderTypeName);
262                                         sp.Provider = (SettingsProvider) Activator.CreateInstance (providerType);
263                                         sp.Provider.Initialize (null, null);
264                                 } else if (attributes [i] is SettingsSerializeAsAttribute) {
265                                         sp.SerializeAs = ((SettingsSerializeAsAttribute) attributes [i]).SerializeAs;
266                                 } else if (attributes [i] is SettingsAllowAnonymousAttribute) {
267                                         sp.Attributes ["AllowAnonymous"] = ((SettingsAllowAnonymousAttribute) attributes [i]).Allow;
268                                 } else if (attributes [i] is CustomProviderDataAttribute) {
269                                         sp.Attributes ["CustomProviderData"] = ((CustomProviderDataAttribute) attributes [i]).CustomProviderData;
270                                 } else if (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                         if (!defaultAssigned && sp.PropertyType == typeof (string) && sp.DefaultValue == null)
284                                 sp.DefaultValue = String.Empty;
285                         
286                         return sp;
287                 }
288                 
289                 static SettingsProperty CreateSettingsProperty (ProfileGroupSettings pgs, ProfilePropertySettings pps)
290                 {
291                         string name = ((pgs == null) ? String.Empty : pgs.Name + ".") + pps.Name;
292                         SettingsProperty sp = new SettingsProperty (name);
293
294                         sp.Attributes.Add ("AllowAnonymous", pps.AllowAnonymous);
295                         sp.DefaultValue = pps.DefaultValue;
296                         sp.IsReadOnly = pps.ReadOnly;
297                         sp.Provider = ProfileManager.Provider;
298                         sp.ThrowOnErrorDeserializing = false;
299                         sp.ThrowOnErrorSerializing = true;
300
301                         if (pps.Type.Length == 0 || pps.Type == "string")
302                                 sp.PropertyType = typeof (string);
303                         else
304                                 sp.PropertyType = GetPropertyType (pgs, pps);
305
306                         switch (pps.SerializeAs) {
307                                 case SerializationMode.Binary:
308                                         sp.SerializeAs = SettingsSerializeAs.Binary;
309                                         break;
310                                 case SerializationMode.ProviderSpecific:
311                                         sp.SerializeAs = SettingsSerializeAs.ProviderSpecific;
312                                         break;
313                                 case SerializationMode.String:
314                                         sp.SerializeAs = SettingsSerializeAs.String;
315                                         break;
316                                 case SerializationMode.Xml:
317                                         sp.SerializeAs = SettingsSerializeAs.Xml;
318                                         break;
319                         }
320
321                         return sp;
322                 }
323
324
325                 public void Initialize (string username, bool isAuthenticated)
326                 {
327                         _settingsContext = new SettingsContext ();
328                         _settingsContext.Add ("UserName", username);
329                         _settingsContext.Add ("IsAuthenticated", isAuthenticated);
330                         SettingsProviderCollection spc = new SettingsProviderCollection();
331                         spc.Add (ProfileManager.Provider);
332                         base.Initialize (Context, ProfileBase.Properties, spc);
333                 }
334
335                 public override void Save ()
336                 {
337                         if (IsDirty) {
338                                 ProfileManager.Provider.SetPropertyValues (_settingsContext, _propertiyValues);
339                         }
340                 }
341
342                 public bool IsAnonymous {
343                         get {
344                                 return !(bool) _settingsContext ["IsAuthenticated"];
345                         }
346                 }
347
348                 public bool IsDirty {
349                         get {
350                                 return _dirty;
351                         }
352                 }
353
354                 public DateTime LastActivityDate {
355                         get {
356                                 return _lastActivityDate;
357                         }
358                 }
359
360                 public DateTime LastUpdatedDate {
361                         get {
362                                 return _lastUpdatedDate;
363                         }
364                 }
365
366                 public new static SettingsPropertyCollection Properties {
367                         get {
368                                 if (_properties == null)
369                                         InitProperties ();
370
371                                 return _properties;
372                         }
373                 }
374
375                 public string UserName {
376                         get {
377                                 return (string) _settingsContext ["UserName"];
378                         }
379                 }
380         }
381
382 }
383
384 #endif