25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

271 satır
9.3 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 Alphaleonis.Win32.Filesystem;
  10. using Security2;
  11. namespace NTFSSecurity
  12. {
  13. public partial class ShowSimpleAccessForm : 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 ShowSimpleAccessForm()
  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 btnSearchForIdentity_Click(object sender, EventArgs e)
  178. {
  179. trvDirectories.HideSelection = false;
  180. searchResultByFolder = null;
  181. searchResultByFolderIndex = 0;
  182. searchResultByFolder = FindNodeByIdentity((DirectoryTreeNode)trvDirectories.Nodes[0], txtSearchForIdentity.Text);
  183. if (searchResultByFolder.Count() > 0)
  184. {
  185. btnSearchForIdentityNext.Enabled = true;
  186. btnSearchForIdentityPrev.Enabled = true;
  187. trvDirectories.SelectedNode = searchResultByFolder.First();
  188. searchResultByFolderIndex = 0;
  189. }
  190. else
  191. {
  192. btnSearchForIdentityNext.Enabled = false;
  193. btnSearchForIdentityPrev.Enabled = false;
  194. }
  195. }
  196. private void btnSearchForIdentityPrev_Click(object sender, EventArgs e)
  197. {
  198. if (searchResultByFolder.Count() > searchResultByFolderIndex + 1)
  199. {
  200. searchResultByFolderIndex++;
  201. trvDirectories.SelectedNode = searchResultByFolder.ElementAt(searchResultByFolderIndex);
  202. }
  203. }
  204. private void btnSearchForIdentityNext_Click(object sender, EventArgs e)
  205. {
  206. if (searchResultByFolderIndex - 1 > -1)
  207. {
  208. searchResultByFolderIndex--;
  209. trvDirectories.SelectedNode = searchResultByFolder.ElementAt(searchResultByFolderIndex);
  210. }
  211. }
  212. }
  213. public class DirectoryTreeNode : TreeNode
  214. {
  215. private IList<SimpleFileSystemAccessRule> acl;
  216. public IList<SimpleFileSystemAccessRule> Acl
  217. {
  218. get { return acl; }
  219. }
  220. public DirectoryTreeNode(string key, string text)
  221. : base(text)
  222. {
  223. this.Name = key;
  224. }
  225. public DirectoryTreeNode(string key, string text, IQueryable<SimpleFileSystemAccessRule> acl)
  226. : this(key, text)
  227. {
  228. this.acl = acl.ToList();
  229. if (acl.Count() == 0)
  230. {
  231. this.ForeColor = Color.Gray;
  232. }
  233. }
  234. }
  235. }