2 // System.Security.PermissionSet.cs
5 // Nick Drochak(ndrochak@gol.com)
6 // Sebastien Pouliot (spouliot@motus.com)
9 // Portions (C) 2003, 2004 Motus Technologies Inc. (http://www.motus.com)
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
38 // Permission is hereby granted, free of charge, to any person obtaining
39 // a copy of this software and associated documentation files (the
40 // "Software"), to deal in the Software without restriction, including
41 // without limitation the rights to use, copy, modify, merge, publish,
42 // distribute, sublicense, and/or sell copies of the Software, and to
43 // permit persons to whom the Software is furnished to do so, subject to
44 // the following conditions:
46 // The above copyright notice and this permission notice shall be
47 // included in all copies or substantial portions of the Software.
49 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
50 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
51 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
52 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
53 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
54 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
55 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
59 using System.Collections;
60 using System.Security.Permissions;
61 using System.Security;
62 using System.Runtime.Serialization;
64 namespace System.Security {
67 public class PermissionSet: ISecurityEncodable, ICollection, IEnumerable, IStackWalk, IDeserializationCallback {
69 private PermissionState state;
70 private ArrayList list;
74 // for PolicyLevel (to avoid validation duplication)
75 internal PermissionSet ()
77 list = new ArrayList ();
80 public PermissionSet (PermissionState state)
82 if (!Enum.IsDefined(typeof(System.Security.Permissions.PermissionState), state))
83 throw new System.ArgumentException(); // state is not a valid System.Security.Permissions.PermissionState value.
85 list = new ArrayList ();
88 public PermissionSet (PermissionSet permSet) : this (PermissionState.None)
90 // LAMESPEC: This would be handled by the compiler. No way permSet is not a PermissionSet.
91 //if (!(permSet is PermissionSet))
92 // throw new System.ArgumentException(); // permSet is not an instance of System.Security.PermissionSet.
94 state = PermissionState.Unrestricted;
96 foreach (IPermission p in permSet.list)
103 public virtual IPermission AddPermission (IPermission perm)
108 IPermission existing = GetPermission (perm.GetType ());
109 if (existing != null)
110 perm = perm.Union (existing);
117 public virtual void Assert ()
121 public virtual PermissionSet Copy ()
123 return new PermissionSet (this);
126 public virtual void CopyTo (Array array, int index)
129 throw new System.ArgumentException("Array has more than one dimension"); // array has more than one dimension.
130 if (index < 0 || index >= array.Length)
131 throw new System.IndexOutOfRangeException(); // index is outside the range of allowable values for array.
133 throw new System.ArgumentNullException(); // array is null.
134 list.CopyTo (array, index);
138 public virtual void Demand ()
143 public virtual void Deny ()
147 // to be re-used by NamedPermissionSet (and other derived classes)
148 internal void FromXml (SecurityElement et, string className)
151 throw new ArgumentNullException ("et");
152 if (et.Tag != "PermissionSet")
153 throw new ArgumentException ("not PermissionSet");
154 if (!(et.Attributes ["class"] as string).EndsWith (className))
155 throw new ArgumentException ("not " + className);
156 // version isn't checked
157 // if ((et.Attributes ["version"] as string) != "1")
158 // throw new ArgumentException ("wrong version");
160 if ((et.Attributes ["Unrestricted"] as string) == "true")
161 state = PermissionState.Unrestricted;
163 state = PermissionState.None;
166 public virtual void FromXml (SecurityElement et)
169 FromXml (et, "PermissionSet");
170 if (et.Children != null) {
171 foreach (SecurityElement se in et.Children) {
172 string className = (se.Attributes ["class"] as string);
173 Type classType = Type.GetType (className);
174 object [] psNone = new object [1] { PermissionState.None };
175 IPermission p = (IPermission) Activator.CreateInstance (classType, psNone);
182 public virtual IEnumerator GetEnumerator ()
184 return list.GetEnumerator ();
187 public virtual bool IsSubsetOf (PermissionSet target)
189 // if target is empty we must be empty too
190 if ((target == null) || (target.IsEmpty ()))
191 return this.IsEmpty ();
192 // if we're unrestricted then target must also be unrestricted
193 if (this.IsUnrestricted () && target.IsUnrestricted ())
196 // if each of our permission is (a) present and (b) a subset of target
197 foreach (IPermission p in list) {
198 // for every type in both list
199 IPermission i = target.GetPermission (p.GetType ());
201 return false; // not present (condition a)
202 if (!p.IsSubsetOf (i))
203 return false; // not a subset (condition b)
209 public virtual void PermitOnly ()
213 public bool ContainsNonCodeAccessPermissions ()
215 foreach (IPermission p in list) {
216 if (! p.GetType ().IsSubclassOf (typeof(CodeAccessPermission)))
222 // undocumented behavior
224 public static byte[] ConvertPermissionSet (string inFormat, byte[] inData, string outFormat)
229 public virtual IPermission GetPermission (Type permClass)
231 foreach (object o in list) {
232 if (o.GetType ().Equals (permClass))
233 return (IPermission) o;
238 public virtual PermissionSet Intersect (PermissionSet other)
240 // no intersection possible
241 if ((other == null) || (other.IsEmpty ()) || (this.IsEmpty ()))
242 return new PermissionSet (PermissionState.None);
243 // intersections with unrestricted
244 if (this.IsUnrestricted ())
245 return other.Copy ();
246 if (other.IsUnrestricted ())
249 PermissionSet interSet = new PermissionSet (PermissionState.None);
250 foreach (IPermission p in other.list) {
251 // for every type in both list
252 IPermission i = interSet.GetPermission (p.GetType ());
254 // add intersection for this type
255 interSet.AddPermission (p.Intersect (i));
262 public virtual bool IsEmpty ()
264 // note: Unrestricted isn't empty
265 return ((state == PermissionState.Unrestricted) ? false : (list.Count == 0));
268 public virtual bool IsUnrestricted ()
270 return (state == PermissionState.Unrestricted);
273 public virtual IPermission RemovePermission (Type permClass)
275 foreach (object o in list) {
276 if (o.GetType ().Equals (permClass)) {
278 return (IPermission) o;
284 public virtual IPermission SetPermission (IPermission perm)
286 if (perm is IUnrestrictedPermission)
287 state = PermissionState.None;
288 RemovePermission (perm.GetType ());
293 public override string ToString ()
295 return ToXml ().ToString ();
298 public virtual SecurityElement ToXml ()
300 SecurityElement se = new SecurityElement ("PermissionSet");
301 se.AddAttribute ("class", GetType ().FullName);
302 se.AddAttribute ("version", "1");
303 if (state == PermissionState.Unrestricted)
304 se.AddAttribute ("Unrestricted", "true");
306 foreach (IPermission p in list)
307 se.AddChild (p.ToXml ());
312 public virtual PermissionSet Union (PermissionSet other)
316 if (this.IsUnrestricted () || other.IsUnrestricted ())
317 return new PermissionSet (PermissionState.Unrestricted);
319 PermissionSet copy = this.Copy ();
320 foreach (IPermission p in other.list) {
321 copy.AddPermission (p);
326 public virtual int Count {
327 get { return list.Count; }
330 public virtual bool IsSynchronized {
331 get { return list.IsSynchronized; }
334 public virtual bool IsReadOnly {
335 get { return false; } // always false
338 public virtual object SyncRoot {
339 get { return list.SyncRoot; }
343 void IDeserializationCallback.OnDeserialization (object sender)