2 // System.Web.UI.WebControls.ProfileBase.cs
5 // Chris Toshok (toshok@ximian.com)
6 // Vladimir Krasnov (vladimirk@mainsoft.com)
8 // (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using System.Configuration;
33 using System.Configuration.Provider;
34 using System.Web.Configuration;
36 namespace System.Web.Profile
38 public class ProfileBase : SettingsBase
40 bool _propertiyValuesLoaded = false;
42 DateTime _lastActivityDate = DateTime.MinValue;
43 DateTime _lastUpdatedDate = DateTime.MinValue;
44 SettingsContext _settingsContext = null;
45 SettingsPropertyValueCollection _propertiyValues = null;
48 const string Profiles_SettingsPropertyCollection = "Profiles.SettingsPropertyCollection";
49 static SettingsPropertyCollection _properties
53 object o = AppDomain.CurrentDomain.GetData (Profiles_SettingsPropertyCollection);
54 return (SettingsPropertyCollection) o;
58 AppDomain.CurrentDomain.SetData (Profiles_SettingsPropertyCollection, value);
62 static SettingsPropertyCollection _properties = null;
65 static void InitProperties ()
67 _properties = new SettingsPropertyCollection ();
69 ProfileSection config = (ProfileSection) WebConfigurationManager.GetSection ("system.web/profile");
70 RootProfilePropertySettingsCollection ps = config.PropertySettings;
72 for (int i = 0; i < ps.GroupSettings.Count; i++) {
73 ProfileGroupSettings pgs = ps.GroupSettings [i];
74 ProfilePropertySettingsCollection ppsc = pgs.PropertySettings;
76 for (int s = 0; s < ppsc.Count; s++) {
77 _properties.Add (CreateSettingsPropery (pgs, ppsc [s]));
81 for (int s = 0; s < ps.Count; s++)
82 _properties.Add (CreateSettingsPropery (null, ps [s]));
84 _properties.SetReadOnly ();
91 public static ProfileBase Create (string username)
93 return Create (username, true);
96 public static ProfileBase Create (string username, bool isAuthenticated)
98 ProfileBase profile = null;
99 Type profileType = ProfileParser.GetProfileCommonType (HttpContext.Current);
100 if (profileType != null)
101 profile = (ProfileBase) Activator.CreateInstance (profileType);
103 profile = (ProfileBase) new DefaultProfile ();
105 profile.Initialize (username, isAuthenticated);
109 public ProfileGroupBase GetProfileGroup (string groupName)
111 ProfileGroupBase group = null;
112 Type groupType = ProfileParser.GetProfileGroupType (HttpContext.Current, "ProfileGroup" + groupName);
113 if (groupType != null)
114 group = (ProfileGroupBase) Activator.CreateInstance (groupType);
116 throw new ProviderException ("Group " + groupName + " not found");
118 group.Init (this, groupName);
122 public object GetPropertyValue (string propertyName)
124 if (!_propertiyValuesLoaded)
125 InitPropertiesValues ();
127 _lastActivityDate = DateTime.UtcNow;
129 return ((SettingsPropertyValue) _propertiyValues [propertyName]).PropertyValue;
132 public void SetPropertyValue (string propertyName, object propertyValue)
134 if (!_propertiyValuesLoaded)
135 InitPropertiesValues ();
137 if (_propertiyValues [propertyName] == null)
138 throw new SettingsPropertyNotFoundException ("The settings property '" + propertyName + "' was not found.");
140 ((SettingsPropertyValue) _propertiyValues [propertyName]).PropertyValue = propertyValue;
142 _lastActivityDate = DateTime.UtcNow;
143 _lastUpdatedDate = _lastActivityDate;
146 public override object this [string propertyName]
150 return GetPropertyValue (propertyName);
154 SetPropertyValue (propertyName, value);
158 void InitPropertiesValues ()
160 if (!_propertiyValuesLoaded) {
161 _propertiyValues = ProfileManager.Provider.GetPropertyValues (_settingsContext, Properties);
162 _propertiyValuesLoaded = true;
166 static SettingsProperty CreateSettingsPropery (ProfileGroupSettings pgs, ProfilePropertySettings pps)
168 string name = ((pgs == null) ? "" : pgs.Name + ".") + pps.Name;
169 SettingsProperty sp = new SettingsProperty (name);
171 sp.Attributes.Add ("AllowAnonymous", pps.AllowAnonymous);
172 sp.DefaultValue = pps.DefaultValue;
173 sp.IsReadOnly = pps.ReadOnly;
174 sp.Provider = ProfileManager.Provider;
175 sp.ThrowOnErrorDeserializing = false;
176 sp.ThrowOnErrorSerializing = true;
178 if (pps.Type.Length == 0 || pps.Type == "string")
179 sp.PropertyType = typeof (string);
181 sp.PropertyType = Type.GetType (pps.Type);
183 switch (pps.SerializeAs) {
184 case SerializationMode.Binary:
185 sp.SerializeAs = SettingsSerializeAs.Binary;
187 case SerializationMode.ProviderSpecific:
188 sp.SerializeAs = SettingsSerializeAs.ProviderSpecific;
190 case SerializationMode.String:
191 sp.SerializeAs = SettingsSerializeAs.String;
193 case SerializationMode.Xml:
194 sp.SerializeAs = SettingsSerializeAs.Xml;
202 public void Initialize (string username, bool isAuthenticated)
204 _settingsContext = new SettingsContext ();
205 _settingsContext.Add ("UserName", username);
206 _settingsContext.Add ("IsAuthenticated", isAuthenticated);
209 public override void Save ()
212 ProfileManager.Provider.SetPropertyValues (_settingsContext, _propertiyValues);
216 public bool IsAnonymous {
218 return !(bool) _settingsContext ["IsAuthenticated"];
222 public bool IsDirty {
228 public DateTime LastActivityDate {
230 return _lastActivityDate;
234 public DateTime LastUpdatedDate {
236 return _lastUpdatedDate;
240 public static SettingsPropertyCollection Properties {
242 if (_properties == null)
249 public string UserName {
251 return (string) _settingsContext ["UserName"];