2010-05-27 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.Security / PermissionSetCollection.cs
blobc13c1efef77cffd8a1a2ba48bc5d61622b8ffef2
1 //
2 // System.Security.PermissionSetCollection class
3 //
4 // Authors
5 // Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
29 using System.Collections;
30 using System.Runtime.InteropServices;
31 using System.Security.Permissions;
33 namespace System.Security {
35 [Serializable]
36 [ComVisible (true)]
37 [Obsolete ("seems the *Choice actions won't survive")]
38 public sealed class PermissionSetCollection : ICollection, IEnumerable {
40 private static string tagName = "PermissionSetCollection";
41 private IList _list;
43 public PermissionSetCollection ()
45 _list = (IList) new ArrayList ();
48 // properties
50 public int Count {
51 get { return _list.Count; }
54 public bool IsSynchronized {
55 get { return false; }
58 public IList PermissionSets {
59 get { return _list; }
62 public object SyncRoot {
63 // this is the "real thing"
64 get { throw new NotSupportedException (); }
67 // methods
69 public void Add (PermissionSet permSet)
71 if (permSet == null)
72 throw new ArgumentNullException ("permSet");
73 _list.Add (permSet);
76 public PermissionSetCollection Copy ()
78 PermissionSetCollection psc = new PermissionSetCollection ();
79 foreach (PermissionSet ps in _list) {
80 psc._list.Add (ps.Copy ());
82 return psc;
85 public void CopyTo (PermissionSet[] array, int index)
87 // this is the "real thing"
88 throw new NotSupportedException ();
91 void ICollection.CopyTo (Array array, int index)
93 // this is the "real thing"
94 throw new NotSupportedException ();
97 public void Demand ()
99 // check all collection in a single stack walk
100 PermissionSet superset = new PermissionSet (PermissionState.None);
101 foreach (PermissionSet ps in _list) {
102 foreach (IPermission p in ps) {
103 superset.AddPermission (p);
106 superset.Demand ();
109 public void FromXml (SecurityElement el)
111 if (el == null)
112 throw new ArgumentNullException ("el");
113 if (el.Tag != tagName) {
114 string msg = String.Format ("Invalid tag {0} expected {1}", el.Tag, tagName);
115 throw new ArgumentException (msg, "el");
117 _list.Clear ();
118 if (el.Children != null) {
119 foreach (SecurityElement child in el.Children) {
120 PermissionSet ps = new PermissionSet (PermissionState.None);
121 ps.FromXml (child);
122 _list.Add (ps);
127 public IEnumerator GetEnumerator ()
129 return _list.GetEnumerator ();
132 public PermissionSet GetSet (int index)
134 if ((index < 0) || (index >= _list.Count))
135 throw new ArgumentOutOfRangeException ("index");
137 return (PermissionSet) _list [index];
140 public void RemoveSet (int index)
142 if ((index < 0) || (index >= _list.Count))
143 throw new ArgumentOutOfRangeException ("index");
145 _list.RemoveAt (index);
148 public override string ToString ()
150 return ToXml ().ToString ();
153 public SecurityElement ToXml ()
155 SecurityElement se = new SecurityElement (tagName);
156 foreach (PermissionSet ps in _list) {
157 se.AddChild (ps.ToXml ());
159 return se;
162 // internal stuff
164 internal void DemandChoice ()
166 SecurityException exception = null;
167 bool result = false;
168 foreach (PermissionSet pset in _list) {
169 try {
170 pset.Demand ();
171 result = true;
172 break;
174 catch (SecurityException se) {
175 // keep the first failure, we may throw it if we not succeed
176 if (exception == null)
177 exception = se;
181 if (!result) {
182 if (exception != null)
183 throw exception;
184 else
185 throw new SecurityException ("DemandChoice failed.");
189 // 2.0 metadata format
191 internal static PermissionSetCollection CreateFromBinaryFormat (byte[] data)
193 if ((data == null) || (data [0] != 0x2E) || (data.Length < 2)) {
194 string msg = Locale.GetText ("Invalid data in 2.0 metadata format.");
195 throw new SecurityException (msg);
198 int pos = 1;
199 int numattr = PermissionSet.ReadEncodedInt (data, ref pos);
200 PermissionSetCollection psc = new PermissionSetCollection ();
201 for (int i = 0; i < numattr; i++) {
202 IPermission p = PermissionSet.ProcessAttribute (data, ref pos);
203 if (p == null) {
204 string msg = Locale.GetText ("Unsupported data found in 2.0 metadata format.");
205 throw new SecurityException (msg);
208 PermissionSet ps = new PermissionSet (PermissionState.None);
209 ps.DeclarativeSecurity = true;
210 ps.AddPermission (p);
211 psc.Add (ps);
213 return psc;