StorePermission.cs 4.3 KB

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