Copied remotely
[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 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 using System.Collections;
32 using System.Globalization;
33
34 namespace System.Security.Policy {
35
36         [Serializable]
37         public sealed class ZoneMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
38
39                 private readonly int version = 1;
40
41                 private SecurityZone zone;
42
43                 // so System.Activator.CreateInstance can create an instance...
44                 internal ZoneMembershipCondition ()
45                 {
46                 }
47                 
48                 public ZoneMembershipCondition (SecurityZone zone)
49                 {
50                         // we need the validations
51                         SecurityZone = zone;
52                 }
53
54                 public SecurityZone SecurityZone {
55                         get { return zone; }
56                         set {
57                                 if (!Enum.IsDefined (typeof (SecurityZone), value)) {
58                                         throw new ArgumentException (Locale.GetText (
59                                                 "invalid zone"));
60                                 }
61                                 if (value == SecurityZone.NoZone) {
62                                         throw new ArgumentException (Locale.GetText (
63                                                 "NoZone isn't valid for membership condition"));
64                                 }
65
66                                 zone = value;
67                         }
68                 }
69
70                 public bool Check (Evidence evidence)
71                 {
72                         if (evidence == null)
73                                 return false;
74
75                         IEnumerator e = evidence.GetHostEnumerator ();
76                         while (e.MoveNext ()) {
77                                 Zone z = (e.Current as Zone);
78                                 if (z != null) {
79                                         if (z.SecurityZone == zone)
80                                                 return true;
81                                 }
82                         }
83                         return false;
84                 }
85
86                 public IMembershipCondition Copy ()
87                 {
88                         return new ZoneMembershipCondition (zone);
89                 }
90
91                 public override bool Equals (Object o)
92                 {
93                         if (o is ZoneMembershipCondition == false)
94                                 return false;
95                         else
96                                 return ((ZoneMembershipCondition) o).SecurityZone == zone;
97                 }
98
99                 public void FromXml (SecurityElement element)
100                 {
101                         FromXml (element, null);
102                 }
103
104                 public void FromXml (SecurityElement element, PolicyLevel level)
105                 {
106                         MembershipConditionHelper.CheckSecurityElement (element, "element", version, version);
107
108                         string z = element.Attribute ("Zone");
109                         if (z != null) {
110                                 zone = (SecurityZone) Enum.Parse (typeof (SecurityZone), z);
111                         }
112                 }
113
114                 public override int GetHashCode ()
115                 {
116                         return zone.GetHashCode ();
117                 }
118
119                 public override string ToString ()
120                 {
121                         return "Zone - " + zone;
122                 }
123
124                 public SecurityElement ToXml ()
125                 {
126                         return ToXml (null);
127                 }
128
129                 public SecurityElement ToXml (PolicyLevel level)
130                 {
131                         // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
132                         SecurityElement se = MembershipConditionHelper.Element (typeof (ZoneMembershipCondition), version);
133                         se.AddAttribute ("Zone", zone.ToString ());
134                         return se;
135                 }
136         }
137 }