using System;
using System.Collections.Specialized;
+using System.Security.Principal;
using System.Configuration;
using System.ComponentModel;
using System.Xml;
+using System.Globalization;
+using System.Web.Util;
-#if NET_2_0
namespace System.Web.Configuration {
static ConfigurationPropertyCollection properties;
AuthorizationRuleAction action;
-
+ ConfigurationSaveMode saveMode = ConfigurationSaveMode.Full;
+
static AuthorizationRule ()
{
- rolesProp = new ConfigurationProperty ("roles", typeof (StringCollection));
- usersProp = new ConfigurationProperty ("users", typeof (StringCollection));
- verbsProp = new ConfigurationProperty ("verbs", typeof (StringCollection));
+ rolesProp = new ConfigurationProperty ("roles", typeof (StringCollection), null,
+ PropertyHelper.CommaDelimitedStringCollectionConverter,
+ PropertyHelper.DefaultValidator,
+ ConfigurationPropertyOptions.None);
+ usersProp = new ConfigurationProperty ("users", typeof (StringCollection), null,
+ PropertyHelper.CommaDelimitedStringCollectionConverter,
+ PropertyHelper.DefaultValidator,
+ ConfigurationPropertyOptions.None);
+ verbsProp = new ConfigurationProperty ("verbs", typeof (StringCollection), null,
+ PropertyHelper.CommaDelimitedStringCollectionConverter,
+ PropertyHelper.DefaultValidator,
+ ConfigurationPropertyOptions.None);
properties = new ConfigurationPropertyCollection ();
properties.Add (rolesProp);
public AuthorizationRule (AuthorizationRuleAction action)
{
this.action = action;
+ base[rolesProp] = new CommaDelimitedStringCollection ();
+ base[usersProp] = new CommaDelimitedStringCollection ();
+ base[verbsProp] = new CommaDelimitedStringCollection ();
}
public override bool Equals (object obj)
return hashCode;
}
- [MonoTODO]
- protected override bool IsModified ()
+ [MonoTODO ("Not implemented")]
+ protected internal override bool IsModified ()
+ {
+ if (((CommaDelimitedStringCollection)Roles).IsModified || ((CommaDelimitedStringCollection)Users).IsModified || ((CommaDelimitedStringCollection)Verbs).IsModified)
+ return true;
+
+ return false;
+ }
+
+ void VerifyData ()
{
- throw new NotImplementedException ();
+ if (Roles.Count == 0 && Users.Count == 0)
+ throw new ConfigurationErrorsException ("You must supply either a list of users or roles when creating an AuthorizationRule");
}
- [MonoTODO]
protected override void PostDeserialize ()
{
base.PostDeserialize();
+
+ VerifyData ();
}
- [MonoTODO]
protected override void PreSerialize (XmlWriter writer)
{
base.PreSerialize (writer);
+
+ VerifyData ();
}
- [MonoTODO]
- protected override void Reset (ConfigurationElement parentElement)
+ protected internal override void Reset (ConfigurationElement parentElement)
{
+ AuthorizationRule r = (AuthorizationRule)parentElement;
+ Action = r.Action;
+
base.Reset (parentElement);
}
- [MonoTODO]
- protected override void ResetModified ()
+ protected internal override void ResetModified ()
{
base.ResetModified ();
}
- [MonoTODO]
- protected override bool SerializeElement (XmlWriter writer, bool serializeCollectionKey)
+ protected internal override bool SerializeElement (XmlWriter writer, bool serializeCollectionKey)
{
- bool ret = base.SerializeElement (writer, serializeCollectionKey);
+ if (saveMode != ConfigurationSaveMode.Full && !IsModified ())
+ return true;
+
+ PreSerialize (writer);
+
+ writer.WriteStartElement (action == AuthorizationRuleAction.Allow ? "allow" : "deny");
+ if (Roles.Count > 0)
+ writer.WriteAttributeString ("roles", Roles.ToString());
+ if (Users.Count > 0)
+ writer.WriteAttributeString ("users", Users.ToString());
+ if (Verbs.Count > 0)
+ writer.WriteAttributeString ("verbs", Verbs.ToString());
- /* XXX more here? .. */
+ writer.WriteEndElement ();
- return ret;
+ return true;
}
- [MonoTODO]
- protected override void SetReadOnly ()
+ protected internal override void SetReadOnly ()
{
base.SetReadOnly();
}
- [MonoTODO]
- protected override void Unmerge (ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
+ protected internal override void Unmerge (ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
{
base.Unmerge (sourceElement, parentElement, saveMode);
+ this.saveMode = saveMode;
+
+ AuthorizationRule source = sourceElement as AuthorizationRule;
+ if (source != null)
+ this.action = source.Action;
}
public AuthorizationRuleAction Action {
set { action = value; }
}
-#if notyet
[TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
-#endif
[ConfigurationProperty ("roles")]
public StringCollection Roles {
get { return (StringCollection) base [rolesProp];}
}
-#if notyet
[TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
-#endif
[ConfigurationProperty ("users")]
public StringCollection Users {
get { return (StringCollection) base [usersProp];}
}
-#if notyet
[TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
-#endif
[ConfigurationProperty ("verbs")]
public StringCollection Verbs {
get { return (StringCollection) base [verbsProp];}
}
- protected override ConfigurationPropertyCollection Properties {
+ protected internal override ConfigurationPropertyCollection Properties {
get { return properties; }
}
+
+ internal bool CheckVerb (string verb)
+ {
+ foreach (string v in Verbs) {
+ if (String.Compare (v, verb, true, Helpers.InvariantCulture) == 0)
+ return true;
+ }
+ return false;
+ }
+
+ internal bool CheckUser (string user)
+ {
+ foreach (string u in Users) {
+ if (String.Compare (u, user, true, Helpers.InvariantCulture) == 0 ||
+ u == "*" ||
+ (u == "?" && user == ""))
+ return true;
+ }
+ return false;
+ }
+
+ internal bool CheckRole (IPrincipal user)
+ {
+ foreach (string r in Roles) {
+ if (user.IsInRole (r))
+ return true;
+ }
+ return false;
+ }
+
}
}
-#endif