The Labs \ Source Viewer \ SSCLI \ System.Security.Policy \ ApplicationDirectoryMembershipCondition

  1. // ==++==
  2. //
  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. //
  14. // ==--==
  15. // ApplicationDirectoryMembershipCondition.cs
  16. //
  17. // Implementation of membership condition for "application directories"
  18. //
  19. namespace System.Security.Policy
  20. {
  21.     using System;
  22.     using SecurityElement = System.Security.SecurityElement;
  23.     using System.Security.Policy;
  24.     using URLString = System.Security.Util.URLString;
  25.     using System.Collections;
  26.    
  27.     [Serializable()]
  28.     [System.Runtime.InteropServices.ComVisible(true)]
  29.     public sealed class ApplicationDirectoryMembershipCondition : IMembershipCondition, IConstantMembershipCondition
  30.     {
  31.         //------------------------------------------------------
  32.         //
  33.         // PRIVATE STATE DATA
  34.         //
  35.         //------------------------------------------------------
  36.        
  37.         //------------------------------------------------------
  38.         //
  39.         // PUBLIC CONSTRUCTORS
  40.         //
  41.         //------------------------------------------------------
  42.        
  43.         public ApplicationDirectoryMembershipCondition()
  44.         {
  45.         }
  46.        
  47.         //------------------------------------------------------
  48.         //
  49.         // IMEMBERSHIPCONDITION IMPLEMENTATION
  50.         //
  51.         //------------------------------------------------------
  52.        
  53.         public bool Check(Evidence evidence)
  54.         {
  55.             if (evidence == null)
  56.                 return false;
  57.            
  58.             IEnumerator enumerator = evidence.GetHostEnumerator();
  59.             while (enumerator.MoveNext()) {
  60.                 object obj = enumerator.Current;
  61.                
  62.                 if (obj is ApplicationDirectory) {
  63.                     ApplicationDirectory dir = (ApplicationDirectory)obj;
  64.                    
  65.                     IEnumerator innerEnumerator = evidence.GetHostEnumerator();
  66.                    
  67.                     while (innerEnumerator.MoveNext()) {
  68.                         object innerObj = innerEnumerator.Current;
  69.                        
  70.                         if (innerObj is Url) {
  71.                             // We need to add a wildcard at the end because IsSubsetOf
  72.                             // keys off of it.
  73.                            
  74.                             string appDir = dir.Directory;
  75.                            
  76.                             if (appDir != null && appDir.Length > 1) {
  77.                                 if (appDir[appDir.Length - 1] == '/')
  78.                                     appDir += "*";
  79.                                 else
  80.                                     appDir += "/*";
  81.                                 // comment
  82.                                 URLString appDirString = new URLString(appDir);
  83.                                
  84.                                 if (((Url)innerObj).GetURLString().IsSubsetOf(appDirString)) {
  85.                                     return true;
  86.                                 }
  87.                             }
  88.                         }
  89.                     }
  90.                 }
  91.             }
  92.            
  93.             return false;
  94.         }
  95.        
  96.         public IMembershipCondition Copy()
  97.         {
  98.             return new ApplicationDirectoryMembershipCondition();
  99.         }
  100.        
  101.         public SecurityElement ToXml()
  102.         {
  103.             return ToXml(null);
  104.         }
  105.        
  106.         public void FromXml(SecurityElement e)
  107.         {
  108.             FromXml(e, null);
  109.         }
  110.        
  111.         public SecurityElement ToXml(PolicyLevel level)
  112.         {
  113.             SecurityElement root = new SecurityElement("IMembershipCondition");
  114.             System.Security.Util.XMLUtil.AddClassAttribute(root, this.GetType(), "System.Security.Policy.ApplicationDirectoryMembershipCondition");
  115.             // If you hit this assert then most likely you are trying to change the name of this class.
  116.             // This is ok as long as you change the hard coded string above and change the assert below.
  117.             BCLDebug.Assert(this.GetType().FullName.Equals("System.Security.Policy.ApplicationDirectoryMembershipCondition"), "Class name changed!");
  118.            
  119.             root.AddAttribute("version", "1");
  120.            
  121.             return root;
  122.         }
  123.        
  124.         public void FromXml(SecurityElement e, PolicyLevel level)
  125.         {
  126.             if (e == null)
  127.                 throw new ArgumentNullException("e");
  128.            
  129.             if (!e.Tag.Equals("IMembershipCondition")) {
  130.                 throw new ArgumentException(Environment.GetResourceString("Argument_MembershipConditionElement"));
  131.             }
  132.         }
  133.        
  134.         public override bool Equals(object o)
  135.         {
  136.             return (o is ApplicationDirectoryMembershipCondition);
  137.         }
  138.        
  139.         public override int GetHashCode()
  140.         {
  141.             return typeof(ApplicationDirectoryMembershipCondition).GetHashCode();
  142.         }
  143.        
  144.         public override string ToString()
  145.         {
  146.             return Environment.GetResourceString("ApplicationDirectory_ToString");
  147.         }
  148.     }
  149. }

Developer Fusion