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.
 
 

122 line
5.0 KiB

  1. using Alphaleonis.Win32.Filesystem;
  2. using System.Collections.Generic;
  3. using System.Security.AccessControl;
  4. namespace Security2
  5. {
  6. public partial class FileSystemAccessRule2
  7. {
  8. public static FileSystemAccessRule2 AddFileSystemAccessRule(FileSystemSecurity2 sd, IdentityReference2 account, FileSystemRights2 rights, AccessControlType type, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags)
  9. {
  10. if (type == AccessControlType.Allow)
  11. rights = rights | FileSystemRights2.Synchronize;
  12. FileSystemAccessRule ace = null;
  13. if (sd.IsFile)
  14. {
  15. ace = (FileSystemAccessRule)sd.SecurityDescriptor.AccessRuleFactory(account, (int)rights, false, InheritanceFlags.None, PropagationFlags.None, type);
  16. ((FileSecurity)sd.SecurityDescriptor).AddAccessRule(ace);
  17. }
  18. else
  19. {
  20. ace = (FileSystemAccessRule)sd.SecurityDescriptor.AccessRuleFactory(account, (int)rights, false, inheritanceFlags, propagationFlags, type);
  21. ((DirectorySecurity)sd.SecurityDescriptor).AddAccessRule(ace);
  22. }
  23. return ace;
  24. }
  25. public static FileSystemAccessRule2 AddFileSystemAccessRule(FileSystemInfo item, IdentityReference2 account, FileSystemRights2 rights, AccessControlType type, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags)
  26. {
  27. if (type == AccessControlType.Allow)
  28. rights = rights | FileSystemRights2.Synchronize;
  29. var sd = new FileSystemSecurity2(item);
  30. var ace = AddFileSystemAccessRule(sd, account, rights, type, inheritanceFlags, propagationFlags);
  31. sd.Write();
  32. return ace;
  33. }
  34. public static IEnumerable<FileSystemAccessRule2> AddFileSystemAccessRule(FileSystemInfo item, List<IdentityReference2> accounts, FileSystemRights2 rights, AccessControlType type, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags)
  35. {
  36. var aces = new List<FileSystemAccessRule2>();
  37. foreach (var account in accounts)
  38. {
  39. aces.Add(AddFileSystemAccessRule(item, account, rights, type, inheritanceFlags, propagationFlags));
  40. }
  41. return aces;
  42. }
  43. public static IEnumerable<FileSystemAccessRule2> AddFileSystemAccessRule(FileSystemSecurity2 sd, List<IdentityReference2> accounts, FileSystemRights2 rights, AccessControlType type, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags)
  44. {
  45. var aces = new List<FileSystemAccessRule2>();
  46. foreach (var account in accounts)
  47. {
  48. aces.Add(AddFileSystemAccessRule(sd, account, rights, type, inheritanceFlags, propagationFlags));
  49. }
  50. return aces;
  51. }
  52. public static FileSystemAccessRule2 AddFileSystemAccessRule(string path, IdentityReference2 account, FileSystemRights2 rights, AccessControlType type, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags)
  53. {
  54. if (type == AccessControlType.Allow)
  55. rights = rights | FileSystemRights2.Synchronize;
  56. FileSystemAccessRule ace = null;
  57. if (File.Exists(path))
  58. {
  59. var item = new FileInfo(path);
  60. ace = AddFileSystemAccessRule(item, account, rights, type, inheritanceFlags, propagationFlags);
  61. }
  62. else
  63. {
  64. var item = new DirectoryInfo(path);
  65. ace = AddFileSystemAccessRule(item, account, rights, type, inheritanceFlags, propagationFlags);
  66. }
  67. return ace;
  68. }
  69. public static IEnumerable<FileSystemAccessRule2> AddFileSystemAccessRule(string path, List<IdentityReference2> accounts, FileSystemRights2 rights, AccessControlType type, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags)
  70. {
  71. if (type == AccessControlType.Allow)
  72. rights = rights | FileSystemRights2.Synchronize;
  73. if (File.Exists(path))
  74. {
  75. var item = new FileInfo(path);
  76. foreach (var account in accounts)
  77. {
  78. yield return AddFileSystemAccessRule(item, account, rights, type, inheritanceFlags, propagationFlags);
  79. }
  80. }
  81. else
  82. {
  83. var item = new DirectoryInfo(path);
  84. foreach (var account in accounts)
  85. {
  86. yield return AddFileSystemAccessRule(item, account, rights, type, inheritanceFlags, propagationFlags);
  87. }
  88. }
  89. }
  90. public static void AddFileSystemAccessRule(FileSystemAccessRule2 rule)
  91. {
  92. AddFileSystemAccessRule(rule.fullName,
  93. rule.Account,
  94. rule.AccessRights,
  95. rule.AccessControlType,
  96. rule.InheritanceFlags,
  97. rule.PropagationFlags);
  98. }
  99. }
  100. }