1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
//
// System.Web.UI.WebControls.ProfileBase.cs
//
// Authors:
// Chris Toshok (toshok@ximian.com)
// Vladimir Krasnov (vladimirk@mainsoft.com)
//
// (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_2_0
using System;
using System.Configuration;
using System.Configuration.Provider;
using System.Web.Security;
using System.Web.Configuration;
using System.Reflection;
namespace System.Web.Profile
{
public class ProfileBase : SettingsBase
{
bool _propertiyValuesLoaded = false;
bool _dirty = false;
DateTime _lastActivityDate = DateTime.MinValue;
DateTime _lastUpdatedDate = DateTime.MinValue;
SettingsContext _settingsContext = null;
SettingsPropertyValueCollection _propertiyValues = null;
const string Profiles_SettingsPropertyCollection = "Profiles.SettingsPropertyCollection";
#if TARGET_J2EE
static SettingsPropertyCollection _properties
{
get
{
object o = AppDomain.CurrentDomain.GetData (Profiles_SettingsPropertyCollection);
return (SettingsPropertyCollection) o;
}
set
{
AppDomain.CurrentDomain.SetData (Profiles_SettingsPropertyCollection, value);
}
}
#else
static SettingsPropertyCollection _properties = null;
#endif
static void InitProperties ()
{
SettingsPropertyCollection properties = new SettingsPropertyCollection ();
ProfileSection config = (ProfileSection) WebConfigurationManager.GetSection ("system.web/profile");
RootProfilePropertySettingsCollection ps = config.PropertySettings;
for (int i = 0; i < ps.GroupSettings.Count; i++) {
ProfileGroupSettings pgs = ps.GroupSettings [i];
ProfilePropertySettingsCollection ppsc = pgs.PropertySettings;
for (int s = 0; s < ppsc.Count; s++) {
SettingsProperty settingsProperty = CreateSettingsProperty (pgs, ppsc [s]);
ValidateProperty (settingsProperty, ppsc [s].ElementInformation);
properties.Add (settingsProperty);
}
}
for (int s = 0; s < ps.Count; s++) {
SettingsProperty settingsProperty = CreateSettingsProperty (null, ps [s]);
ValidateProperty (settingsProperty, ps [s].ElementInformation);
properties.Add (settingsProperty);
}
if (config.Inherits.Length > 0) {
Type profileType = ProfileParser.GetProfileCommonType (HttpContext.Current);
if (profileType != null) {
Type properiesType = profileType.BaseType;
for (; ; ) {
PropertyInfo [] pi = properiesType.GetProperties (BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
if (pi.Length > 0)
for (int i = 0; i < pi.Length; i++)
properties.Add (CreateSettingsProperty (pi [i]));
if (properiesType.BaseType == null ||
properiesType.BaseType == typeof (ProfileBase))
break;
properiesType = properiesType.BaseType;
}
}
}
properties.SetReadOnly ();
lock (Profiles_SettingsPropertyCollection) {
if (_properties == null)
_properties = properties;
}
}
public ProfileBase ()
{
}
public static ProfileBase Create (string username)
{
return Create (username, true);
}
public static ProfileBase Create (string username, bool isAuthenticated)
{
ProfileBase profile = null;
Type profileType = ProfileParser.GetProfileCommonType (HttpContext.Current);
if (profileType != null)
profile = (ProfileBase) Activator.CreateInstance (profileType);
else
profile = (ProfileBase) new DefaultProfile ();
profile.Initialize (username, isAuthenticated);
return profile;
}
public ProfileGroupBase GetProfileGroup (string groupName)
{
ProfileGroupBase group = null;
Type groupType = ProfileParser.GetProfileGroupType (HttpContext.Current, groupName);
if (groupType != null)
group = (ProfileGroupBase) Activator.CreateInstance (groupType);
else
throw new ProviderException ("Group '" + groupName + "' not found");
group.Init (this, groupName);
return group;
}
public object GetPropertyValue (string propertyName)
{
if (!_propertiyValuesLoaded)
InitPropertiesValues ();
_lastActivityDate = DateTime.UtcNow;
return ((SettingsPropertyValue) _propertiyValues [propertyName]).PropertyValue;
}
public void SetPropertyValue (string propertyName, object propertyValue)
{
if (!_propertiyValuesLoaded)
InitPropertiesValues ();
if (_propertiyValues [propertyName] == null)
throw new SettingsPropertyNotFoundException ("The settings property '" + propertyName + "' was not found.");
if (!(bool)((SettingsPropertyValue)
_propertiyValues [propertyName]).Property.Attributes["AllowAnonymous"] && IsAnonymous)
throw new ProviderException ("This property cannot be set for anonymous users.");
((SettingsPropertyValue) _propertiyValues [propertyName]).PropertyValue = propertyValue;
_dirty = true;
_lastActivityDate = DateTime.UtcNow;
_lastUpdatedDate = _lastActivityDate;
}
public override object this [string propertyName]
{
get
{
return GetPropertyValue (propertyName);
}
set
{
SetPropertyValue (propertyName, value);
}
}
void InitPropertiesValues ()
{
if (!_propertiyValuesLoaded) {
_propertiyValues = ProfileManager.Provider.GetPropertyValues (_settingsContext, Properties);
_propertiyValuesLoaded = true;
}
}
static Type GetPropertyType (ProfileGroupSettings pgs, ProfilePropertySettings pps)
{
Type type = HttpApplication.LoadType (pps.Type);
if (type != null)
return type;
Type profileType = null;
if (pgs == null)
profileType = ProfileParser.GetProfileCommonType (HttpContext.Current);
else
profileType = ProfileParser.GetProfileGroupType (HttpContext.Current, pgs.Name);
if (profileType == null)
return null;
PropertyInfo pi = profileType.GetProperty (pps.Name);
if (pi != null)
return pi.PropertyType;
return null;
}
static void ValidateProperty (SettingsProperty settingsProperty, ElementInformation elementInfo)
{
string exceptionMessage = string.Empty;
if (!AnonymousIdentificationModule.Enabled &&
(bool) settingsProperty.Attributes ["AllowAnonymous"])
exceptionMessage = "Profile property '{0}' allows anonymous users to store data. " +
"This requires that the AnonymousIdentification feature be enabled.";
if (settingsProperty.PropertyType == null)
exceptionMessage = "The type specified for a profile property '{0}' could not be found.";
if (settingsProperty.SerializeAs == SettingsSerializeAs.Binary &&
!settingsProperty.PropertyType.IsSerializable)
exceptionMessage = "The type for the property '{0}' cannot be serialized " +
"using the binary serializer, since the type is not marked as serializable.";
if (exceptionMessage.Length > 0)
throw new ConfigurationErrorsException (string.Format (exceptionMessage, settingsProperty.Name),
elementInfo.Source, elementInfo.LineNumber);
}
static SettingsProperty CreateSettingsProperty (PropertyInfo property)
{
SettingsProperty sp = new SettingsProperty (property.Name);
Attribute [] attributes = (Attribute [])property.GetCustomAttributes (false);
SettingsAttributeDictionary attDict = new SettingsAttributeDictionary();
bool defaultAssigned = false;
sp.SerializeAs = SettingsSerializeAs.ProviderSpecific;
sp.PropertyType = property.PropertyType;
sp.IsReadOnly = false;
sp.ThrowOnErrorDeserializing = false;
sp.ThrowOnErrorSerializing = true;
for (int i = 0; i < attributes.Length; i++) {
if (attributes [i] is DefaultSettingValueAttribute) {
sp.DefaultValue = ((DefaultSettingValueAttribute) attributes [i]).Value;
defaultAssigned = true;
} else if (attributes [i] is SettingsProviderAttribute) {
Type providerType = HttpApplication.LoadType (((SettingsProviderAttribute) attributes [i]).ProviderTypeName);
sp.Provider = (SettingsProvider) Activator.CreateInstance (providerType);
sp.Provider.Initialize (null, null);
} else if (attributes [i] is SettingsSerializeAsAttribute) {
sp.SerializeAs = ((SettingsSerializeAsAttribute) attributes [i]).SerializeAs;
} else if (attributes [i] is SettingsAllowAnonymousAttribute) {
sp.Attributes ["AllowAnonymous"] = ((SettingsAllowAnonymousAttribute) attributes [i]).Allow;
} else if (attributes [i] is CustomProviderDataAttribute) {
sp.Attributes ["CustomProviderData"] = ((CustomProviderDataAttribute) attributes [i]).CustomProviderData;
} else if (attributes [i] is ApplicationScopedSettingAttribute ||
attributes [i] is UserScopedSettingAttribute ||
attributes [i] is SettingsDescriptionAttribute ||
attributes [i] is SettingAttribute)
attDict.Add (attributes [i].GetType (), attributes [i]);
}
if (sp.Provider == null)
sp.Provider = ProfileManager.Provider;
if (sp.Attributes ["AllowAnonymous"] == null)
sp.Attributes ["AllowAnonymous"] = false;
if (!defaultAssigned && sp.PropertyType == typeof (string) && sp.DefaultValue == null)
sp.DefaultValue = String.Empty;
return sp;
}
static SettingsProperty CreateSettingsProperty (ProfileGroupSettings pgs, ProfilePropertySettings pps)
{
string name = ((pgs == null) ? String.Empty : pgs.Name + ".") + pps.Name;
SettingsProperty sp = new SettingsProperty (name);
sp.Attributes.Add ("AllowAnonymous", pps.AllowAnonymous);
sp.DefaultValue = pps.DefaultValue;
sp.IsReadOnly = pps.ReadOnly;
sp.Provider = ProfileManager.Provider;
sp.ThrowOnErrorDeserializing = false;
sp.ThrowOnErrorSerializing = true;
if (pps.Type.Length == 0 || pps.Type == "string")
sp.PropertyType = typeof (string);
else
sp.PropertyType = GetPropertyType (pgs, pps);
switch (pps.SerializeAs) {
case SerializationMode.Binary:
sp.SerializeAs = SettingsSerializeAs.Binary;
break;
case SerializationMode.ProviderSpecific:
sp.SerializeAs = SettingsSerializeAs.ProviderSpecific;
break;
case SerializationMode.String:
sp.SerializeAs = SettingsSerializeAs.String;
break;
case SerializationMode.Xml:
sp.SerializeAs = SettingsSerializeAs.Xml;
break;
}
return sp;
}
public void Initialize (string username, bool isAuthenticated)
{
_settingsContext = new SettingsContext ();
_settingsContext.Add ("UserName", username);
_settingsContext.Add ("IsAuthenticated", isAuthenticated);
SettingsProviderCollection spc = new SettingsProviderCollection();
spc.Add (ProfileManager.Provider);
base.Initialize (Context, ProfileBase.Properties, spc);
}
public override void Save ()
{
if (IsDirty) {
ProfileManager.Provider.SetPropertyValues (_settingsContext, _propertiyValues);
}
}
public bool IsAnonymous {
get {
return !(bool) _settingsContext ["IsAuthenticated"];
}
}
public bool IsDirty {
get {
return _dirty;
}
}
public DateTime LastActivityDate {
get {
return _lastActivityDate;
}
}
public DateTime LastUpdatedDate {
get {
return _lastUpdatedDate;
}
}
public new static SettingsPropertyCollection Properties {
get {
if (_properties == null)
InitProperties ();
return _properties;
}
}
public string UserName {
get {
return (string) _settingsContext ["UserName"];
}
}
}
}
#endif
|