Merge pull request #2916 from ludovic-henry/fix-40306
[mono.git] / mcs / class / System.Web / System.Web.Configuration_2.0 / AuthorizationRule.cs
index ab2a66661dea5f548d024d548c72370ac801cd8d..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,7 +48,8 @@ namespace System.Web.Configuration {
                static ConfigurationPropertyCollection properties;
 
                AuthorizationRuleAction action;
-
+               ConfigurationSaveMode saveMode = ConfigurationSaveMode.Full;
+                       
                static AuthorizationRule ()
                {
                        rolesProp = new ConfigurationProperty ("roles", typeof (StringCollection), null,
@@ -124,40 +127,53 @@ namespace System.Web.Configuration {
                        return hashCode;
                }
 
-               [MonoTODO]
-               protected override bool IsModified ()
+               [MonoTODO ("Not implemented")]
+               protected internal override bool IsModified ()
                {
-                       throw new NotImplementedException ();
+                       if (((CommaDelimitedStringCollection)Roles).IsModified || ((CommaDelimitedStringCollection)Users).IsModified || ((CommaDelimitedStringCollection)Verbs).IsModified)
+                               return true;
+
+                       return false;
+               }
+
+               void VerifyData ()
+               {
+                       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 ();
                }
 
                protected override void PreSerialize (XmlWriter writer)
                {
                        base.PreSerialize (writer);
 
-                       if (Roles.Count == 0 && Users.Count == 0)
-                               throw new ConfigurationErrorsException ("You must supply either a list of users or roles when creating an AuthorizationRule");
+                       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 ();
                }
 
-               protected override bool SerializeElement (XmlWriter writer, bool serializeCollectionKey)
+               protected internal override bool SerializeElement (XmlWriter writer, bool serializeCollectionKey)
                {
+                       if (saveMode != ConfigurationSaveMode.Full && !IsModified ())
+                               return true;
+                       
                        PreSerialize (writer);
 
                        writer.WriteStartElement (action == AuthorizationRuleAction.Allow ? "allow" : "deny");
@@ -173,16 +189,19 @@ namespace System.Web.Configuration {
                        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 {
@@ -208,13 +227,42 @@ namespace System.Web.Configuration {
                        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