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