flush
[mcs.git] / class / System.Drawing / System.Drawing.Printing / PrintingPermission.cs
blob7ca0fa16291a172b36d96b22514b52a313552de6
1 //
2 // System.Drawing.PrintingPermission.cs
3 //
4 // Authors:
5 // Dennis Hayes (dennish@Raytek.com)
6 // Herve Poussineau (hpoussineau@fr.st)
7 // Sebastien Pouliot <sebastien@ximian.com>
8 //
9 // (C) 2002 Ximian, Inc
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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.
32 using System.Globalization;
33 using System.Security;
34 using System.Security.Permissions;
36 namespace System.Drawing.Printing {
38 [Serializable]
39 public sealed class PrintingPermission : CodeAccessPermission, IUnrestrictedPermission {
41 private const int version = 1;
43 private PrintingPermissionLevel _Level;
45 public PrintingPermission (PermissionState state)
47 if (CheckPermissionState (state, true) == PermissionState.Unrestricted)
48 _Level = PrintingPermissionLevel.AllPrinting;
51 public PrintingPermission (PrintingPermissionLevel printingLevel)
53 Level = printingLevel;
56 // properties
58 public PrintingPermissionLevel Level{
59 get { return _Level; }
60 set {
61 if (!Enum.IsDefined (typeof (PrintingPermissionLevel), value)) {
62 string msg = Locale.GetText ("Invalid enum {0}");
63 throw new ArgumentException (String.Format (msg, value), "Level");
65 _Level = value;
69 // methods
71 public override IPermission Copy ()
73 return new PrintingPermission (this.Level);
76 public override void FromXml (SecurityElement esd)
78 CheckSecurityElement (esd, "esd", version, version);
79 // Note: we do not (yet) care about the return value
80 // as we only accept version 1 (min/max values)
82 if (IsUnrestricted (esd))
83 _Level = PrintingPermissionLevel.AllPrinting;
84 else {
85 string level = esd.Attribute ("Level");
86 if (level != null) {
87 _Level = (PrintingPermissionLevel) Enum.Parse (
88 typeof (PrintingPermissionLevel), level);
90 else
91 _Level = PrintingPermissionLevel.NoPrinting;
95 public override IPermission Intersect (IPermission target)
97 PrintingPermission pp = Cast (target);
98 if ((pp == null) || IsEmpty () || pp.IsEmpty ())
99 return null;
101 PrintingPermissionLevel level = (_Level <= pp.Level) ? _Level : pp.Level;
102 return new PrintingPermission (level);
105 public override bool IsSubsetOf (IPermission target)
107 PrintingPermission pp = Cast (target);
108 if (pp == null)
109 return IsEmpty ();
111 return (_Level <= pp.Level);
114 public bool IsUnrestricted ()
116 return (_Level == PrintingPermissionLevel.AllPrinting);
119 public override SecurityElement ToXml ()
121 SecurityElement se = Element (version);
122 if (IsUnrestricted ())
123 se.AddAttribute ("Unrestricted", "true");
124 else
125 se.AddAttribute ("Level", _Level.ToString ());
126 return se;
129 public override IPermission Union (IPermission target)
131 PrintingPermission pp = Cast (target);
132 if (pp == null)
133 return new PrintingPermission (_Level);
134 if (IsUnrestricted () || pp.IsUnrestricted ())
135 return new PrintingPermission (PermissionState.Unrestricted);
136 if (IsEmpty () && pp.IsEmpty ())
137 return null;
139 PrintingPermissionLevel level = (_Level > pp.Level) ? _Level : pp.Level;
140 return new PrintingPermission (level);
143 // Internal helpers methods
145 private bool IsEmpty ()
147 return (_Level == PrintingPermissionLevel.NoPrinting);
150 private PrintingPermission Cast (IPermission target)
152 if (target == null)
153 return null;
155 PrintingPermission pp = (target as PrintingPermission);
156 if (pp == null) {
157 ThrowInvalidPermission (target, typeof (PrintingPermission));
160 return pp;
163 // NOTE: The following static methods should be moved out to a (static?) class
164 // if (ever) System.Drawing.dll gets more than one permission in it's assembly.
166 // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
167 internal SecurityElement Element (int version)
169 SecurityElement se = new SecurityElement ("IPermission");
170 Type type = this.GetType ();
171 se.AddAttribute ("class", type.FullName + ", " + type.Assembly.ToString ().Replace ('\"', '\''));
172 se.AddAttribute ("version", version.ToString ());
173 return se;
176 internal static PermissionState CheckPermissionState (PermissionState state, bool allowUnrestricted)
178 string msg;
179 switch (state) {
180 case PermissionState.None:
181 break;
182 case PermissionState.Unrestricted:
183 if (!allowUnrestricted) {
184 msg = Locale.GetText ("Unrestricted isn't not allowed for identity permissions.");
185 throw new ArgumentException (msg, "state");
187 break;
188 default:
189 msg = String.Format (Locale.GetText ("Invalid enum {0}"), state);
190 throw new ArgumentException (msg, "state");
192 return state;
195 // logic isn't identical to CodeAccessPermission.CheckSecurityElement - see unit tests
196 internal static int CheckSecurityElement (SecurityElement se, string parameterName, int minimumVersion, int maximumVersion)
198 if (se == null)
199 throw new ArgumentNullException (parameterName);
201 string c = se.Attribute ("class");
202 #if NET_2_0
203 if (c == null) {
204 string msg = Locale.GetText ("Missing 'class' attribute.");
205 throw new ArgumentException (msg, parameterName);
207 #else
208 if ((c == null) || (String.Compare (c, 0, "System.Drawing.Printing.PrintingPermission", 0, 42) != 0)) {
209 string msg = Locale.GetText ("Wrong 'class' attribute.");
210 throw new ArgumentException (msg, parameterName);
212 #endif
213 // we assume minimum version if no version number is supplied
214 int version = minimumVersion;
215 string v = se.Attribute ("version");
216 if (v != null) {
217 try {
218 version = Int32.Parse (v);
220 catch (Exception e) {
221 string msg = Locale.GetText ("Couldn't parse version from '{0}'.");
222 msg = String.Format (msg, v);
223 throw new ArgumentException (msg, parameterName, e);
226 #if !NET_2_0
227 else {
228 string msg = Locale.GetText ("Missing 'version' attribute.");
229 throw new ArgumentException (msg, parameterName);
231 #endif
233 if ((version < minimumVersion) || (version > maximumVersion)) {
234 string msg = Locale.GetText ("Unknown version '{0}', expected versions between ['{1}','{2}'].");
235 msg = String.Format (msg, version, minimumVersion, maximumVersion);
236 throw new ArgumentException (msg, parameterName);
238 return version;
241 // must be called after CheckSecurityElement (i.e. se != null)
242 internal static bool IsUnrestricted (SecurityElement se)
244 string value = se.Attribute ("Unrestricted");
245 if (value == null)
246 return false;
247 return (String.Compare (value, Boolean.TrueString, true, CultureInfo.InvariantCulture) == 0);
250 internal static void ThrowInvalidPermission (IPermission target, Type expected)
252 string msg = Locale.GetText ("Invalid permission type '{0}', expected type '{1}'.");
253 msg = String.Format (msg, target.GetType (), expected);
254 throw new ArgumentException (msg, "target");