2004-07-14 Sebastien Pouliot <sebastien@ximian.com>
[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 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 #if NET_2_0
54                 private SecurityAction _action;
55                 private object _demanded;
56                 private object _denyset;
57                 private object _permitset;
58                 private AssemblyName _assembly;
59                 private IPermission _firstperm;
60                 private IPermission _permfailed;
61                 private MethodInfo _method;
62                 private string _url;
63                 private SecurityZone _zone;
64 #endif
65
66                 // Properties
67
68 #if NET_2_0
69                 public SecurityAction Action {
70                         get { return _action; }
71                         set { _action = value; }
72                 }
73
74                 public object Demanded {
75                         get { return _demanded; }
76                         set { _demanded = value; }
77                 }
78
79                 public object DenySetInstance {
80                         get { return _denyset; }
81                         set { _denyset = value; }
82                 }
83
84                 public AssemblyName FailedAssemblyInfo {
85                         get { return _assembly; }
86                         set { _assembly = value; }
87                 }
88
89                 public IPermission FirstPermissionThatFailed {
90                         get { return _firstperm; }
91                         set { _firstperm = value; }
92                 }
93
94                 public MethodInfo Method {
95                         get { return _method; }
96                         set { _method = value; }
97                 }
98
99                 [Obsolete]
100                 public IPermission PermissionThatFailed {
101                         get { return _permfailed; }
102                         set { _permfailed = value; }
103                 }
104
105                 public object PermitOnlySetInstance {
106                         get { return _permitset; }
107                         set { _permitset = value; }
108                 }
109
110                 public string Url {
111                         get { return _url; }
112                         set { _url = value; }
113                 }
114
115                 public SecurityZone Zone {
116                         get { return _zone; }
117                         set { _zone = value; }
118                 }
119 #endif
120
121                 public string PermissionState {
122                         get { return permissionState; }
123 #if NET_2_0
124                         set { permissionState = value; }
125 #endif
126                 }
127
128                 public Type PermissionType {
129                         get { return permissionType; }
130 #if NET_2_0
131                         set { permissionType = value; }
132 #endif
133                 }
134
135 #if NET_1_1
136                 public string GrantedSet {
137                         get { return _granted; }
138 #if NET_2_0
139                         set { _granted = value; }
140 #endif
141                 }
142
143                 public string RefusedSet {
144                         get { return _refused; }
145 #if NET_2_0
146                         set { _refused = value; }
147 #endif
148                 }
149 #endif
150                 // Constructors
151
152                 public SecurityException ()
153                         : base (Locale.GetText ("A security error has been detected."))
154                 {
155                         base.HResult = unchecked ((int)0x8013150A);
156                 }
157
158                 public SecurityException (string message) 
159                         : base (message)
160                 {
161                         base.HResult = unchecked ((int)0x8013150A);
162                 }
163                 
164                 protected SecurityException (SerializationInfo info, StreamingContext context) 
165                         : base (info, context)
166                 {
167                         base.HResult = unchecked ((int)0x8013150A);
168                         permissionState = info.GetString ("PermissionState");
169                 }
170                 
171                 public SecurityException (string message, Exception inner) 
172                         : base (message, inner)
173                 {
174                         base.HResult = unchecked ((int)0x8013150A);
175                 }
176                 
177                 public SecurityException (string message, Type type) 
178                         :  base (message) 
179                 {
180                         base.HResult = unchecked ((int)0x8013150A);
181                         permissionType = type;
182                 }
183                 
184                 public SecurityException (string message, Type type, string state) 
185                         : base (message) 
186                 {
187                         base.HResult = unchecked ((int)0x8013150A);
188                         permissionType = type;
189                         permissionState = state;
190                 }
191
192                 internal SecurityException (string message, PermissionSet granted, PermissionSet refused) 
193                         : base (message)
194                 {
195                         base.HResult = unchecked ((int)0x8013150A);
196                         _granted = granted.ToString ();
197                         _refused = refused.ToString ();
198                 }
199
200 #if NET_2_0
201                 public SecurityException (string message, object deny, object permitOnly, MethodInfo method, 
202                         object demanded, IPermission permThatFailed)
203                         : base (message)
204                 {
205                         _denyset = deny;
206                         _permitset = permitOnly;
207                         _method = method;
208                         _demanded = demanded;
209                         _permfailed = permThatFailed;
210                 }
211
212                 public SecurityException (string message, AssemblyName assemblyName, PermissionSet grant, 
213                         PermissionSet refused, MethodInfo method, SecurityAction action, object demanded, 
214                         IPermission permThatFailed, Evidence evidence)
215                         : base (message)
216                 {
217                         _assembly = assemblyName;
218                         _granted = grant.ToString ();
219                         _refused = refused.ToString ();
220                         _method = method;
221                         _action = action;
222                         _demanded = demanded;
223                         _permfailed = permThatFailed;
224                         // FIXME ? evidence ?
225                 }
226 #endif
227
228                 // Methods
229                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
230                 {
231                         base.GetObjectData (info, context);
232                         info.AddValue ("PermissionState", permissionState);
233                 }
234
235                 public override string ToString ()
236                 {
237                         StringBuilder sb = new StringBuilder (base.ToString ());
238                         if (permissionState != null) {
239                                 sb.Append (Environment.NewLine);
240                                 sb.Append ("State: ");
241                                 sb.Append (permissionState);
242                         }
243                         if (permissionType != null) {
244                                 sb.Append (Environment.NewLine);
245                                 sb.Append ("Type: ");
246                                 sb.Append (permissionType.ToString ());
247                         }
248 #if NET_1_1
249                         if (_granted != null) {
250                                 sb.Append (Environment.NewLine);
251                                 sb.Append ("Granted: ");
252                                 sb.Append (_granted.ToString ());
253                         }
254                         if (_refused != null) {
255                                 sb.Append (Environment.NewLine);
256                                 sb.Append ("Refused: ");
257                                 sb.Append (_refused.ToString ());
258                         }
259 #endif
260                         return sb.ToString ();
261                 }
262         }
263 }