-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
287 lines (257 loc) · 10.1 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
287 lines (257 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.DirectoryServices.AccountManagement;
using System.ComponentModel;
using Microsoft.Win32;
using System.DirectoryServices.ActiveDirectory;
using Ookii.Dialogs.Wpf;
using System.Runtime.InteropServices;
namespace UserFilePermission
{
/// <summary>
/// Aplikace projde zadaný adresář včetně podadresářů do definované úrovně
/// a vypíše souborová oprávnění pro zvoleného uživatele.
/// Podle volby vypíše individuální oprávnšní nebo i oprávnění skupin,
/// ve kterých je členem.
/// Je určeno pro uživatele z Active Directory domeny a sdílené adresáře souborového serveru
/// Aplikaci je nutné pouštět s admin oprávněnímpro proghledávané složky
/// </summary>
/// <settings>
/// INI soubor config.ini je uložen v adresáři aplikace
/// Formát:
/// [Default setting]
/// domain= mydomain.local
/// levels = 3
/// directoryexcluded= _osoba, _archiv
/// </settings>
public partial class MainWindow : Window
{
// Třída pro prohledávání
CheckPermissions checkPermissions;
public MainWindow()
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
checkPermissions = new CheckPermissions();
InitializeComponent();
// Registrace událostí vlákna na pozadí
checkPermissions.BackgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged;
checkPermissions.BackgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
// Napojení formuláře na data
DataContext = checkPermissions;
dgVýsledek.DataContext = checkPermissions;
dgVýsledek.ItemsSource = checkPermissions.Vystup;
// Nastavení výchozích hodnot
IniCfg iniCfg = new IniCfg(".\\config.ini");
tbDomain.Text = iniCfg.IniReadValue("Default setting", "domain");
tbUserName.Text = "";
tbSearchDir.Text = "";
tbNumLevels.Text = iniCfg.IniReadValue("Default setting", "levels"); ;
tbDirectoryExcluded.Text = iniCfg.IniReadValue("Default setting", "directoryexcluded");
}
/// <summary>
/// Spuštění prohledávání
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btStart_Click(object sender, RoutedEventArgs e)
{
// Kontrola a nastavení vstupních parametrů
if (!SetInputParams())
return;
// Změna tlačítek
btStart.Visibility = Visibility.Hidden;
btStop.Visibility = Visibility.Visible;
//Spustí vlastní prohledávání
checkPermissions.FindPermission();
}
/// <summary>
/// Předčasné ukončení prohledávání
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btStop_Click(object sender, RoutedEventArgs e)
{
checkPermissions.StopFind();
// Změna tlačítek
btStart.Visibility = Visibility.Visible;
btStop.Visibility = Visibility.Hidden;
}
/// <summary>
/// Uložení nalezených oprávnění do CSV souboru
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btSaveLog_Click(object sender, RoutedEventArgs e)
{
System.IO.TextWriter txtWriter;
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "CSV file (*.csv)|*.csv";
if (saveFileDialog.ShowDialog() == true)
{
if (System.IO.File.Exists(saveFileDialog.FileName))
System.IO.File.Delete(saveFileDialog.FileName);
txtWriter = new System.IO.StreamWriter(saveFileDialog.FileName, true, Encoding.GetEncoding(1250));
txtWriter.WriteLine(String.Format("OPrávnění pro uživstele: {0} ve složce {1}", checkPermissions.UserName, checkPermissions.SearchDir));
txtWriter.WriteLine(checkPermissions.getVysledek(";"));
txtWriter.Flush();
txtWriter.Close();
}
}
/// <summary>
/// Kontrola vstupních parametrů a jejich uložení
/// </summary>
/// <returns>
/// bool
/// true - pramatry jsou správné
/// false - došlo k chybě
/// </returns>
private bool SetInputParams()
{
// Kontrola a uložení vstupích parametrů
if (DomainExist(tbDomain.Text))
checkPermissions.Domain = tbDomain.Text;
else
{
MessageBox.Show("Domena neexistuje!", "Chyba");
return false;
}
if (UserExists(tbUserName.Text))
checkPermissions.UserName = tbUserName.Text;
else
{
MessageBox.Show("Uživatel neexistuje!", "Chyba");
return false;
}
if (Directory.Exists(tbSearchDir.Text))
checkPermissions.SearchDir = tbSearchDir.Text;
else
{
MessageBox.Show("Vstupní adrešář neexistuje!", "Chyba");
return false;
}
int levels;
if ((int.TryParse(tbNumLevels.Text, out levels) && levels >= -1))
checkPermissions.NumLevels = levels;
else
{
MessageBox.Show("Pčet úrovní musí být číslo větší než -1", "Chyba");
return false;
}
checkPermissions.UserOnly = cbUserOnly.IsChecked.Value;
checkPermissions.DirectoryExcluded = tbDirectoryExcluded.Text.Replace(", ", ",").Split(",");
return true;
}
/// <summary>
/// Otestuje za zadaný uživetel existuje v AD
/// </summary>
/// <param name="usrName"></param>
/// <returns>bool</returns>
private bool UserExists(string usrName)
{
string strDomain = checkPermissions.Domain;
using (var domainContext = new PrincipalContext(ContextType.Domain, strDomain))
{
using (var user = new UserPrincipal(domainContext))
{
user.SamAccountName = usrName;
using (var pS = new PrincipalSearcher())
{
pS.QueryFilter = user;
using (PrincipalSearchResult<Principal> results = pS.FindAll())
{
if (results != null && results.Count() > 0)
{
return true;
}
}
}
}
}
return false;
}
/// <summary>
/// Otestuje zda AD domána existuje a je dostupná
/// </summary>
/// <param name="domain"></param>
/// <returns>bool</returns>
private bool DomainExist(string domain)
{
HashSet<string> domains = new HashSet<string>();
foreach (Domain d in Forest.GetCurrentForest().Domains)
{
domains.Add(d.Name.ToLower());
}
return domains.Contains(domain.ToLower());
}
private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
svVysledek.ScrollToBottom();
}
/// <summary>
/// Ukončení prohledávání
/// vlákno s vlastním prohledávání skončilo
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Prohlédávání skončilo", "Info");
btStart.Visibility = Visibility.Visible;
btStop.Visibility = Visibility.Hidden;
}
/// <summary>
/// Dialog pro výběr strtovacího adresáře
/// Vybraný adresář se nastavý do vstupního parametru
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btselDir_Click(object sender, RoutedEventArgs e)
{
var Dialog = new VistaFolderBrowserDialog();
if (Dialog.ShowDialog() == true)
{
tbSearchDir.Text = Dialog.SelectedPath;
}
}
}
/// <summary>
/// Obecná ttřída pro čtení a zapis do INI souboru
/// </summary>
/// <param>
/// string path - cesta a název INI souboru
/// </param>
/// <example>
/// Obecný formát INI souboru:
/// [section1]
/// key1= value
/// key2= value
/// [section2]
/// key3= value
/// </example>
public class IniCfg
{
public string path;
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
public IniCfg(string INIPath)
{
path = INIPath;
}
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
return temp.ToString();
}
}
}