DataProtectionPermission.cs 4.4 KB

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