Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

222 linhas
7.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using Security2;
  11. namespace NTFSSecurity
  12. {
  13. public partial class ShowSimpleEffectiveAccessForm : Form
  14. {
  15. private IEnumerable<DirectoryInfo> directoryList;
  16. private IQueryable<SimpleFileSystemAccessRule> aceList;
  17. private IEnumerable<DirectoryTreeNode> searchResultByFolder;
  18. private int searchResultByFolderIndex;
  19. public IEnumerable<DirectoryInfo> DirectoryList
  20. {
  21. get { return directoryList; }
  22. }
  23. public IEnumerable<SimpleFileSystemAccessRule> AceList
  24. {
  25. get { return aceList; }
  26. }
  27. public ShowSimpleEffectiveAccessForm()
  28. {
  29. InitializeComponent();
  30. }
  31. public void BuildDirectoryTreeNodes()
  32. {
  33. string previousPath = string.Empty;
  34. DirectoryTreeNode previousNode;
  35. DirectoryTreeNode rootNode;
  36. var rootDirectory = directoryList.First();
  37. rootNode = new DirectoryTreeNode(rootDirectory.FullName, rootDirectory.Name, aceList.Where(ace => ace.FullName == rootDirectory.Name));
  38. previousNode = rootNode;
  39. trvDirectories.Nodes.Add(rootNode);
  40. foreach (var directory in directoryList.Skip(1))
  41. {
  42. if (previousNode.Name == directory.GetParent().FullName)
  43. {
  44. var node = new DirectoryTreeNode(directory.FullName, directory.Name, aceList.Where(ace => ace.FullName == directory.FullName));
  45. previousNode.Nodes.Add(node);
  46. }
  47. else
  48. {
  49. previousNode = (DirectoryTreeNode)trvDirectories.Nodes.Find(directory.GetParent().FullName, true).FirstOrDefault();
  50. if (previousNode != null)
  51. {
  52. var node = new DirectoryTreeNode(directory.FullName, directory.Name, aceList.Where(ace => ace.FullName == directory.FullName));
  53. previousNode.Nodes.Add(node);
  54. }
  55. else
  56. {
  57. var node = new DirectoryTreeNode(directory.FullName, directory.Name, aceList.Where(ace => ace.FullName == directory.FullName));
  58. previousNode.Nodes.Add(node);
  59. }
  60. }
  61. }
  62. }
  63. public void BuildDirectoryTreeNodes(IEnumerable<DirectoryInfo> directoryList, IQueryable<SimpleFileSystemAccessRule> aceList)
  64. {
  65. this.directoryList = directoryList;
  66. this.aceList = aceList;
  67. this.BuildDirectoryTreeNodes();
  68. }
  69. private void trvDirectories_AfterSelect(object sender, TreeViewEventArgs e)
  70. {
  71. lstPermissions.Items.Clear();
  72. foreach (var ace in aceList.Where(ace => ace.FullName == e.Node.Name))
  73. {
  74. ListViewItem listItem = new ListViewItem();
  75. listItem.Name = e.Node.Name;
  76. listItem.Text = ace.Identity.AccountName;
  77. listItem.SubItems.AddRange(new string[] { ace.AccessRights.ToString(), ace.AccessControlType.ToString(), e.Node.FullPath });
  78. listItem.ImageIndex = 1;
  79. lstPermissions.Items.Add(listItem);
  80. }
  81. if (lstPermissions.Items.Count > 0)
  82. {
  83. foreach (ColumnHeader column in lstPermissions.Columns)
  84. {
  85. column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
  86. }
  87. }
  88. }
  89. private void chkRemoveFoldersWithoutAccess_CheckedChanged(object sender, EventArgs e)
  90. {
  91. if (chkRemoveFoldersWithoutAccess.Checked)
  92. {
  93. RemoveFoldersWithoutAccess((DirectoryTreeNode)trvDirectories.Nodes[0]);
  94. }
  95. else
  96. {
  97. trvDirectories.Nodes.Clear();
  98. BuildDirectoryTreeNodes();
  99. }
  100. }
  101. public void RemoveFoldersWithoutAccess(DirectoryTreeNode node)
  102. {
  103. for (int i = 0; i < node.Nodes.Count; i++)
  104. {
  105. if (node.Nodes[i].GetNodeCount(false) > 0)
  106. RemoveFoldersWithoutAccess((DirectoryTreeNode)node.Nodes[i]);
  107. if (node.Nodes[i].Nodes.Count == 0 & ((DirectoryTreeNode)node.Nodes[i]).Acl.Count() == 0)
  108. {
  109. node.Nodes.Remove(node.Nodes[i]);
  110. i--;
  111. }
  112. }
  113. }
  114. private void btnSearchForFolder_Click(object sender, EventArgs e)
  115. {
  116. searchResultByFolder = null;
  117. searchResultByFolderIndex = 0;
  118. trvDirectories.HideSelection = false;
  119. searchResultByFolder = FindNodeByFolder((DirectoryTreeNode)trvDirectories.Nodes[0], txtSearchForFolder.Text);
  120. if (searchResultByFolder.Count() > 0)
  121. {
  122. btnSearchForFolderNext.Enabled = true;
  123. btnSearchForFolderPrev.Enabled = true;
  124. trvDirectories.SelectedNode = searchResultByFolder.First();
  125. searchResultByFolderIndex = 0;
  126. }
  127. else
  128. {
  129. btnSearchForFolderNext.Enabled = false;
  130. btnSearchForFolderPrev.Enabled = false;
  131. }
  132. }
  133. private IEnumerable<DirectoryTreeNode> FindNodeByFolder(DirectoryTreeNode node, string search)
  134. {
  135. if (node.Text.ToLower().Contains(search.ToLower()))
  136. {
  137. yield return node;
  138. }
  139. foreach (DirectoryTreeNode childNode in node.Nodes)
  140. {
  141. foreach (DirectoryTreeNode match in this.FindNodeByFolder(childNode, search))
  142. {
  143. yield return match;
  144. }
  145. }
  146. }
  147. private IEnumerable<DirectoryTreeNode> FindNodeByIdentity(DirectoryTreeNode node, string search)
  148. {
  149. if (node.Acl.Where(ace => ace.Identity.AccountName.ToLower().Contains(search.ToLower())).Count() > 0)
  150. {
  151. yield return node;
  152. }
  153. foreach (DirectoryTreeNode childNode in node.Nodes)
  154. {
  155. foreach (DirectoryTreeNode match in this.FindNodeByIdentity(childNode, search))
  156. {
  157. yield return match;
  158. }
  159. }
  160. }
  161. private void btnSearchForFolderNext_Click(object sender, EventArgs e)
  162. {
  163. if (searchResultByFolder.Count() > searchResultByFolderIndex + 1)
  164. {
  165. searchResultByFolderIndex++;
  166. trvDirectories.SelectedNode = searchResultByFolder.ElementAt(searchResultByFolderIndex);
  167. }
  168. }
  169. private void btnSearchForFolderPrev_Click(object sender, EventArgs e)
  170. {
  171. if (searchResultByFolderIndex - 1 > -1)
  172. {
  173. searchResultByFolderIndex--;
  174. trvDirectories.SelectedNode = searchResultByFolder.ElementAt(searchResultByFolderIndex);
  175. }
  176. }
  177. private void btnSearchForIdentityPrev_Click(object sender, EventArgs e)
  178. {
  179. if (searchResultByFolder.Count() > searchResultByFolderIndex + 1)
  180. {
  181. searchResultByFolderIndex++;
  182. trvDirectories.SelectedNode = searchResultByFolder.ElementAt(searchResultByFolderIndex);
  183. }
  184. }
  185. private void btnSearchForIdentityNext_Click(object sender, EventArgs e)
  186. {
  187. if (searchResultByFolderIndex - 1 > -1)
  188. {
  189. searchResultByFolderIndex--;
  190. trvDirectories.SelectedNode = searchResultByFolder.ElementAt(searchResultByFolderIndex);
  191. }
  192. }
  193. }
  194. }