PrintingPermission.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // System.Drawing.PrintingPermission.cs
  3. //
  4. // Authors:
  5. // Dennis Hayes ([email protected])
  6. // Herve Poussineau ([email protected])
  7. // Sebastien Pouliot <[email protected]>
  8. //
  9. // (C) 2002 Ximian, Inc
  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. using System.Globalization;
  32. using System.Security;
  33. using System.Security.Permissions;
  34. namespace System.Drawing.Printing {
  35. [Serializable]
  36. public sealed class PrintingPermission : CodeAccessPermission, IUnrestrictedPermission {
  37. private const int version = 1;
  38. private PrintingPermissionLevel _Level;
  39. public PrintingPermission (PermissionState state)
  40. {
  41. if (CheckPermissionState (state, true) == PermissionState.Unrestricted)
  42. _Level = PrintingPermissionLevel.AllPrinting;
  43. else
  44. _Level = PrintingPermissionLevel.NoPrinting;
  45. }
  46. public PrintingPermission (PrintingPermissionLevel printingLevel)
  47. {
  48. Level = printingLevel;
  49. }
  50. // properties
  51. public PrintingPermissionLevel Level{
  52. get { return _Level; }
  53. set {
  54. if (!Enum.IsDefined (typeof (PrintingPermissionLevel), value)) {
  55. string msg = Locale.GetText ("Invalid enum {0}");
  56. throw new ArgumentException (String.Format (msg, value), "Level");
  57. }
  58. _Level = value;
  59. }
  60. }
  61. // methods
  62. public override IPermission Copy ()
  63. {
  64. return new PrintingPermission (this.Level);
  65. }
  66. public override void FromXml (SecurityElement esd)
  67. {
  68. CheckSecurityElement (esd, "esd", version, version);
  69. // Note: we do not (yet) care about the return value
  70. // as we only accept version 1 (min/max values)
  71. if (IsUnrestricted (esd))
  72. _Level = PrintingPermissionLevel.AllPrinting;
  73. else {
  74. string level = esd.Attribute ("Level");
  75. if (level != null) {
  76. _Level = (PrintingPermissionLevel) Enum.Parse (
  77. typeof (PrintingPermissionLevel), level);
  78. }
  79. else
  80. _Level = PrintingPermissionLevel.NoPrinting;
  81. }
  82. }
  83. public override IPermission Intersect (IPermission target)
  84. {
  85. PrintingPermission pp = Cast (target);
  86. if ((pp == null) || IsEmpty () || pp.IsEmpty ())
  87. return null;
  88. PrintingPermissionLevel level = (_Level <= pp.Level) ? _Level : pp.Level;
  89. return new PrintingPermission (level);
  90. }
  91. public override bool IsSubsetOf (IPermission target)
  92. {
  93. PrintingPermission pp = Cast (target);
  94. if (pp == null)
  95. return IsEmpty ();
  96. return (_Level <= pp.Level);
  97. }
  98. public bool IsUnrestricted ()
  99. {
  100. return (_Level == PrintingPermissionLevel.AllPrinting);
  101. }
  102. public override SecurityElement ToXml ()
  103. {
  104. SecurityElement se = Element (version);
  105. if (IsUnrestricted ())
  106. se.AddAttribute ("Unrestricted", "true");
  107. else
  108. se.AddAttribute ("Level", _Level.ToString ());
  109. return se;
  110. }
  111. public override IPermission Union (IPermission target)
  112. {
  113. PrintingPermission pp = Cast (target);
  114. if (pp == null)
  115. return new PrintingPermission (_Level);
  116. if (IsUnrestricted () || pp.IsUnrestricted ())
  117. return new PrintingPermission (PermissionState.Unrestricted);
  118. if (IsEmpty () && pp.IsEmpty ())
  119. return null;
  120. PrintingPermissionLevel level = (_Level > pp.Level) ? _Level : pp.Level;
  121. return new PrintingPermission (level);
  122. }
  123. // Internal helpers methods
  124. private bool IsEmpty ()
  125. {
  126. return (_Level == PrintingPermissionLevel.NoPrinting);
  127. }
  128. private PrintingPermission Cast (IPermission target)
  129. {
  130. if (target == null)
  131. return null;
  132. PrintingPermission pp = (target as PrintingPermission);
  133. if (pp == null) {
  134. ThrowInvalidPermission (target, typeof (PrintingPermission));
  135. }
  136. return pp;
  137. }
  138. // NOTE: The following static methods should be moved out to a (static?) class
  139. // if (ever) System.Drawing.dll gets more than one permission in it's assembly.
  140. // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
  141. internal SecurityElement Element (int version)
  142. {
  143. SecurityElement se = new SecurityElement ("IPermission");
  144. Type type = this.GetType ();
  145. se.AddAttribute ("class", type.FullName + ", " + type.Assembly.ToString ().Replace ('\"', '\''));
  146. se.AddAttribute ("version", version.ToString ());
  147. return se;
  148. }
  149. internal static PermissionState CheckPermissionState (PermissionState state, bool allowUnrestricted)
  150. {
  151. string msg;
  152. switch (state) {
  153. case PermissionState.None:
  154. break;
  155. case PermissionState.Unrestricted:
  156. if (!allowUnrestricted) {
  157. msg = Locale.GetText ("Unrestricted isn't not allowed for identity permissions.");
  158. throw new ArgumentException (msg, "state");
  159. }
  160. break;
  161. default:
  162. msg = String.Format (Locale.GetText ("Invalid enum {0}"), state);
  163. throw new ArgumentException (msg, "state");
  164. }
  165. return state;
  166. }
  167. // logic isn't identical to CodeAccessPermission.CheckSecurityElement - see unit tests
  168. internal static int CheckSecurityElement (SecurityElement se, string parameterName, int minimumVersion, int maximumVersion)
  169. {
  170. if (se == null)
  171. throw new ArgumentNullException (parameterName);
  172. string c = se.Attribute ("class");
  173. #if NET_2_0
  174. if (c == null) {
  175. string msg = Locale.GetText ("Missing 'class' attribute.");
  176. throw new ArgumentException (msg, parameterName);
  177. }
  178. #else
  179. if ((c == null) || (String.Compare (c, 0, "System.Drawing.Printing.PrintingPermission", 0, 42) != 0)) {
  180. string msg = Locale.GetText ("Wrong 'class' attribute.");
  181. throw new ArgumentException (msg, parameterName);
  182. }
  183. #endif
  184. // we assume minimum version if no version number is supplied
  185. int version = minimumVersion;
  186. string v = se.Attribute ("version");
  187. if (v != null) {
  188. try {
  189. version = Int32.Parse (v);
  190. }
  191. catch (Exception e) {
  192. string msg = Locale.GetText ("Couldn't parse version from '{0}'.");
  193. msg = String.Format (msg, v);
  194. throw new ArgumentException (msg, parameterName, e);
  195. }
  196. }
  197. #if !NET_2_0
  198. else {
  199. string msg = Locale.GetText ("Missing 'version' attribute.");
  200. throw new ArgumentException (msg, parameterName);
  201. }
  202. #endif
  203. if ((version < minimumVersion) || (version > maximumVersion)) {
  204. string msg = Locale.GetText ("Unknown version '{0}', expected versions between ['{1}','{2}'].");
  205. msg = String.Format (msg, version, minimumVersion, maximumVersion);
  206. throw new ArgumentException (msg, parameterName);
  207. }
  208. return version;
  209. }
  210. // must be called after CheckSecurityElement (i.e. se != null)
  211. internal static bool IsUnrestricted (SecurityElement se)
  212. {
  213. string value = se.Attribute ("Unrestricted");
  214. if (value == null)
  215. return false;
  216. return (String.Compare (value, Boolean.TrueString, true, CultureInfo.InvariantCulture) == 0);
  217. }
  218. internal static void ThrowInvalidPermission (IPermission target, Type expected)
  219. {
  220. string msg = Locale.GetText ("Invalid permission type '{0}', expected type '{1}'.");
  221. msg = String.Format (msg, target.GetType (), expected);
  222. throw new ArgumentException (msg, "target");
  223. }
  224. }
  225. }