copying the latest Sys.Web.Services from trunk.
[mono.git] / mcs / class / corlib / System.Security / SecurityException.cs
1 //
2 // System.Security.SecurityException.cs
3 //
4 // Authors:
5 //      Nick Drochak(ndrochak@gol.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) Nick Drochak
9 // (C) 2004 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.Reflection;
34 using System.Runtime.InteropServices;
35 using System.Runtime.Serialization;
36 using System.Security.Permissions;
37 using System.Security.Policy;
38 using System.Text;
39
40 namespace System.Security {
41
42         [Serializable]
43 #if NET_2_0
44         public class SecurityException : SystemException, _Exception {
45 #else
46         public class SecurityException : SystemException {
47 #endif
48                 // Fields
49                 string permissionState;
50                 Type permissionType;
51                 private string _granted;
52                 private string _refused;
53                 private SecurityAction _action;
54                 private object _demanded;
55                 private object _denyset;
56                 private object _permitset;
57                 private AssemblyName _assembly;
58                 private IPermission _firstperm;
59                 private IPermission _permfailed;
60                 private MethodInfo _method;
61                 private string _url;
62                 private SecurityZone _zone;
63
64                 // Properties
65
66 #if NET_2_0
67                 [ComVisible (false)]
68                 public SecurityAction Action {
69                         get { return _action; }
70                         set { _action = value; }
71                 }
72
73                 [ComVisible (false)]
74                 public object DenySetInstance {
75                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
76                         get { return _denyset; }
77                         set { _denyset = value; }
78                 }
79
80                 [ComVisible (false)]
81                 public AssemblyName FailedAssemblyInfo {
82                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
83                         get { return _assembly; }
84                         set { _assembly = value; }
85                 }
86
87                 [ComVisible (false)]
88                 public MethodInfo Method {
89                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
90                         get { return _method; }
91                         set { _method = value; }
92                 }
93
94                 [Obsolete]
95                 [ComVisible (false)]
96                 public IPermission PermissionThatFailed {
97                         get { return _permfailed; }
98                         set { _permfailed = value; }
99                 }
100
101                 [ComVisible (false)]
102                 public object PermitOnlySetInstance {
103                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
104                         get { return _permitset; }
105                         set { _permitset = value; }
106                 }
107
108                 public string Url {
109                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
110                         get { return _url; }
111                         set { _url = value; }
112                 }
113
114                 public SecurityZone Zone {
115                         get { return _zone; }
116                         set { _zone = value; }
117                 }
118 #endif
119
120 #if NET_2_0
121                 [ComVisible (false)]
122                 public 
123 #else
124                 internal
125 #endif
126                 object Demanded {
127                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
128                         get { return _demanded; }
129                         set { _demanded = value; }
130                 }
131
132 #if NET_2_0
133                 public 
134 #else
135                 internal
136 #endif
137                 IPermission FirstPermissionThatFailed {
138                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
139                         get { return _firstperm; }
140                         set { _firstperm = value; }
141                 }
142
143                 public string PermissionState {
144                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
145                         get { return permissionState; }
146 #if NET_2_0
147                         set { permissionState = value; }
148 #endif
149                 }
150
151                 public Type PermissionType {
152                         get { return permissionType; }
153 #if NET_2_0
154                         set { permissionType = value; }
155 #endif
156                 }
157
158 #if NET_1_1
159                 public string GrantedSet {
160                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
161                         get { return _granted; }
162 #if NET_2_0
163                         set { _granted = value; }
164 #endif
165                 }
166
167                 public string RefusedSet {
168                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
169                         get { return _refused; }
170 #if NET_2_0
171                         set { _refused = value; }
172 #endif
173                 }
174 #endif
175                 // Constructors
176
177                 public SecurityException ()
178                         : base (Locale.GetText ("A security error has been detected."))
179                 {
180                         base.HResult = unchecked ((int)0x8013150A);
181                 }
182
183                 public SecurityException (string message) 
184                         : base (message)
185                 {
186                         base.HResult = unchecked ((int)0x8013150A);
187                 }
188                 
189                 protected SecurityException (SerializationInfo info, StreamingContext context) 
190                         : base (info, context)
191                 {
192                         base.HResult = unchecked ((int)0x8013150A);
193                         permissionState = info.GetString ("PermissionState");
194                 }
195                 
196                 public SecurityException (string message, Exception inner) 
197                         : base (message, inner)
198                 {
199                         base.HResult = unchecked ((int)0x8013150A);
200                 }
201                 
202                 public SecurityException (string message, Type type) 
203                         :  base (message) 
204                 {
205                         base.HResult = unchecked ((int)0x8013150A);
206                         permissionType = type;
207                 }
208                 
209                 public SecurityException (string message, Type type, string state) 
210                         : base (message) 
211                 {
212                         base.HResult = unchecked ((int)0x8013150A);
213                         permissionType = type;
214                         permissionState = state;
215                 }
216
217                 internal SecurityException (string message, PermissionSet granted, PermissionSet refused) 
218                         : base (message)
219                 {
220                         base.HResult = unchecked ((int)0x8013150A);
221                         _granted = granted.ToString ();
222                         _refused = refused.ToString ();
223                 }
224
225 #if NET_2_0
226                 public
227 #else
228                 internal
229 #endif
230                 SecurityException (string message, object deny, object permitOnly, MethodInfo method, 
231                         object demanded, IPermission permThatFailed)
232                         : base (message)
233                 {
234                         base.HResult = unchecked ((int)0x8013150A);
235                         _denyset = deny;
236                         _permitset = permitOnly;
237                         _method = method;
238                         _demanded = demanded;
239                         _firstperm = permThatFailed;
240                 }
241
242 #if NET_2_0
243                 public
244 #else
245                 internal
246 #endif
247                 SecurityException (string message, AssemblyName assemblyName, PermissionSet grant, 
248                         PermissionSet refused, MethodInfo method, SecurityAction action, object demanded, 
249                         IPermission permThatFailed, Evidence evidence)
250                         : base (message)
251                 {
252                         base.HResult = unchecked ((int)0x8013150A);
253                         _assembly = assemblyName;
254                         _granted = (grant == null) ? String.Empty : grant.ToString ();
255                         _refused = (refused == null) ? String.Empty : refused.ToString ();
256                         _method = method;
257                         _action = action;
258                         _demanded = demanded;
259                         _firstperm = permThatFailed;
260                         if (_firstperm != null)
261                                 permissionType = _firstperm.GetType ();
262                         // FIXME ? evidence ?
263                 }
264
265                 // Methods
266                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
267                 {
268                         base.GetObjectData (info, context);
269                         try {
270                                 info.AddValue ("PermissionState", PermissionState);
271                         }
272                         catch (SecurityException) {
273                                 // serialize only if permitted to do so
274                         }
275                 }
276
277                 public override string ToString ()
278                 {
279                         StringBuilder sb = new StringBuilder (base.ToString ());
280                         try {
281                                 if (permissionType != null) {
282                                         sb.AppendFormat ("{0}Type: {1}", Environment.NewLine, PermissionType);
283                                 }
284                                 if (_method != null) {
285                                         // method string representation doesn't include the type
286                                         string m = _method.ToString ();
287                                         int ret = m.IndexOf (" ") + 1;
288                                         sb.AppendFormat ("{0}Method: {1} {2}.{3}", Environment.NewLine, 
289                                                 _method.ReturnType.Name, _method.ReflectedType, m.Substring (ret));
290                                 }
291                                 if (permissionState != null) {
292                                         sb.AppendFormat ("{0}State: {1}", Environment.NewLine, PermissionState);
293                                 }
294                                 if ((_granted != null) && (_granted.Length > 0)) {
295                                         sb.AppendFormat ("{0}Granted: {1}", Environment.NewLine, GrantedSet);
296                                 }
297                                 if ((_refused != null) && (_refused.Length > 0)) {
298                                         sb.AppendFormat ("{0}Refused: {1}", Environment.NewLine, RefusedSet);
299                                 }
300                                 if (_demanded != null) {
301                                         sb.AppendFormat ("{0}Demanded: {1}", Environment.NewLine, Demanded);
302                                 }
303                                 if (_firstperm != null) {
304                                         sb.AppendFormat ("{0}Failed Permission: {1}", Environment.NewLine, FirstPermissionThatFailed);
305                                 }
306                         }
307                         catch (SecurityException) {
308                                 // some informations can't be displayed
309                         }
310                         return sb.ToString ();
311                 }
312         }
313 }