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.
29 using System
.Collections
;
30 using System
.Runtime
.InteropServices
;
31 using System
.Security
.Permissions
;
33 namespace System
.Security
{
37 [Obsolete ("seems the *Choice actions won't survive")]
38 public sealed class PermissionSetCollection
: ICollection
, IEnumerable
{
40 private static string tagName
= "PermissionSetCollection";
43 public PermissionSetCollection ()
45 _list
= (IList
) new ArrayList ();
51 get { return _list.Count; }
54 public bool IsSynchronized
{
58 public IList PermissionSets
{
62 public object SyncRoot
{
63 // this is the "real thing"
64 get { throw new NotSupportedException (); }
69 public void Add (PermissionSet permSet
)
72 throw new ArgumentNullException ("permSet");
76 public PermissionSetCollection
Copy ()
78 PermissionSetCollection psc
= new PermissionSetCollection ();
79 foreach (PermissionSet ps
in _list
) {
80 psc
._list
.Add (ps
.Copy ());
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 ();
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
);
109 public void FromXml (SecurityElement el
)
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");
118 if (el
.Children
!= null) {
119 foreach (SecurityElement child
in el
.Children
) {
120 PermissionSet ps
= new PermissionSet (PermissionState
.None
);
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 ());
164 internal void DemandChoice ()
166 SecurityException exception
= null;
168 foreach (PermissionSet pset
in _list
) {
174 catch (SecurityException se
) {
175 // keep the first failure, we may throw it if we not succeed
176 if (exception
== null)
182 if (exception
!= null)
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
);
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
);
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
);