-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
248 lines (215 loc) · 8.75 KB
/
Form1.cs
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
using Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Management.Automation;
using System.Security;
using System.Windows.Forms;
namespace NewAdUser {
public partial class Form1 : Form {
private const string DefaultPassword = "Zasqw12";
private Helpers.TransliterationType TransType;
private List<SqlInstance> SqlInstances;
private Splash StartupSplash;
private bool FormLoaded = false;
private SqlConnection Sql;
public Form1() {
this.StartupSplash = Splash.ShowSplash();
InitializeComponent();
SetupVars();
this.StartupSplash.Status = "Рисуем форму";
}
private void SetupVars() {
//checkTrustedHosts();
this.StartupSplash.Status = "Получаем список доменов";
this.comboBoxAdDomain.DataSource = Enum.GetValues(typeof(Domain.AdDomain));
this.StartupSplash.Status = "Получаем список почтовых доменов";
this.comboBoxMailDomain.DataSource = Enum.GetValues(typeof(Domain.MailDomain));
this.StartupSplash.Status = "Получаем список инстансов в сети";
#if DEBUG
this.SqlInstances = SqlInstance.GetSqlServersFromNetwork(debug: true);
#else
this.SqlInstances = SqlInstance.GetSqlServersFromNetwork(debug: false);
#endif
this.comboBoxInstances.DataSource = this.SqlInstances;
this.comboBoxInstances.DisplayMember = "InstanceFullName";
this.comboBoxInstances.SelectedIndex = -1;
this.checkBoxAddMail.Checked = true;
this.checkBoxPassword.Checked = true;
checkBox1_CheckedChanged(null, null);
checkBoxAddMail_CheckedChanged(null, null);
this.TransType = Helpers.TransliterationType.Gost;
}
private void checkTrustedHosts() {
throw new NotImplementedException();
}
private void Form1_Load(object sender, EventArgs e) {
}
private void textBoxLastname_TextChanged(object sender, EventArgs e) {
SetLogin();
SetFullName();
}
private void textBoxName_TextChanged(object sender, EventArgs e) {
SetLogin();
SetFullName();
}
private void textBoxSurname_TextChanged(object sender, EventArgs e) {
SetFullName();
}
private void SetFullName() {
this.textBoxFullname.Text = ComposeFullName();
}
private void SetLogin() {
this.textBoxLogin.Text = ComposeLogin();
}
private string ComposeLogin() {
string result = string.Empty;
if (!string.IsNullOrEmpty(this.textBoxName.Text)) {
result += Helpers.Transliteration.Front(this.textBoxName.Text.Substring(0, 1), this.TransType);
}
if (!string.IsNullOrEmpty(this.textBoxSurName.Text)) {
result += Helpers.Transliteration.Front(this.textBoxSurName.Text, this.TransType);
}
return result;
}
private string ComposeFullName() {
string result = string.Empty;
if (!string.IsNullOrEmpty(this.textBoxSurName.Text)) {
result += $"{this.textBoxSurName.Text}";
}
if (!string.IsNullOrEmpty(this.textBoxName.Text)) {
result += $" {this.textBoxName.Text}";
}
if (!string.IsNullOrEmpty(this.textBoxSecondName.Text)) {
result += $" {this.textBoxSecondName.Text}";
}
return result;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e) {
this.textBoxUserPassword.Enabled = !this.checkBoxPassword.Checked;
this.textBoxUserPassword.Text = this.checkBoxPassword.Checked ? DefaultPassword : String.Empty;
}
private void gOSTToolStripMenuItem_CheckedChanged(object sender, EventArgs e) {
if (sender is ToolStripMenuItem m) {
if (m.Checked) {
ChangeTransitType(Helpers.TransliterationType.Gost);
this.gOSTToolStripMenuItem.Checked = true;
this.iSOToolStripMenuItem.Checked = !this.gOSTToolStripMenuItem.Checked;
}
}
}
private void ChangeTransitType(Helpers.TransliterationType tType) {
this.TransType = tType;
SetLogin();
}
private void iSOToolStripMenuItem_CheckedChanged(object sender, EventArgs e) {
if (sender is ToolStripMenuItem m) {
if (m.Checked) {
ChangeTransitType(Helpers.TransliterationType.ISO);
this.iSOToolStripMenuItem.Checked = true;
this.gOSTToolStripMenuItem.Checked = false;
}
}
}
private void checkBoxAddMail_CheckedChanged(object sender, EventArgs e) {
this.comboBoxMailDomain.Enabled = this.checkBoxAddMail.Checked;
this.checkBoxOnlyMail.Enabled = this.checkBoxAddMail.Checked;
this.checkBoxOnlyMail.Checked = false;
}
private void Form1_Shown(object sender, EventArgs e) {
this.StartupSplash.CloseSplash();
Focus();
Activate();
this.FormLoaded = true;
}
private void comboBoxInstances_SelectedIndexChanged(object sender, EventArgs e) {
if (!this.FormLoaded) return;
if (sender is ComboBox cmbx) {
SqlInstance instance = cmbx.SelectedItem as SqlInstance;
string ConnectionString = (instance.InstanceName == "DEFAULT") ? $@"Server={instance.Host.FQDN};Database=KB;Trusted_Connection=True;Connection Timeout=2;" : $@"Server={instance.InstanceFullName};Database=KB;Trusted_Connection=True;Connection Timeout=2;";
if (this.Sql == null) this.Sql = new SqlConnection(ConnectionString);
switch (this.Sql.State) {
case ConnectionState.Open:
case ConnectionState.Connecting:
case ConnectionState.Executing:
case ConnectionState.Fetching:
case ConnectionState.Broken:
this.Sql.Close();
this.Sql = new SqlConnection(ConnectionString);
break;
}
this.Sql.Open();
List<Roles> roles = GetRoles(instance);
if (roles.Any()) {
this.listBoxRoles.Enabled = true;
this.listBoxRoles.DataSource = roles;
this.listBoxRoles.DisplayMember = "RoleName";
this.listBoxRoles.ClearSelected();
}
else {
this.listBoxRoles.Items.Clear();
this.listBoxRoles.Enabled = false;
}
List<KBMenu> menus = GetMenus(instance);
if (roles.Any()) {
this.listBoxMenu.Enabled = true;
this.listBoxMenu.DataSource = menus;
this.listBoxMenu.DisplayMember = "MenuName";
this.listBoxMenu.ClearSelected();
}
else {
this.listBoxMenu.Items.Clear();
this.listBoxMenu.Enabled = false;
}
}
}
private void buttonExit_Click(object sender, EventArgs e) {
Close();
}
private void buttonAddUser_Click(object sender, EventArgs e) {
string runasUsername;
string runasPassword;
Splash splash = Splash.ShowSplash();
splash.Status = "Выбираем домены.";
switch ((Domain.AdDomain)this.comboBoxAdDomain.SelectedIndex) {
case Domain.AdDomain.Formulabi:
runasUsername = AccountDetails.FormulaLogin;
runasPassword = AccountDetails.FormulaPassword;
break;
case Domain.AdDomain.Radarias:
runasUsername = AccountDetails.RadarLogin;
runasPassword = AccountDetails.RadarPassword;
break;
default:
throw new ArgumentOutOfRangeException();
}
splash.Status = "Секурим пароли.";
SecureString ssRunasPassword = new SecureString();
foreach (char x in runasPassword) {
ssRunasPassword.AppendChar(x);
}
splash.Status = "Создаём учётные данные.";
PSCredential credentials = new PSCredential(runasUsername, ssRunasPassword);
splash.Status = "Генерим шаблон пользователя.";
AdUser NewUser = new AdUser(this.textBoxLogin.Text, this.textBoxName.Text, this.textBoxSecondName.Text, this.textBoxSurName.Text, (Domain.AdDomain)this.comboBoxAdDomain.SelectedIndex, (Domain.MailDomain)this.comboBoxMailDomain.SelectedIndex);
if (!this.checkBoxPassword.Checked) NewUser.Password = this.textBoxUserPassword.Text;
splash.Status = "Начинаем создание пользователя.";
if (!this.checkBoxOnlyMail.Checked) {
Helpers.AddADUser(NewUser, credentials, splash);
}
if (this.checkBoxAddMail.Checked) {
string ExchangeUsername = AccountDetails.FbiLogin;
string ExchangePassword = AccountDetails.FbiPassword;
SecureString SecExchangePassword = new SecureString();
foreach (char x in ExchangePassword) {
SecExchangePassword.AppendChar(x);
}
PSCredential ExchangeCreds = new PSCredential(ExchangeUsername, SecExchangePassword);
Helpers.AddMailUser(NewUser, ExchangeCreds, splash);
}
splash?.CloseSplash();
}
}
}