2004-09-01 Sebastien Pouliot <sebastien@ximian.com>
authorSebastien Pouliot <sebastien@ximian.com>
Wed, 1 Sep 2004 12:25:44 +0000 (12:25 -0000)
committerSebastien Pouliot <sebastien@ximian.com>
Wed, 1 Sep 2004 12:25:44 +0000 (12:25 -0000)
* NamedPermissionSet.cs: FromXml now calls base class instead of an
internal method of PermissionSet. This should allow class to inherit
from NamedPermissionSet properly.
* PermissionSet.cs: Added an internal PolicyLevel property to allow
policy file class name resolution. Fixed IsEmpty to return true if
the list contains "empty" permissions. Fixed Copy to copy permissions
even for unrestricted sets (again because of IUnrestrictedPermission).
* SecurityManager.cs: Fixed Resolve(Evidence) because permission
classes Intersect methods can either return null or an empty
PermissionSet.  Fixed ResolvePolicy(Evidence[]) for null (NET_2_0).

svn path=/trunk/mcs/; revision=33151

mcs/class/corlib/System.Security/ChangeLog
mcs/class/corlib/System.Security/NamedPermissionSet.cs
mcs/class/corlib/System.Security/PermissionSet.cs
mcs/class/corlib/System.Security/SecurityManager.cs

