2004-04-02 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / corlib / System.Security / CodeAccessPermission.cs
index 3994f785dcfde785b1d01b1f5792a8757d1c22a8..ce56ae221659089b29e859714f6017f0a542b941 100755 (executable)
 //
 // System.Security.CodeAccessPermission.cs
 //
-// Author:
-//   Miguel de Icaza (miguel@ximian.com)
+// Authors:
+//     Miguel de Icaza (miguel@ximian.com)
+//     Nick Drochak, ndrochak@gol.com
+//     Sebastien Pouliot (spouliot@motus.com)
 //
 // (C) Ximian, Inc. http://www.ximian.com
+// Copyright (C) 2001 Nick Drochak, All Rights Reserved
+// Portions (C) 2004 Motus Technologies Inc. (http://www.motus.com)
 //
 
+using System.Globalization;
+using System.Security.Permissions;
+using System.Text;
+
 namespace System.Security {
 
        [Serializable]
-       public abstract class CodeAccessPermission : IPermission, ISecurityEncodable, IStackWalk
-       {
-               ///<summary>Constructs a new instance of the System.Security.CodeAccessPermission class.</summary>
-               protected CodeAccessPermission(){}
-
-               ///<summary> Asserts that calling code can access the resource identified by the current instance through the code that      calls this method, even if callers have not been granted permission to      access the resource. </summary>
-               ///<exception cref="System.Security.SecurityException">The calling code does not have System.Security.Permissions.SecurityPermissionFlag.Assertion. </exception>
-               public void Assert()
+       public abstract class CodeAccessPermission : IPermission, ISecurityEncodable, IStackWalk {
+
+               protected CodeAccessPermission () {}
+
+               // LAMESPEC: Documented as virtual
+               [MonoTODO("SecurityStackFrame not ready")]
+               public void Assert ()
                {
+                       // throw a SecurityException if Assertion is denied
+                       new SecurityPermission (SecurityPermissionFlag.Assertion).Demand ();
+//                     SecurityStackFrame.Current.Assert = this;
                }
 
-               ///<summary> Returns a System.Security.CodeAccessPermission containing the same values as the current instance.</summary>
-               ///<returns> A new System.Security.CodeAccessPermission instance that is value equal to the current instance.</returns>
-               public abstract IPermission Copy();
+               public abstract IPermission Copy ();
+
+               // LAMESPEC: Documented as virtual
+               [MonoTODO("MS contralize demands, but I think we should branch back into indivual permission classes.")]
+               public void Demand ()
+               {
+                       IBuiltInPermission perm = (this as IBuiltInPermission);
+                       if (perm == null)
+                               return; // not sure about this :(
+
+                       // TODO : Loop the stack
+                       switch (perm.GetTokenIndex ()) {
+                               case 0: // EnvironmentPermission
+                                       // TODO
+                                       break;
+                               case 1: // FileDialogPermission
+                                       // TODO
+                                       break;
+                               case 2: // FileIOPermission
+                                       // TODO
+                                       break;
+                               case 3: // IsolatedStorageFilePermission
+                                       // TODO
+                                       break;
+                               case 4: // ReflectionPermission
+                                       // TODO
+                                       break;
+                               case 5: // RegistryPermission
+                                       // TODO
+                                       break;
+                               case 6: // SecurityPermission
+                                       // TODO
+                                       break;
+                               case 7: // UIPermission
+                                       // TODO
+                                       break;
+                               case 8: // PrincipalPermission
+                                       // TODO
+                                       break;
+                               case 9: // PublisherIdentityPermission
+                                       // TODO
+                                       break;
+                               case 10: // SiteIdentityPermission
+                                       // TODO
+                                       break;
+                               case 11: // StrongNameIdentityPermission
+                                       // TODO
+                                       break;
+                               case 12: // UrlIdentityPermission
+                                       // TODO
+                                       break;
+                               case 13: // ZoneIdentityPermission
+                                       // TODO
+                                       break;
+                               default:
+                                       string message = String.Format (Locale.GetText ("Unknown IBuiltInPermission #{0}"), perm.GetTokenIndex ());
+                                       throw new SecurityException (message);
+                       }
+               }
 
-               ///<summary>Forces a System.Security.SecurityException if all callers do not have the permission specified by the current instance.</summary>
-               ///<exception cref="System.Security.SecurityException"> A caller does not have the permission specified by the current instance. A caller has called System.Security.CodeAccessPermission.Deny for the resource protected by the current instance. </exception>
-               public void Demand()
+               // LAMESPEC: Documented as virtual
+               [MonoTODO("SecurityStackFrame not ready")]
+               public void Deny ()
                {
+//                     SecurityStackFrame.Current.Deny = this;
                }
 
-               ///<summary> Denies access to the resources specified by the current instance through the code that calls this method.</summary>
-               public void Deny(){}
-
-               ///<summary> Reconstructs the state of a System.Security.CodeAccessPermission object using the specified XML encoding.</summary>
-               ///<param name="elem">A System.Security.SecurityElement instance containing the XML encoding to use to reconstruct the state of a System.Security.CodeAccessPermission object.</param>
-               ///<exception cref="System.ArgumentException">elem does not contain the XML encoding for a instance of the same type as the current instance.The version number of elem is not valid.</exception>
-               public abstract void FromXml(SecurityElement elem);
-
-               ///<summary> Returns a System.Security.CodeAccessPermission object that is the intersection of the current instance and the specified object.</summary>
-               ///<param name="target">A System.Security.CodeAccessPermission instance to intersect with the current instance.</param>
-               ///<returns> A new System.Security.CodeAccessPermission instance that represents the intersection of the current instance andtarget. If the intersection is empty or target is null, returns null. If the  current instance is unrestricted, returns a copy of target. Iftarget is unrestricted, returns a copy of the current instance.</returns>
-               ///<exception cref="System.ArgumentException">target is not null and is not a System.Security.CodeAccessPermission object.</exception>
-               public abstract IPermission Intersect(IPermission target);
-
-               ///<summary>Determines whether the current instance is a subset of the specified      object.</summary>
-               ///<param name="target">A System.Security.CodeAccessPermission instance that is to be tested for the subset relationship.</param>
-               ///<returns>true if the current instance is a   subset of target; otherwise, false. If the current   instance is unrestricted, and target is not, returnsfalse. If target is unrestricted, returns true.</returns>
-               ///<exception cref="System.ArgumentException">target is not null and is not of type System.Security.CodeAccessPermission.</exception>
-               public abstract bool IsSubsetOf(IPermission target);
-
-               ///<summary> Returns the XML representation of the state of the current instance.</summary>
-               ///<returns> A System.String containing the XML  representation of the state of the current instance.</returns>
-               public override string ToString()
+               public abstract void FromXml (SecurityElement elem);
+
+               public abstract IPermission Intersect (IPermission target);
+
+               public abstract bool IsSubsetOf (IPermission target);
+
+               public override string ToString ()
                {
                        SecurityElement elem = ToXml ();
-                       return elem == null ? null : elem.ToString ();
+                       return elem.ToString ();
                }
 
-               ///<summary> Returns the XML encoding of the current instance.</summary>
-               ///<returns>A System.Security.SecurityElement containing an XML encoding of the state of the current instance.</returns>
-               public abstract SecurityElement ToXml();
+               public abstract SecurityElement ToXml ();
 
-               ///<summary> Returns a System.Security.CodeAccessPermission object that is the union of the current instance and the specified object.</summary>
-               ///<param name="other">A System.Security.IPermission object of the same type as the current instance to be combined with the current instance.</param>
-               ///<returns>If other is null, returns a copy of the current  instance using the System.Security.IPermission.Copy method.</returns>
-               ///<exception cref="System.ArgumentException">other is not of type System.Security.CodeAccessPermission.</exception>
-               ///<exception cref="System.NotSupportedException">other is not null.</exception>
-               public virtual IPermission Union(IPermission other)
+               public virtual IPermission Union (IPermission other)
                {
-                   if (!(other is System.Security.CodeAccessPermission))
-                   {
-                       throw new System.ArgumentException(); // other is not of type System.Security.CodeAccessPermission.
-                   }
-                   if (null != other)
-                   {
-                       throw new System.NotSupportedException(); // other is not null.
-                   }
-                   return null;
+                       if (null != other)
+                               throw new System.NotSupportedException (); // other is not null.
+                       return null;
                }
 
-               public void PermitOnly(){}
+               // LAMESPEC: Documented as virtual
+               [MonoTODO("SecurityStackFrame not ready")]
+               public void PermitOnly ()
+               {
+//                     SecurityStackFrame.Current.PermitOnly = this;
+               }
+
+               [MonoTODO("SecurityStackFrame not ready")]
+               public static void RevertAll ()
+               {
+//                     SecurityStackFrame.Current.RevertAll ();
+               }
+
+               [MonoTODO("SecurityStackFrame not ready")]
+               public static void RevertAssert () 
+               {
+//                     SecurityStackFrame.Current.RevertAssert ();
+               }
+
+               [MonoTODO("SecurityStackFrame not ready")]
+               public static void RevertDeny ()
+               {
+//                     SecurityStackFrame.Current.RevertDeny ();
+               }
+
+               [MonoTODO("SecurityStackFrame not ready")]
+               public static void RevertPermitOnly () 
+               {
+//                     SecurityStackFrame.Current.RevertPermitOnly ();
+               }
+
+               // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
+               internal SecurityElement Element (object o, int version) 
+               {
+                       SecurityElement se = new SecurityElement ("IPermission");
+                       Type type = this.GetType ();
+                       StringBuilder asmName = new StringBuilder (type.Assembly.ToString ());
+                       asmName.Replace ('\"', '\'');
+                       se.AddAttribute ("class", type.FullName + ", " + asmName);
+                       se.AddAttribute ("version", version.ToString ());
+                       return se;
+               }
        }
 }