In .:
[mono.git] / mcs / class / corlib / System.Security.Permissions / KeyContainerPermission.cs
1 //
2 // System.Security.Permissions.KeyContainerPermission class
3 //
4 // Author
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 #if NET_2_0
30
31 using System.Globalization;
32 using System.Runtime.InteropServices;
33
34 namespace System.Security.Permissions {
35
36         [Serializable]
37         [ComVisible (true)]
38         public sealed class KeyContainerPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
39
40                 private KeyContainerPermissionAccessEntryCollection _accessEntries;
41                 private KeyContainerPermissionFlags _flags;
42
43                 private const int version = 1;
44
45                 // Constructors
46
47                 public KeyContainerPermission (PermissionState state) 
48                 {
49                         if (CheckPermissionState (state, true) == PermissionState.Unrestricted) {
50                                 _flags = KeyContainerPermissionFlags.AllFlags;
51                         }
52                 }
53
54                 public KeyContainerPermission (KeyContainerPermissionFlags flags)
55                 {
56                         SetFlags (flags);
57                 }
58
59                 public KeyContainerPermission (KeyContainerPermissionFlags flags, KeyContainerPermissionAccessEntry[] accessList) 
60                 {
61                         SetFlags (flags);
62                         if (accessList != null) {
63                                 foreach (KeyContainerPermissionAccessEntry kcpae in accessList) {
64                                         _accessEntries.Add (kcpae);
65                                 }
66                         }
67                 }
68
69                 // Properties
70
71                 public KeyContainerPermissionAccessEntryCollection AccessEntries {
72                         get { return _accessEntries; }
73                 }
74
75                 public KeyContainerPermissionFlags Flags {
76                         get { return _flags; }
77                 }
78
79                 // Methods
80
81                 public override IPermission Copy () 
82                 {
83                         if (_accessEntries.Count == 0)
84                                 return new KeyContainerPermission (_flags);
85
86                         KeyContainerPermissionAccessEntry[] list = new KeyContainerPermissionAccessEntry [_accessEntries.Count];
87                         _accessEntries.CopyTo (list, 0);
88                         return new KeyContainerPermission (_flags, list);
89                 }
90
91                 [MonoTODO ("(2.0) missing support for AccessEntries")]
92                 public override void FromXml (SecurityElement esd) 
93                 {
94                         // General validation in CodeAccessPermission
95                         CheckSecurityElement (esd, "esd", version, version);
96                         // Note: we do not (yet) care about the return value 
97                         // as we only accept version 1 (min/max values)
98
99                         if (IsUnrestricted (esd)) {
100                                 _flags = KeyContainerPermissionFlags.AllFlags;
101                         }
102                         else {
103                                 // ???
104                                 _flags = (KeyContainerPermissionFlags) Enum.Parse (
105                                         typeof (KeyContainerPermissionFlags), esd.Attribute ("Flags"));
106                         }
107                 }
108
109                 [MonoTODO ("(2.0)")]
110                 public override IPermission Intersect (IPermission target) 
111                 {
112                         return null;
113                 }
114
115                 [MonoTODO ("(2.0)")]
116                 public override bool IsSubsetOf (IPermission target) 
117                 {
118                         return false;
119                 }
120
121                 public bool IsUnrestricted () 
122                 {
123                         return (_flags == KeyContainerPermissionFlags.AllFlags);
124                 }
125
126                 [MonoTODO ("(2.0) missing support for AccessEntries")]
127                 public override SecurityElement ToXml () 
128                 {
129                         SecurityElement e = Element (version);
130                         if (IsUnrestricted ()) {
131                                 e.AddAttribute ("Unrestricted", "true");
132                         } else {
133                                 // ...
134                         }
135                         return e;
136                 }
137
138                 public override IPermission Union (IPermission target)
139                 {
140                         KeyContainerPermission kcp = Cast (target);
141                         if (kcp == null)
142                                 return Copy ();
143
144                         KeyContainerPermissionAccessEntryCollection kcpaec = new KeyContainerPermissionAccessEntryCollection ();
145                         // copy first group
146                         foreach (KeyContainerPermissionAccessEntry kcpae in _accessEntries) {
147                                 kcpaec.Add (kcpae);
148                         }
149                         // copy second group...
150                         foreach (KeyContainerPermissionAccessEntry kcpae in kcp._accessEntries) {
151                                 // ... but only if not present in first group
152                                 if (_accessEntries.IndexOf (kcpae) == -1)
153                                         kcpaec.Add (kcpae);
154                         }
155
156                         if (kcpaec.Count == 0)
157                                 return new KeyContainerPermission ((_flags | kcp._flags));
158
159                         KeyContainerPermissionAccessEntry[] list = new KeyContainerPermissionAccessEntry [kcpaec.Count];
160                         kcpaec.CopyTo (list, 0);
161                         return new KeyContainerPermission ((_flags | kcp._flags), list);
162                 }
163
164                 // IBuiltInPermission
165                 int IBuiltInPermission.GetTokenIndex ()
166                 {
167                         return (int) BuiltInToken.KeyContainer;
168                 }
169
170                 // helpers
171
172                 private void SetFlags (KeyContainerPermissionFlags flags)
173                 {
174                         if ((flags & KeyContainerPermissionFlags.AllFlags) != 0) {
175                                 string msg = String.Format (Locale.GetText ("Invalid enum {0}"), flags);
176                                 throw new ArgumentException (msg, "KeyContainerPermissionFlags");
177                         }
178                         _flags = flags;
179                 }
180
181                 private KeyContainerPermission Cast (IPermission target)
182                 {
183                         if (target == null)
184                                 return null;
185
186                         KeyContainerPermission kcp = (target as KeyContainerPermission);
187                         if (kcp == null) {
188                                 ThrowInvalidPermission (target, typeof (KeyContainerPermission));
189                         }
190
191                         return kcp;
192                 }
193         }
194 }
195
196 #endif