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