2009-06-30 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / corlib / System.Security.Policy / CodeGroup.cs
index 6dd50ed63812013dba6890345fe6ba53b2a114d4..2e8d833974d99f065461ab1411ae49015124b30d 100644 (file)
@@ -6,7 +6,7 @@
 //     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2001 Nick Drochak, All rights reserved.
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2006 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
 using System.Collections;
 using System.Globalization;
 using System.Reflection;
-using System.Security.Policy;
+using System.Runtime.InteropServices;
 using System.Security.Permissions;
 
 namespace System.Security.Policy {
 
        [Serializable]
+#if NET_2_0
+       [ComVisible (true)]
+#endif
        public abstract class CodeGroup {
                PolicyStatement m_policy;
                IMembershipCondition m_membershipCondition;
                string m_description;
                string m_name;
                ArrayList m_children = new ArrayList();
-               PolicyLevel m_level;
+//             PolicyLevel m_level;
 
+#if NET_2_0
+               protected CodeGroup (IMembershipCondition membershipCondition, PolicyStatement policy)
+#else
                public CodeGroup (IMembershipCondition membershipCondition, PolicyStatement policy)
+#endif
                {
                        if (null == membershipCondition)
                                throw new ArgumentNullException ("membershipCondition");
 
-                       m_policy = policy;
-                       m_membershipCondition = membershipCondition;
+                       if (policy != null)
+                               m_policy = policy.Copy ();
+                       m_membershipCondition = membershipCondition.Copy ();
                }
 
                // for PolicyLevel (to avoid validation duplication)
-               internal CodeGroup (SecurityElement e) 
+               internal CodeGroup (SecurityElement e, PolicyLevel level
                {
-                       FromXml (e);
+                       FromXml (e, level);
                }
 
                // abstract
@@ -115,6 +123,8 @@ namespace System.Security.Policy {
 
                public virtual string PermissionSetName {
                        get {
+                               if (m_policy == null)
+                                       return null;
                                if (m_policy.PermissionSet is Security.NamedPermissionSet)
                                        return ((NamedPermissionSet)(m_policy.PermissionSet)).Name;
                                return null;
@@ -126,15 +136,16 @@ namespace System.Security.Policy {
                        if (null == group)
                                throw new ArgumentNullException ("group");
 
-                       m_children.Add(group);
+                       m_children.Add (group.Copy ());
                }
 
                public override bool Equals (object o)
                {
-                       if (!(o is CodeGroup))
+                       CodeGroup cg = (o as CodeGroup);
+                       if (cg == null)
                                return false;
 
-                       return Equals ((CodeGroup)o, false);
+                       return Equals (cg, false);
                }
 
                public bool Equals (CodeGroup cg, bool compareChildren)
@@ -145,10 +156,7 @@ namespace System.Security.Policy {
                        if (cg.Description != this.Description)
                                return false;
 
-// FIXME: this compiles with CSC. Didn't succeed at creating a smaller/different test case :(
-//                     if (!cg.MembershipCondition.Equals (m_membershipCondition))
-                       if (((object) cg.MembershipCondition).ToString () !=
-                           ((object) m_membershipCondition).ToString ())
+                       if (!cg.MembershipCondition.Equals (m_membershipCondition))
                                return false;
 
                        if (compareChildren) {
@@ -190,21 +198,28 @@ namespace System.Security.Policy {
                                throw new ArgumentNullException("e");
 
                        PermissionSet ps = null;
-                       SecurityElement pset = e.SearchForChildByTag ("PermissionSet");
-                       if (pset != null) {
-                               Type classType = Type.GetType (pset.Attribute ("class"));
-                               ps = (PermissionSet) Activator.CreateInstance (classType, true);
-                               ps.FromXml (pset);
+                       string psetname = e.Attribute ("PermissionSetName");
+                       if ((psetname != null) && (level != null)) {
+                               ps = level.GetNamedPermissionSet (psetname);
+                       }
+                       else {
+                               SecurityElement pset = e.SearchForChildByTag ("PermissionSet");
+                               if (pset != null) {
+                                       Type classType = Type.GetType (pset.Attribute ("class"));
+                                       ps = (PermissionSet) Activator.CreateInstance (classType, true);
+                                       ps.FromXml (pset);
+                               }
+                               else {
+                                       ps = new PermissionSet (new PermissionSet (PermissionState.None));
+                               }
                        }
-                       else
-                               ps = new NamedPermissionSet ("Nothing", new PermissionSet (PermissionState.None));
                        m_policy = new PolicyStatement (ps);
 
                        m_children.Clear ();
                        if ((e.Children != null) && (e.Children.Count > 0)) {
                                foreach (SecurityElement se in e.Children) {
                                        if (se.Tag == "CodeGroup") {
-                                               this.AddChild (CodeGroup.CreateFromXml (se));
+                                               this.AddChild (CodeGroup.CreateFromXml (se, level));
                                        }
                                }
                        }
@@ -224,7 +239,7 @@ namespace System.Security.Policy {
                        m_description = e.Attribute("Description");
 
                        // seems like we might need this to Resolve() in subclasses
-                       m_level = level;
+                       //m_level = level;
 
                        ParseXml (e, level);
                }
@@ -269,7 +284,7 @@ namespace System.Security.Policy {
 
                // internal stuff
 
-               internal static CodeGroup CreateFromXml (SecurityElement se) 
+               internal static CodeGroup CreateFromXml (SecurityElement se, PolicyLevel level
                {
                        string fullClassName = se.Attribute ("class");
                        string className = fullClassName;
@@ -287,16 +302,18 @@ namespace System.Security.Policy {
                        // much faster than calling Activator.CreateInstance
                        switch (className) {
                                case "FileCodeGroup":
-                                       return new FileCodeGroup (se);
+                                       return new FileCodeGroup (se, level);
                                case "FirstMatchCodeGroup":
-                                       return new FirstMatchCodeGroup (se);
+                                       return new FirstMatchCodeGroup (se, level);
                                case "NetCodeGroup":
-                                       return new NetCodeGroup (se);
+                                       return new NetCodeGroup (se, level);
                                case "UnionCodeGroup":
-                                       return new UnionCodeGroup (se);
+                                       return new UnionCodeGroup (se, level);
                                default: // unknown
                                        Type classType = Type.GetType (fullClassName);
-                                       return (CodeGroup) Activator.CreateInstance (classType, true);
+                                       CodeGroup cg = (CodeGroup) Activator.CreateInstance (classType, true);
+                                       cg.FromXml (se, level);
+                                       return cg;
                        }
                }
        }