2007-07-22 Nagappan A <anagappan@novell.com>
[mono.git] / mcs / class / System.Data / System.Data.Common / PermissionHelper.cs
1 //
2 // System.Data.Common.PermissionHelper.cs
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 using System.Globalization;
30 using System.Security;
31 using System.Security.Permissions;
32
33 namespace System.Data.Common {
34
35         // NOTE: This version of PermissionHelper was customized for System.Data
36
37         internal sealed class PermissionHelper {
38
39                 // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
40                 internal static SecurityElement Element (Type type, int version) 
41                 {
42                         SecurityElement se = new SecurityElement ("IPermission");
43                         se.AddAttribute ("class", type.FullName + ", " + type.Assembly.ToString ().Replace ('\"', '\''));
44                         se.AddAttribute ("version", version.ToString ());
45                         return se;
46                 }
47
48                 internal static PermissionState CheckPermissionState (PermissionState state, bool allowUnrestricted)
49                 {
50                         string msg;
51                         switch (state) {
52                         case PermissionState.None:
53                                 break;
54                         case PermissionState.Unrestricted:
55                                 if (!allowUnrestricted) {
56                                         msg = Locale.GetText ("Unrestricted isn't not allowed for identity permissions.");
57                                         throw new ArgumentException (msg, "state");
58                                 }
59                                 break;
60                         default:
61                                 msg = String.Format (Locale.GetText ("Invalid enum {0}"), state);
62 #if NET_2_0
63                                 throw new ArgumentOutOfRangeException (msg, "state");
64 #else
65                                 throw new ArgumentException (msg, "state");
66 #endif
67                         }
68                         return state;
69                 }
70
71                 internal static int CheckSecurityElement (SecurityElement se, string parameterName, int minimumVersion, int maximumVersion) 
72                 {
73                         if (se == null)
74                                 throw new ArgumentNullException (parameterName);
75
76                         if (se.Tag != "IPermission") {
77                                 string msg = Locale.GetText ("Invalid tag '{0}' expected 'IPermission'.");
78                                 throw new ArgumentException (String.Format (msg, se.Tag), parameterName);
79                         }
80
81                         // we assume minimum version if no version number is supplied
82                         int version = minimumVersion;
83                         string v = se.Attribute ("version");
84                         if (v != null) {
85                                 try {
86                                         version = Int32.Parse (v);
87                                 }
88                                 catch (Exception e) {
89                                         string msg = Locale.GetText ("Couldn't parse version from '{0}'.");
90                                         msg = String.Format (msg, v);
91                                         throw new ArgumentException (msg, parameterName, e);
92                                 }
93                         }
94
95                         if ((version < minimumVersion) || (version > maximumVersion)) {
96                                 string msg = Locale.GetText ("Unknown version '{0}', expected versions between ['{1}','{2}'].");
97                                 msg = String.Format (msg, version, minimumVersion, maximumVersion);
98                                 throw new ArgumentException (msg, parameterName);
99                         }
100                         return version;
101                 }
102
103                 // must be called after CheckSecurityElement (i.e. se != null)
104                 internal static bool IsUnrestricted (SecurityElement se) 
105                 {
106                         string value = se.Attribute ("Unrestricted");
107                         if (value == null)
108                                 return false;
109                         return (String.Compare (value, Boolean.TrueString, true, CultureInfo.InvariantCulture) == 0);
110                 }
111
112                 internal static void ThrowInvalidPermission (IPermission target, Type expected) 
113                 {
114                         string msg = Locale.GetText ("Invalid permission type '{0}', expected type '{1}'.");
115                         msg = String.Format (msg, target.GetType (), expected);
116                         throw new ArgumentException (msg, "target");
117                 }
118         }
119 }