2 // SiteMembershipCondition.cs: Site MembershipCondition
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections;
31 using System.Globalization;
32 using System.Runtime.InteropServices;
34 namespace System.Security.Policy {
40 public sealed class SiteMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
42 private readonly int version = 1;
48 // so System.Activator.CreateInstance can create an instance...
49 internal SiteMembershipCondition ()
53 public SiteMembershipCondition (string site)
64 throw new ArgumentNullException ("site");
65 if (!System.Security.Policy.Site.IsValid (value))
66 throw new ArgumentException ("invalid site");
73 public bool Check (Evidence evidence)
78 IEnumerator e = evidence.GetHostEnumerator ();
79 while (e.MoveNext ()) {
80 if (e.Current is Site) {
81 string[] s1 = _site.Split ('.');
82 string[] s2 = (e.Current as Site).origin_site.Split ('.');
83 for (int i = s1.Length - 1, j = s2.Length - 1; i>=0; i--, j--) {
86 return (String.Compare (s1 [0], "*", true, CultureInfo.InvariantCulture) == 0);
88 if (String.Compare (s1 [i], s2 [j], true, CultureInfo.InvariantCulture) != 0)
97 public IMembershipCondition Copy ()
99 return new SiteMembershipCondition (_site);
102 public override bool Equals (object o)
106 if (o is SiteMembershipCondition) {
107 Site s = new Site ((o as SiteMembershipCondition)._site);
108 return s.Equals (new Site (_site));
113 public void FromXml (SecurityElement e)
118 public void FromXml (SecurityElement e, PolicyLevel level)
120 MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
121 _site = e.Attribute ("Site");
122 // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
125 public override int GetHashCode ()
127 return _site.GetHashCode ();
130 public override string ToString ()
132 return "Site - " + _site;
135 public SecurityElement ToXml ()
140 public SecurityElement ToXml (PolicyLevel level)
142 // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
143 SecurityElement se = MembershipConditionHelper.Element (typeof (SiteMembershipCondition), version);
144 se.AddAttribute ("Site", _site);