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