DataProtectionPermission.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // System.Security.Permissions.DataProtectionPermission class
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2004-2005 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. [Serializable]
  31. public sealed class DataProtectionPermission : CodeAccessPermission, IUnrestrictedPermission {
  32. private const int version = 1;
  33. private DataProtectionPermissionFlags _flags;
  34. public DataProtectionPermission (PermissionState state)
  35. {
  36. if (PermissionHelper.CheckPermissionState (state, true) == PermissionState.Unrestricted)
  37. _flags = DataProtectionPermissionFlags.AllFlags;
  38. }
  39. public DataProtectionPermission (DataProtectionPermissionFlags flag)
  40. {
  41. // reuse validation by the Flags property
  42. Flags = flag;
  43. }
  44. public DataProtectionPermissionFlags Flags {
  45. get { return _flags; }
  46. set {
  47. if ((value & ~DataProtectionPermissionFlags.AllFlags) != 0) {
  48. string msg = String.Format (Locale.GetText ("Invalid enum {0}"), value);
  49. throw new ArgumentException (msg, "DataProtectionPermissionFlags");
  50. }
  51. _flags = value;
  52. }
  53. }
  54. public bool IsUnrestricted ()
  55. {
  56. return (_flags == DataProtectionPermissionFlags.AllFlags);
  57. }
  58. public override IPermission Copy ()
  59. {
  60. return new DataProtectionPermission (_flags);
  61. }
  62. public override IPermission Intersect (IPermission target)
  63. {
  64. DataProtectionPermission dp = Cast (target);
  65. if (dp == null)
  66. return null;
  67. if (this.IsUnrestricted () && dp.IsUnrestricted ())
  68. return new DataProtectionPermission (PermissionState.Unrestricted);
  69. if (this.IsUnrestricted ())
  70. return dp.Copy ();
  71. if (dp.IsUnrestricted ())
  72. return this.Copy ();
  73. return new DataProtectionPermission (_flags & dp._flags);
  74. }
  75. public override IPermission Union (IPermission target)
  76. {
  77. DataProtectionPermission dp = Cast (target);
  78. if (dp == null)
  79. return this.Copy ();
  80. if (this.IsUnrestricted () || dp.IsUnrestricted ())
  81. return new SecurityPermission (PermissionState.Unrestricted);
  82. return new DataProtectionPermission (_flags | dp._flags);
  83. }
  84. public override bool IsSubsetOf (IPermission target)
  85. {
  86. DataProtectionPermission dp = Cast (target);
  87. if (dp == null)
  88. return (_flags == DataProtectionPermissionFlags.NoFlags);
  89. if (dp.IsUnrestricted ())
  90. return true;
  91. if (this.IsUnrestricted ())
  92. return false;
  93. return ((_flags & ~dp._flags) == 0);
  94. }
  95. public override void FromXml (SecurityElement securityElement)
  96. {
  97. // General validation in CodeAccessPermission
  98. PermissionHelper.CheckSecurityElement (securityElement, "securityElement", version, version);
  99. // Note: we do not (yet) care about the return value
  100. // as we only accept version 1 (min/max values)
  101. _flags = (DataProtectionPermissionFlags) Enum.Parse (
  102. typeof (DataProtectionPermissionFlags), securityElement.Attribute ("Flags"));
  103. }
  104. public override SecurityElement ToXml ()
  105. {
  106. SecurityElement e = PermissionHelper.Element (typeof (DataProtectionPermission), version);
  107. e.AddAttribute ("Flags", _flags.ToString ());
  108. return e;
  109. }
  110. // helpers
  111. private DataProtectionPermission Cast (IPermission target)
  112. {
  113. if (target == null)
  114. return null;
  115. DataProtectionPermission dp = (target as DataProtectionPermission);
  116. if (dp == null) {
  117. PermissionHelper.ThrowInvalidPermission (target, typeof (DataProtectionPermission));
  118. }
  119. return dp;
  120. }
  121. }
  122. }