Merge pull request #2916 from ludovic-henry/fix-40306
[mono.git] / mcs / class / System.Web / System.Web.Configuration_2.0 / AuthorizationRule.cs
index baca8cee351bf3a37ea64a8cb0259785fdd83b04..f5ebd6cd2ea6e0b6f3d6f0e03297d7552a59b8ee 100644 (file)
 
 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 {
 
@@ -46,12 +48,22 @@ 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);
@@ -62,6 +74,9 @@ namespace System.Web.Configuration {
                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)
@@ -112,56 +127,81 @@ namespace System.Web.Configuration {
                        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 {
@@ -169,37 +209,60 @@ namespace System.Web.Configuration {
                        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