Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

FileSystemAuditRule2.AddFileSystemAuditRules.cs 4.5 KiB

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