AspNetHostingPermission.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // System.Web.AspNetHostingPermission.cs
  3. //
  4. // Authors:
  5. // Andreas Nahr ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_1_1
  30. using System.Security;
  31. using System.Security.Permissions;
  32. namespace System.Web {
  33. [Serializable]
  34. public sealed class AspNetHostingPermission : CodeAccessPermission, IUnrestrictedPermission {
  35. private const int version = 1;
  36. private AspNetHostingPermissionLevel _level;
  37. public AspNetHostingPermission (AspNetHostingPermissionLevel level)
  38. {
  39. // use the property to get the enum validation
  40. Level = level;
  41. }
  42. public AspNetHostingPermission (PermissionState state)
  43. {
  44. if (PermissionHelper.CheckPermissionState (state, true) == PermissionState.Unrestricted)
  45. _level = AspNetHostingPermissionLevel.Unrestricted;
  46. else
  47. _level = AspNetHostingPermissionLevel.None;
  48. }
  49. public AspNetHostingPermissionLevel Level {
  50. get { return _level; }
  51. set {
  52. if ((value < AspNetHostingPermissionLevel.None) || (value > AspNetHostingPermissionLevel.Unrestricted)) {
  53. string msg = Locale.GetText ("Invalid enum {0}.");
  54. throw new ArgumentException (String.Format (msg, value), "Level");
  55. }
  56. _level = value;
  57. }
  58. }
  59. public bool IsUnrestricted ()
  60. {
  61. return (_level == AspNetHostingPermissionLevel.Unrestricted);
  62. }
  63. public override IPermission Copy ()
  64. {
  65. // note: no need to handle unrestricted here
  66. return new AspNetHostingPermission (_level);
  67. }
  68. public override void FromXml (SecurityElement securityElement)
  69. {
  70. PermissionHelper.CheckSecurityElement (securityElement, "securityElement", version, version);
  71. if (securityElement.Tag != "IPermission") {
  72. string msg = Locale.GetText ("Invalid tag '{0}' for permission.");
  73. throw new ArgumentException (String.Format (msg, securityElement.Tag), "securityElement");
  74. }
  75. if (securityElement.Attribute ("version") == null) {
  76. string msg = Locale.GetText ("Missing version attribute.");
  77. throw new ArgumentException (msg, "securityElement");
  78. }
  79. if (PermissionHelper.IsUnrestricted (securityElement)) {
  80. // in case it's get fixed later...
  81. _level = AspNetHostingPermissionLevel.Unrestricted;
  82. }
  83. else {
  84. string level = securityElement.Attribute ("Level");
  85. if (level != null) {
  86. _level = (AspNetHostingPermissionLevel) Enum.Parse (
  87. typeof (AspNetHostingPermissionLevel), level);
  88. }
  89. else
  90. _level = AspNetHostingPermissionLevel.None;
  91. }
  92. }
  93. public override SecurityElement ToXml ()
  94. {
  95. SecurityElement se = PermissionHelper.Element (typeof (AspNetHostingPermission), version);
  96. #if NET_2_0
  97. if (IsUnrestricted ())
  98. se.AddAttribute ("Unrestricted", "true"); // FDBK15156 fixed in 2.0 RC
  99. #endif
  100. se.AddAttribute ("Level", _level.ToString ());
  101. return se;
  102. }
  103. public override IPermission Intersect (IPermission target)
  104. {
  105. AspNetHostingPermission anhp = Cast (target);
  106. if (anhp == null)
  107. return null;
  108. return new AspNetHostingPermission ((_level <= anhp.Level) ? _level : anhp.Level);
  109. }
  110. public override bool IsSubsetOf (IPermission target)
  111. {
  112. AspNetHostingPermission anhp = Cast (target);
  113. if (anhp == null)
  114. return IsEmpty ();
  115. return (_level <= anhp._level);
  116. }
  117. public override IPermission Union (IPermission target)
  118. {
  119. AspNetHostingPermission anhp = Cast (target);
  120. if (anhp == null)
  121. return Copy ();
  122. return new AspNetHostingPermission ((_level > anhp.Level) ? _level : anhp.Level);
  123. }
  124. // Internal helpers methods
  125. private bool IsEmpty ()
  126. {
  127. return (_level == AspNetHostingPermissionLevel.None);
  128. }
  129. private AspNetHostingPermission Cast (IPermission target)
  130. {
  131. if (target == null)
  132. return null;
  133. AspNetHostingPermission anhp = (target as AspNetHostingPermission);
  134. if (anhp == null) {
  135. PermissionHelper.ThrowInvalidPermission (target, typeof (AspNetHostingPermission));
  136. }
  137. return anhp;
  138. }
  139. }
  140. }
  141. #endif