2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.Security.Policy / ApplicationDirectoryMembershipCondition.cs
1 //
2 // System.Security.Policy.ApplicationDirectoryMembershipCondition\r
3 //\r
4 // Authors:\r
5 //      Nick Drochak (ndrochak@gol.com)\r
6 //      Jackson Harper (Jackson@LatitudeGeo.com)\r
7 //      Sebastien Pouliot  <sebastien@ximian.com>
8 //\r
9 // (C) 2002 Nick Drochak, All rights reserved.\r
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
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.
30 //
31
32 using System.Collections;
33 using System.Globalization;
34 using System.IO;
35 using System.Reflection;
36
37 using Mono.Security;
38 \r
39 namespace System.Security.Policy {\r
40 \r
41         [Serializable]\r
42         public sealed class ApplicationDirectoryMembershipCondition : IConstantMembershipCondition, IMembershipCondition {\r
43
44                 private readonly int version = 1;
45
46                 public ApplicationDirectoryMembershipCondition ()
47                 {
48                 }
49
50                 // Methods\r
51                 public bool Check (Evidence evidence)\r
52                 {\r
53                         if (evidence == null)\r
54                                 return false;\r
55
56                         string codebase = Assembly.GetCallingAssembly ().CodeBase;
57                         Uri local = new Uri (codebase);
58                         Url ucode = new Url (codebase);
59
60                         // *both* ApplicationDirectory and Url must be in *Host* evidences
61                         bool adir = false;
62                         bool url = false;
63                         IEnumerator e = evidence.GetHostEnumerator ();
64                         while (e.MoveNext ()) {
65                                 object o = e.Current;
66 \r
67                                 if (!adir && (o is ApplicationDirectory)) {\r
68                                         ApplicationDirectory ad = (o as ApplicationDirectory);
69                                         string s = ad.Directory;
70                                         adir = (String.Compare (s, 0, local.ToString (), 0, s.Length, true, CultureInfo.InvariantCulture) == 0);
71                                 }
72                                 else if (!url && (o is Url)) {
73                                         url = ucode.Equals (o);
74                                 }
75
76                                 // got both ?
77                                 if (adir && url)
78                                         return true;\r
79                         }\r
80                         return false;\r
81                 }\r
82 \r
83                 public IMembershipCondition Copy () \r
84                 { \r
85                         return new ApplicationDirectoryMembershipCondition ();\r
86                 }\r
87                 \r
88                 public override bool Equals (object o) \r
89                 { \r
90                         return (o is ApplicationDirectoryMembershipCondition); \r
91                 }\r
92                 \r
93                 public void FromXml (SecurityElement e)\r
94                 {\r
95                         FromXml (e, null);\r
96                 }\r
97                 \r
98                 public void FromXml (SecurityElement e, PolicyLevel level)\r
99                 {\r
100                         MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
101                 }\r
102                 \r
103                 // All instances of ApplicationDirectoryMembershipCondition are equal so they should\r
104                 // have the same hashcode\r
105                 public override int GetHashCode () \r
106                 { \r
107                         return typeof (ApplicationDirectoryMembershipCondition).GetHashCode ();\r
108                 }\r
109                 \r
110                 public override string ToString () \r
111                 { \r
112                         return "ApplicationDirectory";\r
113                 }\r
114                 \r
115                 public SecurityElement ToXml () \r
116                 { \r
117                         return ToXml (null);\r
118                 }\r
119                 \r
120                 public SecurityElement ToXml (PolicyLevel level) \r
121                 {\r
122                         SecurityElement se = MembershipConditionHelper.Element (typeof (ApplicationDirectoryMembershipCondition), version);
123                         // nothing to add
124                         return se;
125                 }
126         }\r
127 }\r