index 85dcde31b1b9629082bc04b490a84b050f5f848f..51063f04d543d6e6405a4c069b6d3ad4702b16af 100755 (executable)
@@ -1,3 +1,16 @@
+2004-09-01  Sebastien Pouliot  <sebastien@ximian.com>
+
+       * NamedPermissionSet.cs: FromXml now calls base class instead of an
+       internal method of PermissionSet. This should allow class to inherit
+       from NamedPermissionSet properly.
+       * PermissionSet.cs: Added an internal PolicyLevel property to allow
+       policy file class name resolution. Fixed IsEmpty to return true if
+       the list contains "empty" permissions. Fixed Copy to copy permissions
+       even for unrestricted sets (again because of IUnrestrictedPermission).
+       * SecurityManager.cs: Fixed Resolve(Evidence) because permission 
+       classes Intersect methods can either return null or an empty 
+       PermissionSet.  Fixed ResolvePolicy(Evidence[]) for null (NET_2_0).
+
 2004-08-31  Sebastien Pouliot  <sebastien@ximian.com>
 
        * PermissionSet.cs: Fixed RemovePermission and Intersect (due to typos
index f534b7fe64caded2e6d433f5085b4cc621244b0f..b1328cc75bfe7c7f27b074b87c15c3988ed752e3 100644 (file)
@@ -102,7 +102,7 @@ namespace System.Security {
 
                public override void FromXml (SecurityElement e) 
                {
-                       FromXml (e, "NamedPermissionSet");
+                       base.FromXml (e);
                        // strangely it can import a null Name (bypassing property setter)
                        name = e.Attribute ("Name");
                        description = e.Attribute ("Description");
index 4ced2c6d25539a442c4ab080a17656ac07dc9bf2..b1c730c5de62aa8818f82c7a1455309905779076 100644 (file)
@@ -34,6 +34,7 @@ using System.Diagnostics;
 using System.Reflection;
 using System.Runtime.Serialization;
 using System.Security.Permissions;
+using System.Security.Policy;
 
 namespace System.Security {
 
@@ -46,6 +47,7 @@ namespace System.Security {
                private PermissionState state;
                private ArrayList list;
                private int _hashcode;
+               private PolicyLevel _policyLevel;
 
                // constructors
 
@@ -188,8 +190,8 @@ namespace System.Security {
                {
                }
 
-               // to be re-used by NamedPermissionSet (and other derived classes)
-               internal void FromXml (SecurityElement et, string className) 
+               [MonoTODO ("adjust class version with current runtime - unification")]
+               public virtual void FromXml (SecurityElement et)
                {
                        if (et == null)
                                throw new ArgumentNullException ("et");
@@ -197,33 +199,33 @@ namespace System.Security {
                                string msg = String.Format ("Invalid tag {0} expected {1}", et.Tag, tagName);
                                throw new ArgumentException (msg, "et");
                        }
-//                     if (!et.Attribute ("class").EndsWith (className))
-//                             throw new ArgumentException ("not " + className);
-// version isn't checked
-//                     if (et.Attribute ("version") != "1")
-//                             throw new ArgumentException ("wrong version");
 
                        if (CodeAccessPermission.IsUnrestricted (et))
                                state = PermissionState.Unrestricted;
                        else
                                state = PermissionState.None;
-               }
 
-               [MonoTODO ("adjust class version with current runtime")]
-               public virtual void FromXml (SecurityElement et)
-               {
                        list.Clear ();
-                       FromXml (et, tagName);
                        if (et.Children != null) {
                                foreach (SecurityElement se in et.Children) {
                                        string className = se.Attribute ("class");
-                                       // TODO: adjust class version with current runtime
+                                       if (className == null) {
+                                               throw new ArgumentException (Locale.GetText (
+                                                       "No permission class is specified."));
+                                       }
+                                       if (Resolver != null) {
+                                               // policy class names do not have to be fully qualified
+                                               className = Resolver.ResolveClassName (className);
+                                       }
+                                       // TODO: adjust class version with current runtime (unification)
                                        // http://blogs.msdn.com/shawnfa/archive/2004/08/05/209320.aspx
                                        Type classType = Type.GetType (className);
-                                       object [] psNone = new object [1] { PermissionState.None };
-                                       IPermission p = (IPermission) Activator.CreateInstance (classType, psNone);
-                                       p.FromXml (se);
-                                       list.Add (p);
+                                       if (classType != null) {
+                                               object [] psNone = new object [1] { PermissionState.None };
+                                               IPermission p = (IPermission) Activator.CreateInstance (classType, psNone);
+                                               p.FromXml (se);
+                                               list.Add (p);
+                                       }
                                }
                        }
                }
@@ -370,7 +372,27 @@ namespace System.Security {
                        // note: Unrestricted isn't empty
                        if (state == PermissionState.Unrestricted)
                                return false;
-                       return ((list == null) || (list.Count == 0));
+                       if ((list == null) || (list.Count == 0))
+                               return true;
+                       // the set may include some empty permissions
+                       foreach (IPermission p in list) {
+                               // an empty permission only has a class and/or version attributes
+                               SecurityElement se = p.ToXml ();
+                               int n = se.Attributes.Count;
+                               if (n <= 2) {
+                                       if (se.Attribute ("class") != null)
+                                               n--;
+                                       if (se.Attribute ("version") != null)
+                                               n--;
+                                       if (n > 0)
+                                               return false;   // not class or version - then not empty
+                               }
+                               else {
+                                       // too much attributes - then not empty
+                                       return false;
+                               }
+                       }
+                       return true;
                }
 
                public virtual bool IsUnrestricted () 
@@ -415,9 +437,10 @@ namespace System.Security {
                        se.AddAttribute ("version", version.ToString ());
                        if (state == PermissionState.Unrestricted)
                                se.AddAttribute ("Unrestricted", "true");
-                       else {
-                               foreach (IPermission p in list)
-                                       se.AddChild (p.ToXml ());
+
+                       // required for permissions that do not implement IUnrestrictedPermission
+                       foreach (IPermission p in list) {
+                               se.AddChild (p.ToXml ());
                        }
                        return se;
                }
@@ -516,5 +539,12 @@ namespace System.Security {
                {
                }
 #endif
+
+               // internal
+
+               internal PolicyLevel Resolver {
+                       get { return _policyLevel; }
+                       set { _policyLevel = value; }
+               }
        }
 }
index b568533c73d6c9f3e3d9a96aeeee7d150d902e2f..4cc9a6f5d584af9f06ce5f4125ef346a474c633e 100644 (file)
@@ -167,10 +167,15 @@ namespace System.Security {
                                PolicyStatement pst = pl.Resolve (evidence);
                                if (pst != null) {
                                        if (ps == null)
-                                               ps = pst.PermissionSet;
+                                               ps = pst.PermissionSet; // for first time only
                                        else
                                                ps = ps.Intersect (pst.PermissionSet);
-       
+
+                                       // some permissions returns null, other returns an empty set
+                                       // sadly we must adjust for every variations :(
+                                       if (ps == null)
+                                               ps = new PermissionSet (PermissionState.None);
+
                                        if ((pst.Attributes & PolicyStatementAttribute.LevelFinal) == PolicyStatementAttribute.LevelFinal)
                                                break;
                                }
@@ -192,6 +197,9 @@ namespace System.Security {
 #if NET_2_0
                public static PermissionSet ResolvePolicy (Evidence[] evidences)
                {
+                       if (evidences == null)
+                               throw new PermissionSet (PermissionState.None);
+
                        // probably not optimal
                        PermissionSet ps = null;
                        foreach (Evidence evidence in evidences) {