2005-01-31 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System.Security.Policy / NetCodeGroup.cs
index 503c6e00f8b72104e2e55032c98acfd57bbe5640..d507be0efdc152e5a0ff2191f275971aef6e6b66 100644 (file)
@@ -28,9 +28,9 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System;
 using System.Collections;
 using System.Globalization;
+using System.Runtime.InteropServices;
 
 namespace System.Security.Policy {
 
@@ -43,6 +43,7 @@ namespace System.Security.Policy {
 
                private CodeGroupGrantScope _scope = CodeGroupGrantScope.Assembly;
                private Hashtable _rules = new Hashtable ();
+               private int _hashcode;
 #endif
 
                public NetCodeGroup (IMembershipCondition condition) 
@@ -51,8 +52,8 @@ namespace System.Security.Policy {
                }
 
                // for PolicyLevel (to avoid validation duplication)
-               internal NetCodeGroup (SecurityElement e
-                       : base (e
+               internal NetCodeGroup (SecurityElement e, PolicyLevel level)
+                       : base (e, level)
                {
                }
        
@@ -69,10 +70,15 @@ namespace System.Security.Policy {
                }
 
                public override string PermissionSetName {
+#if NET_2_0
+                       get { return "Same site Web"; }
+#else
                        get { return "Same site Web."; }
+#endif
                }
 
 #if NET_2_0
+               [ComVisible (false)]
                public CodeGroupGrantScope Scope {
                        get { return _scope; }
                        set { _scope = value; }
@@ -129,21 +135,90 @@ namespace System.Security.Policy {
                }
 
 #if NET_2_0
+               private bool Equals (CodeConnectAccess[] rules1, CodeConnectAccess[] rules2)
+               {
+                       for (int i=0; i < rules1.Length; i++) {
+                               bool found = false;
+                               for (int j=0; j < rules2.Length; j++) {
+                                       if (rules1 [i].Equals (rules2 [j])) {
+                                               found = true;
+                                               break;
+                                       }
+                               }
+                               if (!found)
+                                       return false;
+                       }
+                       return true;
+               }
+
+               public override bool Equals (object o)
+               {
+                       if (!base.Equals (o))
+                               return false;
+                       NetCodeGroup ncg = (o as NetCodeGroup);
+                       if (ncg == null) 
+                               return false;
+       
+                       // check rules
+                       foreach (DictionaryEntry de in _rules) {
+                               bool found = false;
+                               CodeConnectAccess[] ccas = (CodeConnectAccess[]) ncg._rules [de.Key];
+                               if (ccas != null)
+                                       found = Equals ((CodeConnectAccess[]) de.Value, ccas);
+                               else
+                                       found = (de.Value == null);
+
+                               if (!found)
+                                       return false;
+                       }
+                       return true;
+               }
+
                public DictionaryEntry[] GetConnectAccessRules ()
                {
                        DictionaryEntry[] result = new DictionaryEntry [_rules.Count];
                        _rules.CopyTo (result, 0);
                        return result;
                }
+
+               public override int GetHashCode ()
+               {
+                       if (_hashcode == 0) {
+                               _hashcode = base.GetHashCode ();
+                               foreach (DictionaryEntry de in _rules) {
+                                       CodeConnectAccess[] ccas = (CodeConnectAccess[]) de.Value;
+                                       if (ccas != null) {
+                                               foreach (CodeConnectAccess cca in ccas) {
+                                                       _hashcode ^= cca.GetHashCode ();
+                                               }
+                                       }
+                               }
+                       }
+                       return _hashcode;
+               }
 #endif
 
-               [MonoTODO]
                public override PolicyStatement Resolve (Evidence evidence)
                {
                        if (evidence == null) 
                                throw new ArgumentNullException ("evidence");
 
-                       throw new NotImplementedException ();
+                       if (!MembershipCondition.Check (evidence))
+                               return null;
+
+                       PolicyStatement pst = this.PolicyStatement.Copy ();
+                       
+                       if (this.Children.Count > 0) {
+                               foreach (CodeGroup child_cg in this.Children) {
+                                       PolicyStatement child_pst = child_cg.Resolve (evidence);
+                                       if (child_pst != null) {
+                                               foreach (IPermission perm in child_pst.PermissionSet) {
+                                                       pst.PermissionSet.AddPermission (perm);
+                                               }
+                                       }
+                               }
+                       }
+                       return pst;
                }
 
 #if NET_2_0
@@ -156,7 +231,7 @@ namespace System.Security.Policy {
                public override CodeGroup ResolveMatchingCodeGroups (Evidence evidence) 
                {
                        if (evidence == null)
-                               throw new ArgumentNullException ();
+                               throw new ArgumentNullException ("evidence");
                        
                        CodeGroup return_group = null;
                        if (MembershipCondition.Check (evidence)) {