2005-01-31 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System.Security.Policy / UrlMembershipCondition.cs
index 4a78acb318f45e27c6fd6d00f555ab8adc3841ad..af0c9b1f4bb1573e89c22ac82d329009895242c9 100644 (file)
@@ -3,44 +3,84 @@
 //
 // Authors:
 //     Duncan Mak (duncan@ximian.com)
-//     Sebastien Pouliot (spouliot@motus.com)
+//     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2003, Ximian Inc.
 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System;
+using System.Collections;
 using System.Globalization;
 
 namespace System.Security.Policy {
 
        [Serializable]
-        public sealed class UrlMembershipCondition
-                : IMembershipCondition, ISecurityEncodable, ISecurityPolicyEncodable, IConstantMembershipCondition
-        {
-                string url;
+        public sealed class UrlMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
+
+               private readonly int version = 1;
+
+               private Url url;
                 
                 public UrlMembershipCondition (string url)
                 {
-                        this.url = System.Security.Policy.Url.Prepare (url);
+                        this.url = new Url (url);
                 }
 
+               internal UrlMembershipCondition (Url url)
+               {
+                       // as the Url object has already been validated there's no
+                       // need to restart the whole process by converting to string
+                       this.url = (Url) url.Copy ();
+               }
+
+               // properties
+
                 public string Url {
-                        get { return url; }
-                       set { url = System.Security.Policy.Url.Prepare (value); }
+                        get { return url.Value; }
+                       set { url = new Url (value); }
                 }
 
+               // methods
+
                 public bool Check (Evidence evidence)
                 {
                        if (evidence == null)
                                return false;
 
-                       foreach (object o in evidence) {
-                               Url u = (o as Url);
-                               if (u != null) {
+                       string u = url.Value;
+                       int wildcard = u.LastIndexOf ("*");     // partial match with a wildcard at the end
+                       if (wildcard == -1)
+                               wildcard = u.Length;            // exact match
+
+                       IEnumerator e = evidence.GetHostEnumerator ();
+                       while (e.MoveNext ()) {
+                               if (e.Current is Url) {
                                        // note: there shouldn't be more than one Url evidence
-                                       if (System.Security.Policy.Url.Compare (url, u.Value))
+                                       if (String.Compare (u, 0, (e.Current as Url).Value, 0, wildcard,
+                                               true, CultureInfo.InvariantCulture) == 0) {
                                                return true;
+                                       }
+                                       // but we must check for all of them!
                                }
                        }
                         return false;
@@ -51,13 +91,19 @@ namespace System.Security.Policy {
                         return new UrlMembershipCondition (url);
                 }
 
-                public override bool Equals (Object o)
-                {
+               public override bool Equals (object o)
+               {
                        if (o is UrlMembershipCondition) {
-                               return System.Security.Policy.Url.Compare (url, ((UrlMembershipCondition) o).Url);
+                               string u = url.Value;
+                               int wildcard = u.LastIndexOf ("*");     // partial match with a wildcard at the end
+                               if (wildcard == -1)
+                                       wildcard = u.Length;            // exact match
+
+                               return (String.Compare (u, 0, (o as UrlMembershipCondition).Url,
+                                       0, wildcard, true, CultureInfo.InvariantCulture) == 0);
                        }
                        return false;
-                }
+               }
 
                 public void FromXml (SecurityElement element)
                 {
@@ -66,22 +112,10 @@ namespace System.Security.Policy {
 
                public void FromXml (SecurityElement element, PolicyLevel level)
                {
-                       if (element == null)
-                               throw new ArgumentNullException ("element");
-                       
-                       if (element.Tag != "IMembershipCondition")
-                               throw new ArgumentException (
-                                       Locale.GetText ("Invalid tag - expected IMembershipCondition"));
-
-                       if (element.Attribute ("class") != GetType ().AssemblyQualifiedName)
-                               throw new ArgumentException (
-                                       Locale.GetText ("Invalid class attribute"));
-
-                       if (element.Attribute ("version") != "1")
-                               throw new ArgumentException (
-                                       Locale.GetText ("Invalid version"));
+                       MembershipConditionHelper.CheckSecurityElement (element, "element", version, version);
                        
-                       url = element.Attribute ("Url");
+                       string u = element.Attribute ("Url");
+                       url = (u == null) ? null : new Url (u);
                }
 
                 public override int GetHashCode ()
@@ -91,7 +125,7 @@ namespace System.Security.Policy {
 
                 public override string ToString ()
                 {
-                        return "Url - " + url;
+                        return "Url - " + url.Value;
                 }
 
                 public SecurityElement ToXml ()
@@ -101,11 +135,10 @@ namespace System.Security.Policy {
 
                 public SecurityElement ToXml (PolicyLevel level)
                 {
-                        SecurityElement element = new SecurityElement ("IMembershipCondition");
-                       element.AddAttribute ("class", this.GetType ().AssemblyQualifiedName);
-                       element.AddAttribute ("version", "1");
-                        element.AddAttribute ("Url", url);
-                        return element;
+                       // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
+                       SecurityElement se = MembershipConditionHelper.Element (typeof (UrlMembershipCondition), version);
+                        se.AddAttribute ("Url", url.Value);
+                        return se;
                 }
         }
 }