PermissionHelper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // System.Security.Permissions.PermissionHelper.cs
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  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. using System.Globalization;
  29. namespace System.Security.Permissions {
  30. internal sealed class PermissionHelper {
  31. // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
  32. internal static SecurityElement Element (Type type, int version)
  33. {
  34. SecurityElement se = new SecurityElement ("IPermission");
  35. se.AddAttribute ("class", type.FullName + ", " + type.Assembly.ToString ().Replace ('\"', '\''));
  36. se.AddAttribute ("version", version.ToString ());
  37. return se;
  38. }
  39. internal static PermissionState CheckPermissionState (PermissionState state, bool allowUnrestricted)
  40. {
  41. string msg;
  42. switch (state) {
  43. case PermissionState.None:
  44. break;
  45. case PermissionState.Unrestricted:
  46. if (!allowUnrestricted) {
  47. msg = Locale.GetText ("Unrestricted isn't not allowed for identity permissions.");
  48. throw new ArgumentException (msg, "state");
  49. }
  50. break;
  51. default:
  52. msg = String.Format (Locale.GetText ("Invalid enum {0}"), state);
  53. throw new ArgumentException (msg, "state");
  54. }
  55. return state;
  56. }
  57. internal static int CheckSecurityElement (SecurityElement se, string parameterName, int minimumVersion, int maximumVersion)
  58. {
  59. if (se == null)
  60. throw new ArgumentNullException (parameterName);
  61. if (se.Attribute ("class") == null) {
  62. string msg = Locale.GetText ("Missing 'class' attribute.");
  63. throw new ArgumentException (msg, parameterName);
  64. }
  65. // we assume minimum version if no version number is supplied
  66. int version = minimumVersion;
  67. string v = se.Attribute ("version");
  68. if (v != null) {
  69. try {
  70. version = Int32.Parse (v);
  71. }
  72. catch (Exception e) {
  73. string msg = Locale.GetText ("Couldn't parse version from '{0}'.");
  74. msg = String.Format (msg, v);
  75. throw new ArgumentException (msg, parameterName, e);
  76. }
  77. }
  78. if ((version < minimumVersion) || (version > maximumVersion)) {
  79. string msg = Locale.GetText ("Unknown version '{0}', expected versions between ['{1}','{2}'].");
  80. msg = String.Format (msg, version, minimumVersion, maximumVersion);
  81. throw new ArgumentException (msg, parameterName);
  82. }
  83. return version;
  84. }
  85. // must be called after CheckSecurityElement (i.e. se != null)
  86. internal static bool IsUnrestricted (SecurityElement se)
  87. {
  88. string value = se.Attribute ("Unrestricted");
  89. if (value == null)
  90. return false;
  91. return (String.Compare (value, Boolean.TrueString, true, CultureInfo.InvariantCulture) == 0);
  92. }
  93. internal static void ThrowInvalidPermission (IPermission target, Type expected)
  94. {
  95. string msg = Locale.GetText ("Invalid permission type '{0}', expected type '{1}'.");
  96. msg = String.Format (msg, target.GetType (), expected);
  97. throw new ArgumentException (msg, "target");
  98. }
  99. }
  100. }