[msvc] Update csproj files (#4669)
[mono.git] / mcs / class / corlib / System.Security / PermissionSetCollection.cs
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.
27 //
28
29 using System.Collections;
30 using System.Runtime.InteropServices;
31 using System.Security.Permissions;
32
33 namespace System.Security {
34
35         [Serializable]
36         [ComVisible (true)]
37         [Obsolete ("seems the *Choice actions won't survive")]
38         public sealed class PermissionSetCollection : ICollection, IEnumerable {
39
40                 private static string tagName = "PermissionSetCollection";
41                 private IList _list;
42
43                 public PermissionSetCollection ()
44                 {
45                         _list = (IList) new ArrayList ();
46                 }
47
48                 // properties
49
50                 public int Count {
51                         get { return _list.Count; }
52                 }
53
54                 public bool IsSynchronized {
55                         get { return false; }
56                 }
57
58                 public IList PermissionSets {
59                         get { return _list; }
60                 }
61
62                 public object SyncRoot {
63                         // this is the "real thing"
64                         get { throw new NotSupportedException (); }
65                 }
66
67                 // methods
68
69                 public void Add (PermissionSet permSet)
70                 {
71                         if (permSet == null)
72                                 throw new ArgumentNullException ("permSet");
73                         _list.Add (permSet);
74                 }
75
76                 public PermissionSetCollection Copy ()
77                 {
78                         PermissionSetCollection psc = new PermissionSetCollection ();
79                         foreach (PermissionSet ps in _list) {
80                                 psc._list.Add (ps.Copy ());
81                         }
82                         return psc;
83                 }
84
85                 public void CopyTo (PermissionSet[] array, int index)
86                 {
87                         // this is the "real thing"
88                         throw new NotSupportedException ();
89                 }
90
91                 void ICollection.CopyTo (Array array, int index)
92                 {
93                         // this is the "real thing"
94                         throw new NotSupportedException ();
95                 }
96
97                 public void Demand ()
98                 {
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);
104                                 }
105                         }
106                         superset.Demand ();
107                 }
108
109                 public void FromXml (SecurityElement el) 
110                 {
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");
116                         }
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);
123                                 }
124                         }
125                 }
126
127                 public IEnumerator GetEnumerator ()
128                 {
129                         return _list.GetEnumerator ();
130                 }
131
132                 public PermissionSet GetSet (int index) 
133                 {
134                         if ((index < 0) || (index >= _list.Count))
135                                 throw new ArgumentOutOfRangeException ("index");
136
137                         return (PermissionSet) _list [index];
138                 }
139
140                 public void RemoveSet (int index) 
141                 {
142                         if ((index < 0) || (index >= _list.Count))
143                                 throw new ArgumentOutOfRangeException ("index");
144
145                         _list.RemoveAt (index);
146                 }
147
148                 public override string ToString ()
149                 {
150                         return ToXml ().ToString ();
151                 }
152
153                 public SecurityElement ToXml ()
154                 {
155                         SecurityElement se = new SecurityElement (tagName);
156                         foreach (PermissionSet ps in _list) {
157                                 se.AddChild (ps.ToXml ());
158                         }
159                         return se;
160                 }
161
162                 // internal stuff
163
164                 internal void DemandChoice ()
165                 {
166                         SecurityException exception = null;
167                         bool result = false;
168                         foreach (PermissionSet pset in _list) {
169                                 try {
170                                         pset.Demand ();
171                                         result = true;
172                                         break;
173                                 }
174                                 catch (SecurityException se) {
175                                         // keep the first failure, we may throw it if we not succeed
176                                         if (exception == null)
177                                                 exception = se;
178                                 }
179                         }
180
181                         if (!result) {
182                                 if (exception != null)
183                                         throw exception;
184                                 else
185                                         throw new SecurityException ("DemandChoice failed.");
186                         }
187                 }
188
189                 // 2.0 metadata format
190
191                 internal static PermissionSetCollection CreateFromBinaryFormat (byte[] data)
192                 {
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);
196                         }
197
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);
206                                 }
207
208                                 PermissionSet ps = new PermissionSet (PermissionState.None);
209                                 ps.DeclarativeSecurity = true;
210                                 ps.AddPermission (p); 
211                                 psc.Add (ps);
212                         }
213                         return psc;
214                 }
215         }
216 }
217