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