2010-05-27 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.Security.Policy / ApplicationDirectoryMembershipCondition.cs
blob3f73bc83518ccb85e3fdc75ae8a5125efeaf8f68
1 //
2 // System.Security.Policy.ApplicationDirectoryMembershipCondition
3 //
4 // Authors:
5 // Nick Drochak (ndrochak@gol.com)
6 // Jackson Harper (Jackson@LatitudeGeo.com)
7 // Sebastien Pouliot <sebastien@ximian.com>
8 //
9 // (C) 2002 Nick Drochak, All rights reserved.
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Globalization;
34 using System.IO;
35 using System.Reflection;
36 using System.Runtime.InteropServices;
38 using Mono.Security;
40 namespace System.Security.Policy {
42 [Serializable]
43 [ComVisible (true)]
44 public sealed class ApplicationDirectoryMembershipCondition : IConstantMembershipCondition, IMembershipCondition {
46 private readonly int version = 1;
48 public ApplicationDirectoryMembershipCondition ()
52 // Methods
53 public bool Check (Evidence evidence)
55 if (evidence == null)
56 return false;
58 string codebase = Assembly.GetCallingAssembly ().CodeBase;
59 Uri local = new Uri (codebase);
60 Url ucode = new Url (codebase);
62 // *both* ApplicationDirectory and Url must be in *Host* evidences
63 bool adir = false;
64 bool url = false;
65 IEnumerator e = evidence.GetHostEnumerator ();
66 while (e.MoveNext ()) {
67 object o = e.Current;
69 if (!adir && (o is ApplicationDirectory)) {
70 ApplicationDirectory ad = (o as ApplicationDirectory);
71 string s = ad.Directory;
72 adir = (String.Compare (s, 0, local.ToString (), 0, s.Length, true, CultureInfo.InvariantCulture) == 0);
74 else if (!url && (o is Url)) {
75 url = ucode.Equals (o);
78 // got both ?
79 if (adir && url)
80 return true;
82 return false;
85 public IMembershipCondition Copy ()
87 return new ApplicationDirectoryMembershipCondition ();
90 public override bool Equals (object o)
92 return (o is ApplicationDirectoryMembershipCondition);
95 public void FromXml (SecurityElement e)
97 FromXml (e, null);
100 public void FromXml (SecurityElement e, PolicyLevel level)
102 MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
105 // All instances of ApplicationDirectoryMembershipCondition are equal so they should
106 // have the same hashcode
107 public override int GetHashCode ()
109 return typeof (ApplicationDirectoryMembershipCondition).GetHashCode ();
112 public override string ToString ()
114 return "ApplicationDirectory";
117 public SecurityElement ToXml ()
119 return ToXml (null);
122 public SecurityElement ToXml (PolicyLevel level)
124 SecurityElement se = MembershipConditionHelper.Element (typeof (ApplicationDirectoryMembershipCondition), version);
125 // nothing to add
126 return se;