You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

136 lines
5.3 KiB

  1. using System.Security.AccessControl;
  2. namespace Security2
  3. {
  4. public class SimpleFileSystemAccessRule
  5. {
  6. private string fullName;
  7. private IdentityReference2 identity;
  8. private FileSystemRights2 accessRights;
  9. private AccessControlType type;
  10. public AccessControlType AccessControlType
  11. {
  12. get { return type; }
  13. set { type = value; }
  14. }
  15. public string FullName
  16. {
  17. get { return fullName; }
  18. }
  19. public string Name
  20. {
  21. get
  22. {
  23. return System.IO.Path.GetFileName(fullName);
  24. }
  25. }
  26. public IdentityReference2 Identity
  27. {
  28. get { return identity; }
  29. }
  30. public SimpleFileSystemAccessRights AccessRights
  31. {
  32. get
  33. {
  34. SimpleFileSystemAccessRights result = SimpleFileSystemAccessRights.None;
  35. if ((accessRights & FileSystemRights2.Read) == FileSystemRights2.Read)
  36. { result |= SimpleFileSystemAccessRights.Read; }
  37. if ((accessRights & FileSystemRights2.CreateFiles) == FileSystemRights2.CreateFiles)
  38. { result |= SimpleFileSystemAccessRights.Write; }
  39. if ((accessRights & FileSystemRights2.AppendData) == FileSystemRights2.AppendData)
  40. { result |= SimpleFileSystemAccessRights.Write; }
  41. if ((accessRights & FileSystemRights2.ReadExtendedAttributes) == FileSystemRights2.ReadExtendedAttributes)
  42. { result |= SimpleFileSystemAccessRights.Read; }
  43. if ((accessRights & FileSystemRights2.WriteExtendedAttributes) == FileSystemRights2.WriteExtendedAttributes)
  44. { result |= SimpleFileSystemAccessRights.Write; }
  45. if ((accessRights & FileSystemRights2.ExecuteFile) == FileSystemRights2.ExecuteFile)
  46. { result |= SimpleFileSystemAccessRights.Read; }
  47. if ((accessRights & FileSystemRights2.DeleteSubdirectoriesAndFiles) == FileSystemRights2.DeleteSubdirectoriesAndFiles)
  48. { result |= SimpleFileSystemAccessRights.Delete; }
  49. if ((accessRights & FileSystemRights2.ReadAttributes) == FileSystemRights2.ReadAttributes)
  50. { result |= SimpleFileSystemAccessRights.Read; }
  51. if ((accessRights & FileSystemRights2.WriteAttributes) == FileSystemRights2.WriteAttributes)
  52. { result |= SimpleFileSystemAccessRights.Write; }
  53. if ((accessRights & FileSystemRights2.Delete) == FileSystemRights2.Delete)
  54. { result |= SimpleFileSystemAccessRights.Delete; }
  55. if ((accessRights & FileSystemRights2.ReadPermissions) == FileSystemRights2.ReadPermissions)
  56. { result |= SimpleFileSystemAccessRights.Read; }
  57. if ((accessRights & FileSystemRights2.ChangePermissions) == FileSystemRights2.ChangePermissions)
  58. { result |= SimpleFileSystemAccessRights.Write; }
  59. if ((accessRights & FileSystemRights2.TakeOwnership) == FileSystemRights2.TakeOwnership)
  60. { result |= SimpleFileSystemAccessRights.Write; }
  61. if ((accessRights & FileSystemRights2.Synchronize) == FileSystemRights2.Synchronize)
  62. { result |= SimpleFileSystemAccessRights.Read; }
  63. if ((accessRights & FileSystemRights2.FullControl) == FileSystemRights2.FullControl)
  64. { result = (SimpleFileSystemAccessRights.Write | SimpleFileSystemAccessRights.Read | SimpleFileSystemAccessRights.Delete); }
  65. if ((accessRights & FileSystemRights2.GenericRead) == FileSystemRights2.GenericRead)
  66. { result |= SimpleFileSystemAccessRights.Read; }
  67. if ((accessRights & FileSystemRights2.GenericWrite) == FileSystemRights2.GenericWrite)
  68. { result |= SimpleFileSystemAccessRights.Write; }
  69. if ((accessRights & FileSystemRights2.GenericExecute) == FileSystemRights2.GenericExecute)
  70. { result |= SimpleFileSystemAccessRights.Read; }
  71. if ((accessRights & FileSystemRights2.GenericAll) == FileSystemRights2.GenericAll)
  72. { result = (SimpleFileSystemAccessRights.Write | SimpleFileSystemAccessRights.Read | SimpleFileSystemAccessRights.Delete); }
  73. return result;
  74. }
  75. }
  76. public SimpleFileSystemAccessRule(string path, IdentityReference2 account, FileSystemRights2 access, AccessControlType accessControlType)
  77. {
  78. fullName = path;
  79. accessRights = access;
  80. identity = account;
  81. type = accessControlType;
  82. }
  83. public override bool Equals(object obj)
  84. {
  85. var compareObject = obj as SimpleFileSystemAccessRule;
  86. if (compareObject == null)
  87. {
  88. return false;
  89. }
  90. if (this.AccessRights == compareObject.AccessRights && this.Identity == compareObject.Identity && this.AccessControlType == compareObject.AccessControlType)
  91. {
  92. return true;
  93. }
  94. else
  95. {
  96. return false;
  97. }
  98. }
  99. public override int GetHashCode()
  100. {
  101. return this.Identity.GetHashCode() | this.AccessRights.GetHashCode() | this.AccessControlType.GetHashCode();
  102. }
  103. }
  104. }