AspNetHostingPermission.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // System.Web.AspNetHostingPermission.cs
  3. //
  4. // Authors:
  5. // Andreas Nahr ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2004 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 (!Enum.IsDefined (typeof (AspNetHostingPermissionLevel), value)) {
  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. // FIXME: attribute "unrestricted" isn't used by this class - reported as FDBK15156
  97. se.AddAttribute ("Level", _level.ToString ());
  98. return se;
  99. }
  100. public override IPermission Intersect (IPermission target)
  101. {
  102. AspNetHostingPermission anhp = Cast (target);
  103. if (anhp == null)
  104. return null;
  105. return new AspNetHostingPermission ((_level <= anhp.Level) ? _level : anhp.Level);
  106. }
  107. public override bool IsSubsetOf (IPermission target)
  108. {
  109. AspNetHostingPermission anhp = Cast (target);
  110. if (anhp == null)
  111. return IsEmpty ();
  112. return (_level <= anhp._level);
  113. }
  114. public override IPermission Union (IPermission target)
  115. {
  116. AspNetHostingPermission anhp = Cast (target);
  117. if (anhp == null)
  118. return Copy ();
  119. return new AspNetHostingPermission ((_level > anhp.Level) ? _level : anhp.Level);
  120. }
  121. // Internal helpers methods
  122. private bool IsEmpty ()
  123. {
  124. return (_level == AspNetHostingPermissionLevel.None);
  125. }
  126. private AspNetHostingPermission Cast (IPermission target)
  127. {
  128. if (target == null)
  129. return null;
  130. AspNetHostingPermission anhp = (target as AspNetHostingPermission);
  131. if (anhp == null) {
  132. PermissionHelper.ThrowInvalidPermission (target, typeof (AspNetHostingPermission));
  133. }
  134. return anhp;
  135. }
  136. }
  137. }
  138. #endif