2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.Security.Policy / SiteMembershipCondition.cs
1 //
2 // SiteMembershipCondition.cs: Site MembershipCondition
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Collections;
31 using System.Globalization;
32
33 namespace System.Security.Policy {
34
35         [Serializable]
36         public sealed class SiteMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
37
38                 private readonly int version = 1;
39
40                 private string _site;
41
42                 // constructors
43
44                 // so System.Activator.CreateInstance can create an instance...
45                 internal SiteMembershipCondition ()
46                 {
47                 }
48
49                 public SiteMembershipCondition (string site)
50                 {
51                         Site = site;
52                 }
53
54                 // properties
55
56                 public string Site { 
57                         get { return _site; }
58                         set {
59                                 if (value == null)
60                                         throw new ArgumentNullException ("site");
61                                 if (!System.Security.Policy.Site.IsValid (value))
62                                         throw new ArgumentException ("invalid site");
63                                 _site = value;
64                         }
65                 }
66
67                 // methods
68
69                 public bool Check (Evidence evidence) 
70                 {
71                         if (evidence == null)
72                                 return false;
73
74                         IEnumerator e = evidence.GetHostEnumerator ();
75                         while (e.MoveNext ()) {
76                                 if (e.Current is Site) {
77                                         string[] s1 = _site.Split ('.');
78                                         string[] s2 = (e.Current as Site).origin_site.Split ('.');
79                                         for (int i = s1.Length - 1, j = s2.Length - 1; i>=0; i--, j--) {
80                                                 if (i == 0) {
81                                                         // special * case
82                                                         return (String.Compare (s1 [0], "*", true, CultureInfo.InvariantCulture) == 0);
83                                                 }
84                                                 if (String.Compare (s1 [i], s2 [j], true, CultureInfo.InvariantCulture) != 0)
85                                                         return false;
86                                         }
87                                         return true;
88                                 }
89                         }
90                         return false;
91                 }
92                 
93                 public IMembershipCondition Copy () 
94                 {
95                         return new SiteMembershipCondition (_site);
96                 }
97
98                 public override bool Equals (object o) 
99                 {
100                         if (o == null)
101                                 return false;
102                         if (o is SiteMembershipCondition) {
103                                 Site s = new Site ((o as SiteMembershipCondition)._site);
104                                 return s.Equals (new Site (_site));
105                         }
106                         return false;
107                 }
108
109                 public void FromXml (SecurityElement e) 
110                 {
111                         FromXml (e, null);
112                 }
113
114                 public void FromXml (SecurityElement e, PolicyLevel level) 
115                 {
116                         MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
117                         _site = e.Attribute ("Site");
118                         // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
119                 }
120
121                 public override int GetHashCode () 
122                 {
123                         return _site.GetHashCode ();
124                 }
125
126                 public override string ToString () 
127                 {
128                         return "Site - " + _site;
129                 }
130
131                 public SecurityElement ToXml () 
132                 {
133                         return ToXml (null);
134                 }
135
136                 public SecurityElement ToXml (PolicyLevel level) 
137                 {
138                         // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
139                         SecurityElement se = MembershipConditionHelper.Element (typeof (SiteMembershipCondition), version);
140                         se.AddAttribute ("Site", _site);
141                         return se;
142                 }
143         }
144 }