2 // SiteMembershipCondition.cs: Site MembershipCondition
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.Globalization;
36 using System.Security;
38 namespace System.Security.Policy {
41 public sealed class SiteMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
47 public SiteMembershipCondition (string site)
58 throw new ArgumentNullException ("site");
59 if (!System.Security.Policy.Site.IsValid (value))
60 throw new ArgumentException ("invalid site");
67 public bool Check (Evidence evidence)
72 IEnumerator e = evidence.GetHostEnumerator ();
73 while (e.MoveNext ()) {
74 if (e.Current is Site) {
75 string[] s1 = _site.Split ('.');
76 string[] s2 = (e.Current as Site).origin_site.Split ('.');
77 for (int i = s1.Length - 1, j = s2.Length - 1; i>=0; i--, j--) {
80 return (String.Compare (s1 [0], "*", true, CultureInfo.InvariantCulture) == 0);
82 if (String.Compare (s1 [i], s2 [j], true, CultureInfo.InvariantCulture) != 0)
91 public IMembershipCondition Copy ()
93 return new SiteMembershipCondition (_site);
96 public override bool Equals (object o)
100 if (o is SiteMembershipCondition) {
101 Site s = new Site ((o as SiteMembershipCondition)._site);
102 return s.Equals (new Site (_site));
107 public void FromXml (SecurityElement e)
112 public void FromXml (SecurityElement e, PolicyLevel level)
115 throw new ArgumentNullException ("e");
116 if (e.Tag != "IMembershipCondition")
117 throw new ArgumentException (Locale.GetText ("Invalid XML - not a IMembershipCondition tag."));
118 if (e.Attribute ("class") != GetType ().AssemblyQualifiedName)
119 throw new ArgumentException (Locale.GetText ("Invalid class."));
120 if (e.Attribute ("version") != "1")
121 throw new ArgumentException (Locale.GetText ("Invalid version."));
123 _site = e.Attribute ("Site");
126 public override int GetHashCode ()
128 return _site.GetHashCode ();
131 public override string ToString ()
133 return "Site - " + _site;
136 public SecurityElement ToXml ()
141 public SecurityElement ToXml (PolicyLevel level)
143 SecurityElement element = new SecurityElement ("IMembershipCondition");
144 element.AddAttribute ("class", this.GetType ().AssemblyQualifiedName);
145 element.AddAttribute ("version", "1");
146 element.AddAttribute ("Site", _site);