The Labs \ Source Viewer \ SSCLI \ System.Net \ DnsPermissionAttribute

  1. //------------------------------------------------------------------------------
  2. // <copyright file="DnsPermission.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.Net
  16. {
  17.    
  18.     using System.Security;
  19.     using System.Security.Permissions;
  20.     using System.Globalization;
  21.    
  22.     //NOTE: While DnsPermissionAttribute resides in System.DLL,
  23.     // no classes from that DLL are able to make declarative usage of DnsPermission.
  24.    
  25.     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
  26.    
  27.     [Serializable()]
  28.     public sealed class DnsPermissionAttribute : CodeAccessSecurityAttribute
  29.     {
  30.         public DnsPermissionAttribute(SecurityAction action) : base(action)
  31.         {
  32.         }
  33.        
  34.         public override IPermission CreatePermission()
  35.         {
  36.             if (Unrestricted) {
  37.                 return new DnsPermission(PermissionState.Unrestricted);
  38.             }
  39.             else {
  40.                 return new DnsPermission(PermissionState.None);
  41.             }
  42.         }
  43.     }
  44.    
  45.    
  46.     /// <devdoc>
  47.     /// <para>
  48.     /// </para>
  49.     /// </devdoc>
  50.     [Serializable()]
  51.     public sealed class DnsPermission : CodeAccessPermission, IUnrestrictedPermission
  52.     {
  53.        
  54.         private bool m_noRestriction;
  55.        
  56.         /// <devdoc>
  57.         /// <para>
  58.         /// Creates a new instance of the <see cref='System.Net.DnsPermission'/>
  59.         /// class that passes all demands or that fails all demands.
  60.         /// </para>
  61.         /// </devdoc>
  62.         public DnsPermission(PermissionState state)
  63.         {
  64.             m_noRestriction = (state == PermissionState.Unrestricted);
  65.         }
  66.        
  67.         internal DnsPermission(bool free)
  68.         {
  69.             m_noRestriction = free;
  70.         }
  71.        
  72.         // IUnrestrictedPermission interface methods
  73.         /// <devdoc>
  74.         /// <para>
  75.         /// Checks the overall permission state of the object.
  76.         /// </para>
  77.         /// </devdoc>
  78.         public bool IsUnrestricted()
  79.         {
  80.             return m_noRestriction;
  81.         }
  82.        
  83.         // IPermission interface methods
  84.         /// <devdoc>
  85.         /// <para>
  86.         /// Creates a copy of a <see cref='System.Net.DnsPermission'/> instance.
  87.         /// </para>
  88.         /// </devdoc>
  89.         public override IPermission Copy()
  90.         {
  91.             return new DnsPermission(m_noRestriction);
  92.         }
  93.        
  94.         /// <devdoc>
  95.         /// <para>Returns the logical union between two <see cref='System.Net.DnsPermission'/> instances.</para>
  96.         /// </devdoc>
  97.         public override IPermission Union(IPermission target)
  98.         {
  99.             // Pattern suggested by Security engine
  100.             if (target == null) {
  101.                 return this.Copy();
  102.             }
  103.             DnsPermission other = target as DnsPermission;
  104.             if (other == null) {
  105.                 throw new ArgumentException(SR.GetString(SR.net_perm_target), "target");
  106.             }
  107.             return new DnsPermission(m_noRestriction || other.m_noRestriction);
  108.         }
  109.        
  110.         /// <devdoc>
  111.         /// <para>Returns the logical intersection between two <see cref='System.Net.DnsPermission'/> instances.</para>
  112.         /// </devdoc>
  113.         public override IPermission Intersect(IPermission target)
  114.         {
  115.             // Pattern suggested by Security engine
  116.             if (target == null) {
  117.                 return null;
  118.             }
  119.             DnsPermission other = target as DnsPermission;
  120.             if (other == null) {
  121.                 throw new ArgumentException(SR.GetString(SR.net_perm_target), "target");
  122.             }
  123.            
  124.             // return null if resulting permission is restricted and empty
  125.             // Hence, the only way for a bool permission will be.
  126.             if (this.m_noRestriction && other.m_noRestriction) {
  127.                 return new DnsPermission(true);
  128.             }
  129.             return null;
  130.         }
  131.        
  132.        
  133.         /// <devdoc>
  134.         /// <para>Compares two <see cref='System.Net.DnsPermission'/> instances.</para>
  135.         /// </devdoc>
  136.         public override bool IsSubsetOf(IPermission target)
  137.         {
  138.             // Pattern suggested by Security engine
  139.             if (target == null) {
  140.                 return m_noRestriction == false;
  141.             }
  142.             DnsPermission other = target as DnsPermission;
  143.             if (other == null) {
  144.                 throw new ArgumentException(SR.GetString(SR.net_perm_target), "target");
  145.             }
  146.             //Here is the matrix of result based on m_noRestriction for me and she
  147.             // me.noRestriction she.noRestriction me.isSubsetOf(she)
  148.             // 0 0 1
  149.             // 0 1 1
  150.             // 1 0 0
  151.             // 1 1 1
  152.             return (!m_noRestriction || other.m_noRestriction);
  153.         }
  154.        
  155.         /// <devdoc>
  156.         /// </devdoc>
  157.         public override void FromXml(SecurityElement securityElement)
  158.         {
  159.            
  160.             if (securityElement == null) {
  161.                 //
  162.                 // null SecurityElement
  163.                 //
  164.                
  165.                 throw new ArgumentNullException("securityElement");
  166.             }
  167.            
  168.             if (!securityElement.Tag.Equals("IPermission")) {
  169.                 //
  170.                 // SecurityElement must be a permission element
  171.                 //
  172.                
  173.                 throw new ArgumentException(SR.GetString(SR.net_no_classname), "securityElement");
  174.             }
  175.            
  176.             string className = securityElement.Attribute("class");
  177.            
  178.             if (className == null) {
  179.                 //
  180.                 // SecurityElement must be a permission element for this type
  181.                 //
  182.                
  183.                 throw new ArgumentException(SR.GetString(SR.net_no_classname), "securityElement");
  184.             }
  185.            
  186.            
  187.             if (className.IndexOf(this.GetType().FullName) < 0) {
  188.                 //
  189.                 // SecurityElement must be a permission element for this type
  190.                 //
  191.                
  192.                 throw new ArgumentException(SR.GetString(SR.net_no_typename), "securityElement");
  193.             }
  194.            
  195.             string str = securityElement.Attribute("Unrestricted");
  196.             m_noRestriction = (str != null ? (0 == string.Compare(str, "true", StringComparison.OrdinalIgnoreCase)) : false);
  197.         }
  198.        
  199.         /// <devdoc>
  200.         /// <para>[To be supplied.]</para>
  201.         /// </devdoc>
  202.         public override SecurityElement ToXml()
  203.         {
  204.            
  205.             SecurityElement securityElement = new SecurityElement("IPermission");
  206.            
  207.             securityElement.AddAttribute("class", this.GetType().FullName + ", " + this.GetType().Module.Assembly.FullName.Replace('"', '\''));
  208.             securityElement.AddAttribute("version", "1");
  209.            
  210.             if (m_noRestriction) {
  211.                 securityElement.AddAttribute("Unrestricted", "true");
  212.             }
  213.            
  214.             return securityElement;
  215.         }
  216.        
  217.     }
  218.     // class DnsPermission
  219.    
  220. }
  221. // namespace System.Net

Developer Fusion