-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserEditForm.cs
249 lines (195 loc) · 8.07 KB
/
UserEditForm.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
249
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace AdUserManager
{
public partial class UserEditForm : Form
{
public UserEditForm()
{
InitializeComponent();
}
List<GroupPrincipal> managedGroups;
bool isNewUser;
UserPrincipal user;
List<GroupPrincipal> userGroups;
public void LoadManagedGroups(IEnumerable<GroupPrincipal> managedGroups)
{
this.managedGroups = new List<GroupPrincipal>(managedGroups);
groupListBox.Items.Clear();
groupListBox.DisplayMember = "Name";
foreach (var group in managedGroups)
groupListBox.Items.Add(group.Name);
}
public void LoadUser(UserPrincipal user, IEnumerable<GroupPrincipal> userGroups)
{
this.user = user;
this.userGroups = new List<GroupPrincipal>(userGroups);
isNewUser = user.Guid == null;
logonNameTextBox.ReadOnly = !isNewUser;
firstNameTextBox.Text = user.GivenName;
lastNameTextBox.Text = user.Surname;
displayNameTextBox.Text = user.DisplayName;
logonNameTextBox.Text = user.SamAccountName;
emailTextBox.Text = user.EmailAddress;
enabledCheckBox.Checked = user.Enabled == true || user.Enabled == null;
bool expires = user.AccountExpirationDate.HasValue;
var expiresRadio = expires ? expiresEndOfRadioButton : expiresNeverRadioButton;
expiresRadio.Checked = true;
DateTime expirationDate;
if (isNewUser && Properties.Settings.Default.expiration_enabled)
expiresEndOfRadioButton.Checked = true;
if (expires)
expirationDate = user.AccountExpirationDate.Value.AddDays(-1);
else
expirationDate = Utils.getNextExpirationDate();
expirationDateTimePicker.Value = expirationDate;
logonNameTextBox.ReadOnly = !isNewUser;
for (var i = 0; i < managedGroups.Count; i++)
{
var check = userGroups.Contains(managedGroups[i]);
while (groupListBox.GetItemChecked(i) != check)
groupListBox.SetItemChecked(i, check);
}
}
private string CapitalizeFirst(string input)
{
if (input == "")
return "";
return input[0].ToString().ToUpper() + input.Substring(1);
}
private string EditTextBox(TextBox textBox, Func<string, string> func)
{
var text = textBox.Text;
if (text.Length == 0)
return text;
if (char.IsLower(text[0]))
{
var selpos = textBox.SelectionStart;
var sellen = textBox.SelectionLength;
text = textBox.Text = func(text);
textBox.SelectionStart = selpos;
textBox.SelectionLength = sellen;
}
return text;
}
private void nameTextBox_TextChanged(object sender, EventArgs e)
{
var fname = EditTextBox(firstNameTextBox, CapitalizeFirst);
var lname = EditTextBox(lastNameTextBox, CapitalizeFirst);
var fLetter = (fname == "") ? "" : fname.Substring(0, 1);
displayNameTextBox.Text = fname + " " + lname;
if (isNewUser)
logonNameTextBox.Text = Unaccent(fLetter + lname);
}
private void logonNameTextBox_TextChanged(object sender, EventArgs e)
{
EditTextBox(logonNameTextBox, (string str) => RemoveSpecialChars(Unaccent(str)));
}
private void groupListBox_Click(object sender, EventArgs e)
{
var clientClickPos = groupListBox.PointToClient(Cursor.Position);
if (groupListBox.IndexFromPoint(clientClickPos) <= -1)
{
groupListBox.SelectedItem = null;
}
}
private string Unaccent(string input)
{
var normalizedString = input.Normalize(NormalizationForm.FormD);
var stringBuilder = new StringBuilder();
foreach (var c in normalizedString)
{
var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
if (unicodeCategory != UnicodeCategory.NonSpacingMark)
{
stringBuilder.Append(c);
}
}
return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
}
private string RemoveSpecialChars(string input)
{
string regExp = "[^A-Za-z]";
return Regex.Replace(input, regExp, "");
}
private (UserPrincipal, List<GroupPrincipal>) ModifyUser()
{
if (string.IsNullOrWhiteSpace(firstNameTextBox.Text))
throw new ApplicationException("First name cannot be empty");
if (string.IsNullOrWhiteSpace(lastNameTextBox.Text))
throw new ApplicationException("Last name cannot be empty");
if (string.IsNullOrWhiteSpace(firstNameTextBox.Text))
throw new ApplicationException("Logon name cannot be empty");
user.GivenName = firstNameTextBox.Text;
user.Surname = lastNameTextBox.Text;
user.DisplayName = displayNameTextBox.Text;
if (isNewUser)
{
// Do not change logon name for existing users
user.SamAccountName = Unaccent(logonNameTextBox.Text);
user.UserPrincipalName = user.SamAccountName + "@" + LdapUtils.DomainName;
user.Name = user.DisplayName;
}
else
{
// Update user.Name
var dirEntry = (DirectoryEntry) user.GetUnderlyingObject();
dirEntry.Rename("CN=" + user.DisplayName);
}
user.EmailAddress = emailTextBox.Text;
user.Enabled = enabledCheckBox.Checked;
if (expiresEndOfRadioButton.Checked)
user.AccountExpirationDate = expirationDateTimePicker.Value.AddDays(1);
else
user.AccountExpirationDate = null;
if (string.IsNullOrWhiteSpace(user.EmailAddress))
user.EmailAddress = null;
userGroups = managedGroups.Where((g, i) => groupListBox.CheckedIndices.Contains(i)).ToList();
return (user, userGroups);
}
private void cancelButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void saveButton_Click(object sender, EventArgs e)
{
try
{
// TODO validate userPrincipalName collision
var userTuple = ModifyUser();
var user = userTuple.Item1;
var userGroups = userTuple.Item2;
user.Save();
foreach (var group in managedGroups)
{
if (userGroups.Contains(group) && !group.Members.Contains(user))
{
group.Members.Add(user);
group.Save();
}
else if (!userGroups.Contains(group) && group.Members.Contains(user))
{
group.Members.Remove(user);
group.Save();
}
}
}
catch (Exception ex)
{
MessageBox.Show(this, "Error saving user\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Logging.logWarning($"Error saving user\n{ex.Message}");
return;
}
DialogResult = DialogResult.OK;
Close();
}
}
}