Merge pull request #2420 from stefansedich/master
[mono.git] / mcs / class / corlib / System.Security.Permissions / ReflectionPermission.cs
1 //
2 // System.Security.Permissions.ReflectionPermission.cs
3 //
4 // Authors:
5 //      Tim Coleman <tim@timcoleman.com>
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2002, Tim Coleman
9 // Portions (C) 2003 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Globalization;
33 using System.Runtime.InteropServices;
34
35 namespace System.Security.Permissions {
36
37         [ComVisible (true)]
38         [Serializable]
39         public sealed class ReflectionPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
40
41                 private const int version = 1;
42
43                 ReflectionPermissionFlag flags;
44
45
46                 public ReflectionPermission (PermissionState state)
47                 {
48                         if (CheckPermissionState (state, true) == PermissionState.Unrestricted)
49                                 flags = ReflectionPermissionFlag.AllFlags;
50                         else
51                                 flags = ReflectionPermissionFlag.NoFlags;
52                 }
53
54                 public ReflectionPermission (ReflectionPermissionFlag flag)
55                 {
56                         // reuse validation by the Flags property
57                         Flags = flag;
58                 }
59
60
61                 public ReflectionPermissionFlag Flags {
62                         get { return flags; }
63                         set {
64                                 const ReflectionPermissionFlag all_flags = ReflectionPermissionFlag.AllFlags | ReflectionPermissionFlag.RestrictedMemberAccess;
65
66                                 if ((value & all_flags) != value) {
67                                         string msg = String.Format (Locale.GetText ("Invalid flags {0}"), value);
68                                         throw new ArgumentException (msg, "ReflectionPermissionFlag");
69                                 }
70
71                                 flags = value;
72                         }
73                 }
74
75
76                 public override IPermission Copy ()
77                 {
78                         return new ReflectionPermission (flags);
79                 }
80
81                 public override void FromXml (SecurityElement esd)
82                 {
83                         // General validation in CodeAccessPermission
84                         CheckSecurityElement (esd, "esd", version, version);
85                         // Note: we do not (yet) care about the return value 
86                         // as we only accept version 1 (min/max values)
87
88                         if (IsUnrestricted (esd)) {
89                                 flags = ReflectionPermissionFlag.AllFlags;
90                         }
91                         else {
92                                 flags = ReflectionPermissionFlag.NoFlags;
93                                 string xmlFlags = (esd.Attributes ["Flags"] as string);
94                                 if (xmlFlags.IndexOf ("MemberAccess") >= 0)
95                                         flags |= ReflectionPermissionFlag.MemberAccess;
96                                 if (xmlFlags.IndexOf ("ReflectionEmit") >= 0)
97                                         flags |= ReflectionPermissionFlag.ReflectionEmit;
98                                 if (xmlFlags.IndexOf ("TypeInformation") >= 0)
99                                         flags |= ReflectionPermissionFlag.TypeInformation;
100                         }
101                 }
102
103                 public override IPermission Intersect (IPermission target)
104                 {
105                         ReflectionPermission rp = Cast (target);
106                         if (rp == null)
107                                 return null;
108
109                         if (IsUnrestricted ()) {
110                                 if (rp.Flags == ReflectionPermissionFlag.NoFlags)
111                                         return null;
112                                 else
113                                         return rp.Copy ();
114                         }
115                         if (rp.IsUnrestricted ()) {
116                                 if (flags == ReflectionPermissionFlag.NoFlags)
117                                         return null;
118                                 else
119                                         return Copy ();
120                         }
121
122                         ReflectionPermission p = (ReflectionPermission) rp.Copy ();
123                         p.Flags &= flags;
124                         return ((p.Flags == ReflectionPermissionFlag.NoFlags) ? null : p);
125                 }
126
127                 public override bool IsSubsetOf (IPermission target)
128                 {
129                         ReflectionPermission rp = Cast (target);
130                         if (rp == null)
131                                 return (flags == ReflectionPermissionFlag.NoFlags);
132
133                         if (IsUnrestricted ())
134                                 return rp.IsUnrestricted ();
135                         else if (rp.IsUnrestricted ())
136                                 return true;
137
138                         return ((flags & rp.Flags) == flags);
139                 }
140
141                 public bool IsUnrestricted ()
142                 {
143                         return (flags == ReflectionPermissionFlag.AllFlags);
144                 }
145
146                 public override SecurityElement ToXml ()
147                 {
148                         SecurityElement se = Element (version);
149                         if (IsUnrestricted ()) {
150                                 se.AddAttribute ("Unrestricted", "true");
151                         }
152                         else {
153                                 if (flags == ReflectionPermissionFlag.NoFlags)
154                                         se.AddAttribute ("Flags", "NoFlags");
155                                 else if ((flags & ReflectionPermissionFlag.AllFlags) == ReflectionPermissionFlag.AllFlags)
156                                         se.AddAttribute ("Flags", "AllFlags");
157                                 else {
158                                         string xmlFlags = "";
159                                         if ((flags & ReflectionPermissionFlag.MemberAccess) == ReflectionPermissionFlag.MemberAccess)
160                                                 xmlFlags = "MemberAccess";
161                                         if ((flags & ReflectionPermissionFlag.ReflectionEmit) == ReflectionPermissionFlag.ReflectionEmit) {
162                                                 if (xmlFlags.Length > 0)
163                                                         xmlFlags += ", ";
164                                                 xmlFlags += "ReflectionEmit";
165                                         }
166                                         if ((flags & ReflectionPermissionFlag.TypeInformation) == ReflectionPermissionFlag.TypeInformation) {
167                                                 if (xmlFlags.Length > 0)
168                                                         xmlFlags += ", ";
169                                                 xmlFlags += "TypeInformation";
170                                         }
171                                         se.AddAttribute ("Flags", xmlFlags);
172                                 }
173                         }
174                         return se;
175                 }
176
177                 public override IPermission Union (IPermission other)
178                 {
179                         ReflectionPermission rp = Cast (other);
180                         if (other == null)
181                                 return Copy ();
182
183                         if (IsUnrestricted () || rp.IsUnrestricted ())
184                                 return new ReflectionPermission (PermissionState.Unrestricted);
185
186                         ReflectionPermission p = (ReflectionPermission) rp.Copy ();
187                         p.Flags |= flags;
188                         return p;
189                 }
190
191                 // IBuiltInPermission
192                 int IBuiltInPermission.GetTokenIndex ()
193                 {
194                         return (int) BuiltInToken.Reflection;
195                 }
196
197                 // helpers
198
199                 private ReflectionPermission Cast (IPermission target)
200                 {
201                         if (target == null)
202                                 return null;
203
204                         ReflectionPermission rp = (target as ReflectionPermission);
205                         if (rp == null) {
206                                 ThrowInvalidPermission (target, typeof (ReflectionPermission));
207                         }
208
209                         return rp;
210                 }
211         }
212 }