DataProtectionPermissionAttribute.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // System.Security.Permissions.DataProtectionPermissionAttribute class
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2004 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. [AttributeUsage (AttributeTargets.Assembly | AttributeTargets.Class |
  31. AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method,
  32. AllowMultiple = true, Inherited = false)]
  33. [Serializable]
  34. public sealed class DataProtectionPermissionAttribute : CodeAccessSecurityAttribute {
  35. private DataProtectionPermissionFlags _flags;
  36. public DataProtectionPermissionAttribute (SecurityAction action)
  37. : base (action)
  38. {
  39. }
  40. public DataProtectionPermissionFlags Flags {
  41. get { return _flags; }
  42. set {
  43. if ((value & DataProtectionPermissionFlags.AllFlags) != value) {
  44. string msg = String.Format (Locale.GetText ("Invalid flags {0}"), value);
  45. throw new ArgumentException (msg, "DataProtectionPermissionFlags");
  46. }
  47. _flags = value;
  48. }
  49. }
  50. public bool ProtectData {
  51. get { return ((_flags & DataProtectionPermissionFlags.ProtectData) != 0); }
  52. set {
  53. if (value) {
  54. _flags |= DataProtectionPermissionFlags.ProtectData;
  55. }
  56. else {
  57. _flags &= ~DataProtectionPermissionFlags.ProtectData;
  58. }
  59. }
  60. }
  61. public bool UnprotectData {
  62. get { return ((_flags & DataProtectionPermissionFlags.UnprotectData) != 0); }
  63. set {
  64. if (value) {
  65. _flags |= DataProtectionPermissionFlags.UnprotectData;
  66. }
  67. else {
  68. _flags &= ~DataProtectionPermissionFlags.UnprotectData;
  69. }
  70. }
  71. }
  72. public bool ProtectMemory {
  73. get { return ((_flags & DataProtectionPermissionFlags.ProtectMemory) != 0); }
  74. set {
  75. if (value) {
  76. _flags |= DataProtectionPermissionFlags.ProtectMemory;
  77. }
  78. else {
  79. _flags &= ~DataProtectionPermissionFlags.ProtectMemory;
  80. }
  81. }
  82. }
  83. public bool UnprotectMemory {
  84. get { return ((_flags & DataProtectionPermissionFlags.UnprotectMemory) != 0); }
  85. set {
  86. if (value) {
  87. _flags |= DataProtectionPermissionFlags.UnprotectMemory;
  88. }
  89. else {
  90. _flags &= ~DataProtectionPermissionFlags.UnprotectMemory;
  91. }
  92. }
  93. }
  94. public override IPermission CreatePermission ()
  95. {
  96. DataProtectionPermission perm = null;
  97. if (this.Unrestricted)
  98. perm = new DataProtectionPermission (PermissionState.Unrestricted);
  99. else
  100. perm = new DataProtectionPermission (_flags);
  101. return perm;
  102. }
  103. }
  104. }