2 // System.Drawing.PrintingPermission.cs
5 // Dennis Hayes (dennish@Raytek.com)
6 // Herve Poussineau (hpoussineau@fr.st)
7 // Sebastien Pouliot <sebastien@ximian.com>
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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 {
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;
50 _Level = PrintingPermissionLevel.NoPrinting;
53 public PrintingPermission (PrintingPermissionLevel printingLevel)
55 Level = printingLevel;
60 public PrintingPermissionLevel Level{
61 get { return _Level; }
63 if (!Enum.IsDefined (typeof (PrintingPermissionLevel), value)) {
64 string msg = Locale.GetText ("Invalid enum {0}");
65 throw new ArgumentException (String.Format (msg, value), "Level");
73 public override IPermission Copy ()
75 return new PrintingPermission (this.Level);
78 public override void FromXml (SecurityElement esd)
80 CheckSecurityElement (esd, "esd", version, version);
81 // Note: we do not (yet) care about the return value
82 // as we only accept version 1 (min/max values)
84 if (IsUnrestricted (esd))
85 _Level = PrintingPermissionLevel.AllPrinting;
87 string level = esd.Attribute ("Level");
89 _Level = (PrintingPermissionLevel) Enum.Parse (
90 typeof (PrintingPermissionLevel), level);
93 _Level = PrintingPermissionLevel.NoPrinting;
97 public override IPermission Intersect (IPermission target)
99 PrintingPermission pp = Cast (target);
100 if ((pp == null) || IsEmpty () || pp.IsEmpty ())
103 PrintingPermissionLevel level = (_Level <= pp.Level) ? _Level : pp.Level;
104 return new PrintingPermission (level);
107 public override bool IsSubsetOf (IPermission target)
109 PrintingPermission pp = Cast (target);
113 return (_Level <= pp.Level);
116 public bool IsUnrestricted ()
118 return (_Level == PrintingPermissionLevel.AllPrinting);
121 public override SecurityElement ToXml ()
123 SecurityElement se = Element (version);
124 if (IsUnrestricted ())
125 se.AddAttribute ("Unrestricted", "true");
127 se.AddAttribute ("Level", _Level.ToString ());
131 public override IPermission Union (IPermission target)
133 PrintingPermission pp = Cast (target);
135 return new PrintingPermission (_Level);
136 if (IsUnrestricted () || pp.IsUnrestricted ())
137 return new PrintingPermission (PermissionState.Unrestricted);
138 if (IsEmpty () && pp.IsEmpty ())
141 PrintingPermissionLevel level = (_Level > pp.Level) ? _Level : pp.Level;
142 return new PrintingPermission (level);
145 // Internal helpers methods
147 private bool IsEmpty ()
149 return (_Level == PrintingPermissionLevel.NoPrinting);
152 private PrintingPermission Cast (IPermission target)
157 PrintingPermission pp = (target as PrintingPermission);
159 ThrowInvalidPermission (target, typeof (PrintingPermission));
165 // NOTE: The following static methods should be moved out to a (static?) class
166 // if (ever) System.Drawing.dll gets more than one permission in it's assembly.
168 // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
169 internal SecurityElement Element (int version)
171 SecurityElement se = new SecurityElement ("IPermission");
172 Type type = this.GetType ();
173 se.AddAttribute ("class", type.FullName + ", " + type.Assembly.ToString ().Replace ('\"', '\''));
174 se.AddAttribute ("version", version.ToString ());
178 internal static PermissionState CheckPermissionState (PermissionState state, bool allowUnrestricted)
182 case PermissionState.None:
184 case PermissionState.Unrestricted:
185 if (!allowUnrestricted) {
186 msg = Locale.GetText ("Unrestricted isn't not allowed for identity permissions.");
187 throw new ArgumentException (msg, "state");
191 msg = String.Format (Locale.GetText ("Invalid enum {0}"), state);
192 throw new ArgumentException (msg, "state");
197 // logic isn't identical to CodeAccessPermission.CheckSecurityElement - see unit tests
198 internal static int CheckSecurityElement (SecurityElement se, string parameterName, int minimumVersion, int maximumVersion)
201 throw new ArgumentNullException (parameterName);
203 string c = se.Attribute ("class");
206 string msg = Locale.GetText ("Missing 'class' attribute.");
207 throw new ArgumentException (msg, parameterName);
210 if ((c == null) || (String.Compare (c, 0, "System.Drawing.Printing.PrintingPermission", 0, 42) != 0)) {
211 string msg = Locale.GetText ("Wrong 'class' attribute.");
212 throw new ArgumentException (msg, parameterName);
215 // we assume minimum version if no version number is supplied
216 int version = minimumVersion;
217 string v = se.Attribute ("version");
220 version = Int32.Parse (v);
222 catch (Exception e) {
223 string msg = Locale.GetText ("Couldn't parse version from '{0}'.");
224 msg = String.Format (msg, v);
225 throw new ArgumentException (msg, parameterName, e);
230 string msg = Locale.GetText ("Missing 'version' attribute.");
231 throw new ArgumentException (msg, parameterName);
235 if ((version < minimumVersion) || (version > maximumVersion)) {
236 string msg = Locale.GetText ("Unknown version '{0}', expected versions between ['{1}','{2}'].");
237 msg = String.Format (msg, version, minimumVersion, maximumVersion);
238 throw new ArgumentException (msg, parameterName);
243 // must be called after CheckSecurityElement (i.e. se != null)
244 internal static bool IsUnrestricted (SecurityElement se)
246 string value = se.Attribute ("Unrestricted");
249 return (String.Compare (value, Boolean.TrueString, true, CultureInfo.InvariantCulture) == 0);
252 internal static void ThrowInvalidPermission (IPermission target, Type expected)
254 string msg = Locale.GetText ("Invalid permission type '{0}', expected type '{1}'.");
255 msg = String.Format (msg, target.GetType (), expected);
256 throw new ArgumentException (msg, "target");