The Labs \ Source Viewer \ SSCLI \ System.Configuration \ ConfigurationPermissionAttribute

  1. //------------------------------------------------------------------------------
  2. // <copyright file="ConfigurationPermission.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.Configuration
  16. {
  17.    
  18.     using System.Security;
  19.     using System.Security.Permissions;
  20.     using System.Globalization;
  21.    
  22.     [AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)]
  23.     [Serializable()]
  24.     public sealed class ConfigurationPermissionAttribute : CodeAccessSecurityAttribute
  25.     {
  26.         public ConfigurationPermissionAttribute(SecurityAction action) : base(action)
  27.         {
  28.         }
  29.        
  30.         public override IPermission CreatePermission()
  31.         {
  32.             PermissionState state = (this.Unrestricted) ? PermissionState.Unrestricted : PermissionState.None;
  33.            
  34.             return new ConfigurationPermission(state);
  35.         }
  36.     }
  37.    
  38.     //
  39.     // ConfigurationPermission is used to grant access to configuration sections that
  40.     // would not otherwise be available if the caller attempted to read the configuration
  41.     // files that make up configuration.
  42.     //
  43.     // The permission is a simple boolean one - it is either fully granted or denied.
  44.     // This boolean state is represented by using the PermissionState enumeration.
  45.     //
  46.     [Serializable()]
  47.     public sealed class ConfigurationPermission : CodeAccessPermission, IUnrestrictedPermission
  48.     {
  49.        
  50.         private PermissionState _permissionState;
  51.         // Unrestricted or None
  52.         //
  53.         // Creates a new instance of ConfigurationPermission
  54.         // that passes all demands or that fails all demands.
  55.         //
  56.         public ConfigurationPermission(PermissionState state)
  57.         {
  58.            
  59.             // validate state parameter
  60.             switch (state) {
  61.                 case PermissionState.Unrestricted:
  62.                 case PermissionState.None:
  63.                     _permissionState = state;
  64.                     break;
  65.                 default:
  66.                    
  67.                     throw ExceptionUtil.ParameterInvalid("state");
  68.                     break;
  69.             }
  70.         }
  71.        
  72.         //
  73.         // IUnrestrictedPermission interface methods
  74.         //
  75.        
  76.         //
  77.         // Checks the overall permission state of the object.
  78.         //
  79.         public bool IsUnrestricted()
  80.         {
  81.             return _permissionState == PermissionState.Unrestricted;
  82.         }
  83.        
  84.         //
  85.         // Creates a copy.
  86.         //
  87.         public override IPermission Copy()
  88.         {
  89.             return new ConfigurationPermission(_permissionState);
  90.         }
  91.        
  92.         //
  93.         // Returns the logical union between ConfigurationPermission instances.
  94.         //
  95.         public override IPermission Union(IPermission target)
  96.         {
  97.             if (target == null) {
  98.                 return Copy();
  99.             }
  100.            
  101.             if (target.GetType() != typeof(ConfigurationPermission)) {
  102.                 throw ExceptionUtil.ParameterInvalid("target");
  103.             }
  104.            
  105.             // Create an Unrestricted permission if either this or other is unrestricted
  106.             if (_permissionState == PermissionState.Unrestricted) {
  107.                 return new ConfigurationPermission(PermissionState.Unrestricted);
  108.             }
  109.             else {
  110.                 ConfigurationPermission other = (ConfigurationPermission)target;
  111.                 return new ConfigurationPermission(other._permissionState);
  112.             }
  113.         }
  114.        
  115.         //
  116.         // Returns the logical intersection between two ConfigurationPermission instances.
  117.         //
  118.         public override IPermission Intersect(IPermission target)
  119.         {
  120.             if (target == null) {
  121.                 return null;
  122.             }
  123.            
  124.             if (target.GetType() != typeof(ConfigurationPermission)) {
  125.                 throw ExceptionUtil.ParameterInvalid("target");
  126.             }
  127.            
  128.             // Create an None permission if either this or other is None
  129.             if (_permissionState == PermissionState.None) {
  130.                 return new ConfigurationPermission(PermissionState.None);
  131.             }
  132.             else {
  133.                 ConfigurationPermission other = (ConfigurationPermission)target;
  134.                 return new ConfigurationPermission(other._permissionState);
  135.             }
  136.         }
  137.        
  138.         //
  139.         // Compares two ConfigurationPermission instances
  140.         //
  141.         public override bool IsSubsetOf(IPermission target)
  142.         {
  143.             if (target == null) {
  144.                 return _permissionState == PermissionState.None;
  145.             }
  146.            
  147.             if (target.GetType() != typeof(ConfigurationPermission)) {
  148.                 throw ExceptionUtil.ParameterInvalid("target");
  149.             }
  150.            
  151.             ConfigurationPermission other = (ConfigurationPermission)target;
  152.             return (_permissionState == PermissionState.None || other._permissionState == PermissionState.Unrestricted);
  153.         }
  154.        
  155.         public override void FromXml(SecurityElement securityElement)
  156.         {
  157.             if (securityElement == null) {
  158.                 throw new ArgumentNullException(SR.GetString(SR.ConfigurationPermissionBadXml, "securityElement"));
  159.             }
  160.            
  161.             if (!securityElement.Tag.Equals("IPermission")) {
  162.                 throw new ArgumentException(SR.GetString(SR.ConfigurationPermissionBadXml, "securityElement"));
  163.             }
  164.            
  165.             string className = securityElement.Attribute("class");
  166.             if (className == null) {
  167.                 throw new ArgumentException(SR.GetString(SR.ConfigurationPermissionBadXml, "securityElement"));
  168.             }
  169.            
  170.             if (className.IndexOf(this.GetType().FullName, StringComparison.Ordinal) < 0) {
  171.                 throw new ArgumentException(SR.GetString(SR.ConfigurationPermissionBadXml, "securityElement"));
  172.             }
  173.            
  174.             string version = securityElement.Attribute("version");
  175.             if (version != "1") {
  176.                 throw new ArgumentException(SR.GetString(SR.ConfigurationPermissionBadXml, "version"));
  177.             }
  178.            
  179.             string unrestricted = securityElement.Attribute("Unrestricted");
  180.             if (unrestricted == null) {
  181.                 _permissionState = PermissionState.None;
  182.             }
  183.             else {
  184.                 switch (unrestricted) {
  185.                     case "true":
  186.                         _permissionState = PermissionState.Unrestricted;
  187.                         break;
  188.                     case "false":
  189.                        
  190.                         _permissionState = PermissionState.None;
  191.                         break;
  192.                     default:
  193.                        
  194.                         throw new ArgumentException(SR.GetString(SR.ConfigurationPermissionBadXml, "Unrestricted"));
  195.                         break;
  196.                 }
  197.             }
  198.         }
  199.        
  200.         public override SecurityElement ToXml()
  201.         {
  202.             SecurityElement securityElement = new SecurityElement("IPermission");
  203.             securityElement.AddAttribute("class", this.GetType().FullName + ", " + this.GetType().Module.Assembly.FullName.Replace('"', '\''));
  204.             securityElement.AddAttribute("version", "1");
  205.             if (IsUnrestricted()) {
  206.                 securityElement.AddAttribute("Unrestricted", "true");
  207.             }
  208.            
  209.             return securityElement;
  210.         }
  211.     }
  212. }

Developer Fusion