2 // System.Security.PermissionSetCollection class
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.Runtime.InteropServices;
33 using System.Security.Permissions;
35 namespace System.Security {
39 [Obsolete ("seems the *Choice actions won't survive")]
40 public sealed class PermissionSetCollection : ICollection, IEnumerable {
42 private static string tagName = "PermissionSetCollection";
45 public PermissionSetCollection ()
47 _list = (IList) new ArrayList ();
53 get { return _list.Count; }
56 public bool IsSynchronized {
60 public IList PermissionSets {
64 public object SyncRoot {
65 // this is the "real thing"
66 get { throw new NotSupportedException (); }
71 public void Add (PermissionSet permSet)
74 throw new ArgumentNullException ("permSet");
78 public PermissionSetCollection Copy ()
80 PermissionSetCollection psc = new PermissionSetCollection ();
81 foreach (PermissionSet ps in _list) {
82 psc._list.Add (ps.Copy ());
87 public void CopyTo (PermissionSet[] array, int index)
89 // this is the "real thing"
90 throw new NotSupportedException ();
93 void ICollection.CopyTo (Array array, int index)
95 // this is the "real thing"
96 throw new NotSupportedException ();
101 // check all collection in a single stack walk
102 PermissionSet superset = new PermissionSet (PermissionState.None);
103 foreach (PermissionSet ps in _list) {
104 foreach (IPermission p in ps) {
105 superset.AddPermission (p);
111 public void FromXml (SecurityElement el)
114 throw new ArgumentNullException ("el");
115 if (el.Tag != tagName) {
116 string msg = String.Format ("Invalid tag {0} expected {1}", el.Tag, tagName);
117 throw new ArgumentException (msg, "el");
120 if (el.Children != null) {
121 foreach (SecurityElement child in el.Children) {
122 PermissionSet ps = new PermissionSet (PermissionState.None);
129 public IEnumerator GetEnumerator ()
131 return _list.GetEnumerator ();
134 public PermissionSet GetSet (int index)
136 if ((index < 0) || (index >= _list.Count))
137 throw new ArgumentOutOfRangeException ("index");
139 return (PermissionSet) _list [index];
142 public void RemoveSet (int index)
144 if ((index < 0) || (index >= _list.Count))
145 throw new ArgumentOutOfRangeException ("index");
147 _list.RemoveAt (index);
150 public override string ToString ()
152 return ToXml ().ToString ();
155 public SecurityElement ToXml ()
157 SecurityElement se = new SecurityElement (tagName);
158 foreach (PermissionSet ps in _list) {
159 se.AddChild (ps.ToXml ());
166 internal void DemandChoice ()
168 SecurityException exception = null;
170 foreach (PermissionSet pset in _list) {
176 catch (SecurityException se) {
177 // keep the first failure, we may throw it if we not succeed
178 if (exception == null)
184 if (exception != null)
187 throw new SecurityException ("DemandChoice failed.");
191 // 2.0 metadata format
193 internal static PermissionSetCollection CreateFromBinaryFormat (byte[] data)
195 if ((data == null) || (data [0] != 0x2E) || (data.Length < 2)) {
196 string msg = Locale.GetText ("Invalid data in 2.0 metadata format.");
197 throw new SecurityException (msg);
201 int numattr = PermissionSet.ReadEncodedInt (data, ref pos);
202 PermissionSetCollection psc = new PermissionSetCollection ();
203 for (int i = 0; i < numattr; i++) {
204 IPermission p = PermissionSet.ProcessAttribute (data, ref pos);
206 string msg = Locale.GetText ("Unsupported data found in 2.0 metadata format.");
207 throw new SecurityException (msg);
210 PermissionSet ps = new PermissionSet (PermissionState.None);
211 ps.DeclarativeSecurity = true;
212 ps.AddPermission (p);