The Labs \ Source Viewer \ SSCLI \ System.Web \ AspNetHostingPermission

  1. //------------------------------------------------------------------------------
  2. // <copyright file="AspNetHostingPermission.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Web
  16. {
  17.    
  18.     using System.Security;
  19.     using System.Security.Permissions;
  20.     using System.Globalization;
  21.    
  22.     //NOTE: While AspNetHostingPermissionAttribute resides in System.DLL,
  23.     // no classes from that DLL are able to make declarative usage of AspNetHostingPermission.
  24.    
  25.     [Serializable()]
  26.     public enum AspNetHostingPermissionLevel
  27.     {
  28.         None = 100,
  29.         Minimal = 200,
  30.         Low = 300,
  31.         Medium = 400,
  32.         High = 500,
  33.         Unrestricted = 600
  34.     }
  35.    
  36.     [AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)]
  37.     [Serializable()]
  38.     public sealed class AspNetHostingPermissionAttribute : CodeAccessSecurityAttribute
  39.     {
  40.         AspNetHostingPermissionLevel _level;
  41.        
  42.         public AspNetHostingPermissionAttribute(SecurityAction action) : base(action)
  43.         {
  44.             _level = AspNetHostingPermissionLevel.None;
  45.         }
  46.        
  47.         public AspNetHostingPermissionLevel Level {
  48.             get { return _level; }
  49.            
  50.             set {
  51.                 AspNetHostingPermission.VerifyAspNetHostingPermissionLevel(value, "Level");
  52.                 _level = value;
  53.             }
  54.         }
  55.        
  56.         public override IPermission CreatePermission()
  57.         {
  58.             if (Unrestricted) {
  59.                 return new AspNetHostingPermission(PermissionState.Unrestricted);
  60.             }
  61.             else {
  62.                 return new AspNetHostingPermission(_level);
  63.             }
  64.         }
  65.     }
  66.    
  67.    
  68.     /// <devdoc>
  69.     /// <para>
  70.     /// </para>
  71.     /// </devdoc>
  72.     [Serializable()]
  73.     public sealed class AspNetHostingPermission : CodeAccessPermission, IUnrestrictedPermission
  74.     {
  75.         AspNetHostingPermissionLevel _level;
  76.        
  77.         static internal void VerifyAspNetHostingPermissionLevel(AspNetHostingPermissionLevel level, string arg)
  78.         {
  79.             switch (level) {
  80.                 case AspNetHostingPermissionLevel.Unrestricted:
  81.                 case AspNetHostingPermissionLevel.High:
  82.                 case AspNetHostingPermissionLevel.Medium:
  83.                 case AspNetHostingPermissionLevel.Low:
  84.                 case AspNetHostingPermissionLevel.Minimal:
  85.                 case AspNetHostingPermissionLevel.None:
  86.                     break;
  87.                 default:
  88.                    
  89.                     throw new ArgumentException(arg);
  90.                     break;
  91.             }
  92.         }
  93.        
  94.         /// <devdoc>
  95.         /// <para>
  96.         /// Creates a new instance of the System.Net.AspNetHostingPermission
  97.         /// class that passes all demands or that fails all demands.
  98.         /// </para>
  99.         /// </devdoc>
  100.         public AspNetHostingPermission(PermissionState state)
  101.         {
  102.             switch (state) {
  103.                 case PermissionState.Unrestricted:
  104.                     _level = AspNetHostingPermissionLevel.Unrestricted;
  105.                     break;
  106.                 case PermissionState.None:
  107.                    
  108.                     _level = AspNetHostingPermissionLevel.None;
  109.                     break;
  110.                 default:
  111.                    
  112.                     throw new ArgumentException(SR.GetString(SR.InvalidArgument, state.ToString(), "state"));
  113.                     break;
  114.             }
  115.         }
  116.        
  117.         public AspNetHostingPermission(AspNetHostingPermissionLevel level)
  118.         {
  119.             VerifyAspNetHostingPermissionLevel(level, "level");
  120.             _level = level;
  121.         }
  122.        
  123.         public AspNetHostingPermissionLevel Level {
  124.             get { return _level; }
  125.            
  126.             set {
  127.                 VerifyAspNetHostingPermissionLevel(value, "Level");
  128.                 _level = value;
  129.             }
  130.         }
  131.        
  132.         // IUnrestrictedPermission interface methods
  133.         /// <devdoc>
  134.         /// <para>
  135.         /// Checks the overall permission state of the object.
  136.         /// </para>
  137.         /// </devdoc>
  138.         public bool IsUnrestricted()
  139.         {
  140.             return _level == AspNetHostingPermissionLevel.Unrestricted;
  141.         }
  142.        
  143.         // IPermission interface methods
  144.         /// <devdoc>
  145.         /// <para>
  146.         /// Creates a copy of a System.Net.AspNetHostingPermission
  147.         /// </para>
  148.         /// </devdoc>
  149.         public override IPermission Copy()
  150.         {
  151.             return new AspNetHostingPermission(_level);
  152.         }
  153.        
  154.         /// <devdoc>
  155.         /// <para>Returns the logical union between two System.Net.AspNetHostingPermission instances.</para>
  156.         /// </devdoc>
  157.         public override IPermission Union(IPermission target)
  158.         {
  159.             if (target == null) {
  160.                 return Copy();
  161.             }
  162.            
  163.             if (target.GetType() != typeof(AspNetHostingPermission)) {
  164.                 throw new ArgumentException(SR.GetString(SR.InvalidArgument, target == null ? "null" : target.ToString(), "target"));
  165.             }
  166.            
  167.             AspNetHostingPermission other = (AspNetHostingPermission)target;
  168.             if (Level >= other.Level) {
  169.                 return new AspNetHostingPermission(Level);
  170.             }
  171.             else {
  172.                 return new AspNetHostingPermission(other.Level);
  173.             }
  174.         }
  175.        
  176.         /// <devdoc>
  177.         /// <para>Returns the logical intersection between two System.Net.AspNetHostingPermission instances.</para>
  178.         /// </devdoc>
  179.         public override IPermission Intersect(IPermission target)
  180.         {
  181.             if (target == null) {
  182.                 return null;
  183.             }
  184.            
  185.             if (target.GetType() != typeof(AspNetHostingPermission)) {
  186.                 throw new ArgumentException(SR.GetString(SR.InvalidArgument, target == null ? "null" : target.ToString(), "target"));
  187.             }
  188.            
  189.             AspNetHostingPermission other = (AspNetHostingPermission)target;
  190.             if (Level <= other.Level) {
  191.                 return new AspNetHostingPermission(Level);
  192.             }
  193.             else {
  194.                 return new AspNetHostingPermission(other.Level);
  195.             }
  196.         }
  197.        
  198.        
  199.         /// <devdoc>
  200.         /// <para>Compares two System.Net.AspNetHostingPermission instances.</para>
  201.         /// </devdoc>
  202.         public override bool IsSubsetOf(IPermission target)
  203.         {
  204.             if (target == null) {
  205.                 return _level == AspNetHostingPermissionLevel.None;
  206.             }
  207.            
  208.             if (target.GetType() != typeof(AspNetHostingPermission)) {
  209.                 throw new ArgumentException(SR.GetString(SR.InvalidArgument, target == null ? "null" : target.ToString(), "target"));
  210.             }
  211.            
  212.             AspNetHostingPermission other = (AspNetHostingPermission)target;
  213.             return Level <= other.Level;
  214.         }
  215.        
  216.         /// <devdoc>
  217.         /// </devdoc>
  218.         public override void FromXml(SecurityElement securityElement)
  219.         {
  220.             if (securityElement == null) {
  221.                 throw new ArgumentNullException(SR.GetString(SR.AspNetHostingPermissionBadXml, "securityElement"));
  222.             }
  223.            
  224.             if (!securityElement.Tag.Equals("IPermission")) {
  225.                 throw new ArgumentException(SR.GetString(SR.AspNetHostingPermissionBadXml, "securityElement"));
  226.             }
  227.            
  228.             string className = securityElement.Attribute("class");
  229.             if (className == null) {
  230.                 throw new ArgumentException(SR.GetString(SR.AspNetHostingPermissionBadXml, "securityElement"));
  231.             }
  232.            
  233.             if (className.IndexOf(this.GetType().FullName, StringComparison.Ordinal) < 0) {
  234.                 throw new ArgumentException(SR.GetString(SR.AspNetHostingPermissionBadXml, "securityElement"));
  235.             }
  236.            
  237.             string version = securityElement.Attribute("version");
  238.             if (string.Compare(version, "1", StringComparison.OrdinalIgnoreCase) != 0) {
  239.                 throw new ArgumentException(SR.GetString(SR.AspNetHostingPermissionBadXml, "version"));
  240.             }
  241.            
  242.             string level = securityElement.Attribute("Level");
  243.             if (level == null) {
  244.                 _level = AspNetHostingPermissionLevel.None;
  245.             }
  246.             else {
  247.                 _level = (AspNetHostingPermissionLevel)Enum.Parse(typeof(AspNetHostingPermissionLevel), level);
  248.             }
  249.         }
  250.        
  251.         /// <devdoc>
  252.         /// <para>[To be supplied.]</para>
  253.         /// </devdoc>
  254.         public override SecurityElement ToXml()
  255.         {
  256.             SecurityElement securityElement = new SecurityElement("IPermission");
  257.             securityElement.AddAttribute("class", this.GetType().FullName + ", " + this.GetType().Module.Assembly.FullName.Replace('"', '\''));
  258.             securityElement.AddAttribute("version", "1");
  259.             securityElement.AddAttribute("Level", Enum.GetName(typeof(AspNetHostingPermissionLevel), _level));
  260.             if (IsUnrestricted()) {
  261.                 securityElement.AddAttribute("Unrestricted", "true");
  262.             }
  263.            
  264.             return securityElement;
  265.         }
  266.     }
  267. }

Developer Fusion