2005-01-31 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System.Security.Policy / UrlMembershipCondition.cs
index 0c47c61d04c9ee278c178f66c96cae6e5241181d..af0c9b1f4bb1573e89c22ac82d329009895242c9 100644 (file)
@@ -1,37 +1,89 @@
 //
 // System.Security.Policy.UrlMembershipCondition.cs
 //
-// Author:
-//   Duncan Mak (duncan@ximian.com)
+// Authors:
+//     Duncan Mak (duncan@ximian.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 {
 
-        public sealed class UrlMembershipCondition
-                : IMembershipCondition, ISecurityEncodable, ISecurityPolicyEncodable
-        {
-                string url;
+       [Serializable]
+        public sealed class UrlMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
+
+               private readonly int version = 1;
+
+               private Url url;
                 
                 public UrlMembershipCondition (string url)
                 {
-                        if (url == null)
-                                throw new ArgumentNullException (
-                                        Locale.GetText ("The argument is null."));
-
-                        this.url = 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; }
+                        get { return url.Value; }
+                       set { url = new Url (value); }
                 }
 
-                [MonoTODO]
-                public virtual bool Check (Evidence evidence)
+               // methods
+
+                public bool Check (Evidence evidence)
                 {
+                       if (evidence == null)
+                               return false;
+
+                       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 (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;
                 }
 
                 public IMembershipCondition Copy ()
@@ -39,20 +91,32 @@ namespace System.Security.Policy {
                         return new UrlMembershipCondition (url);
                 }
 
-                public override bool Equals (Object o)
-                {
-                        return (o is UrlMembershipCondition && ((UrlMembershipCondition) o).Url = url);
-                }
+               public override bool Equals (object o)
+               {
+                       if (o is UrlMembershipCondition) {
+                               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)
                 {
-                        return FromXml (element, null);
+                        FromXml (element, null);
                 }
 
-                public void FromXml (SecurityElement element, PolicyLevel level)
-                {
-                        url = element.Attributes ["Url"];
-                }
+               public void FromXml (SecurityElement element, PolicyLevel level)
+               {
+                       MembershipConditionHelper.CheckSecurityElement (element, "element", version, version);
+                       
+                       string u = element.Attribute ("Url");
+                       url = (u == null) ? null : new Url (u);
+               }
 
                 public override int GetHashCode ()
                 {
@@ -61,22 +125,20 @@ namespace System.Security.Policy {
 
                 public override string ToString ()
                 {
-                        Console.WriteLine ("Url - " + url);
+                        return "Url - " + url.Value;
                 }
 
-                public override SecurityElement ToXml ()
+                public SecurityElement ToXml ()
                 {
                         return ToXml (null);
                 }
 
-                public override SecurityElement ToXml (PolicyLevel level)
+                public SecurityElement ToXml (PolicyLevel level)
                 {
-                        SecurityElement element = new SecurityElement ("IMembershipCondition");
-                        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;
                 }
         }
 }