-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserOperations.cs
220 lines (191 loc) · 8.19 KB
/
UserOperations.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
using System.DirectoryServices.AccountManagement;
namespace ADSync
{
internal class UserOperations
{
PrincipalContext userPrincipalContext;
PrincipalContext groupPrincipalContext;
string domainName;
string wifiGroupName;
public UserOperations(
PrincipalContext userPrincipalContext,
PrincipalContext groupPrincipalContext,
string domainName,
string wifiGroupName
)
{
this.userPrincipalContext = userPrincipalContext;
this.groupPrincipalContext = groupPrincipalContext;
this.domainName = "@" + domainName;
this.wifiGroupName = wifiGroupName;
}
public bool CreateOrUpdateUser(
string logonName,
string firstName,
string lastName,
bool wifiAccess,
string mainGroup,
string password = ""
)
{
UserPrincipal usr = UserPrincipal.FindByIdentity(userPrincipalContext, logonName);
if (usr != null) // esiste gia
{
// (re)imposta first e last name
if (lastName == null | lastName.Length < 1)
{
Console.WriteLine("Invalid last name while updating user " + logonName);
return false;
}
if (firstName == null | firstName.Length < 1)
{
Console.WriteLine("Invalid first name while updating user " + logonName);
return false;
}
usr.GivenName = firstName;
usr.Surname = lastName;
usr.DisplayName = firstName + " " + lastName;
usr.Save();
// aggiorna lo stato di wifiaccess
try
{
if (wifiAccess)
{
GroupPrincipal wifiGroup = GroupPrincipal.FindByIdentity(groupPrincipalContext, wifiGroupName);
wifiGroup.Members.Add(groupPrincipalContext, IdentityType.SamAccountName, logonName);
wifiGroup.Save();
}
else
{
GroupPrincipal wifiGroup = GroupPrincipal.FindByIdentity(groupPrincipalContext, wifiGroupName);
wifiGroup.Members.Remove(groupPrincipalContext, IdentityType.SamAccountName, logonName);
wifiGroup.Save();
}
} catch { } // ignora qualsiasi eccezione (uno dei due blocchi da per forza errore siccome aggiorna alla cieca)
Console.WriteLine("Successfully updated user " + logonName);
return true;
}
// utente non esiste, effettua impostazione iniziale
UserPrincipal userPrincipal = new UserPrincipal(userPrincipalContext);
if (lastName == null | lastName.Length < 1)
{
Console.WriteLine("Invalid last name while creating user " + logonName);
return false;
}
if (firstName == null | firstName.Length < 1)
{
Console.WriteLine("Invalid first name while creating user " + logonName);
return false;
}
if (logonName == null | logonName.Length < 1)
{
Console.WriteLine("Invalid logon name while creating user " + logonName);
return false;
}
userPrincipal.GivenName = firstName;
userPrincipal.EmailAddress = logonName + domainName;
userPrincipal.SamAccountName = logonName;
userPrincipal.Surname = lastName;
userPrincipal.DisplayName = firstName + " " + lastName;
userPrincipal.UserPrincipalName = logonName + domainName;
// gestione della password
if (password != "" && password != null)
{
if(password.Length < 8)
{
Console.WriteLine("Password too short while creating user " + logonName);
return false;
}
// gestita autonomamente, impostala
userPrincipal.SetPassword(password);
}
else
{
// gestita da servizi esterni (il campo password viene lasciato vuoto)
userPrincipal.SetPassword(Guid.NewGuid().ToString()); // totalmente random (da impostare con call successiva)
userPrincipal.UserCannotChangePassword = true;
}
userPrincipal.PasswordNeverExpires = true;
userPrincipal.Enabled = true;
try
{
userPrincipal.Save(); // salva dati utente
}
catch (Exception ex)
{
Console.WriteLine("Exception raised while creating user " + logonName + " ... " + ex.ToString());
return false;
}
// aggiunta a gruppo di appartenenza
try
{
GroupPrincipal group = GroupPrincipal.FindByIdentity(groupPrincipalContext, mainGroup);
group.Members.Add(groupPrincipalContext, IdentityType.SamAccountName, logonName);
group.Save();
if (wifiAccess) // se ha l'accesso wifi provvedi
{
GroupPrincipal wifiGroup = GroupPrincipal.FindByIdentity(groupPrincipalContext, "AccessoWiFi");
wifiGroup.Members.Add(groupPrincipalContext, IdentityType.SamAccountName, logonName);
wifiGroup.Save();
}
}
catch (Exception ex)
{
Console.WriteLine("Exception raised while setting main group for user " + logonName + " ... " + ex.ToString());
return false;
}
Console.WriteLine("Successfully created user " + logonName);
return true;
}
public bool UpdatePassword(
string logonName, string password, bool selfManaged = true)
{
try
{
UserPrincipal usr = UserPrincipal.FindByIdentity(userPrincipalContext, logonName);
if (usr == null)
{
Console.WriteLine("Cannot update password for non existing user " + logonName);
return false;
}
if (password == "" | password == null | password.Length < 8)
{
Console.WriteLine("Password too short while updating user " + logonName);
return false;
}
usr.SetPassword(password);
usr.PasswordNeverExpires = true;
usr.Enabled = true;
if (!selfManaged) usr.UserCannotChangePassword = true;
usr.Save();
Console.WriteLine("Password successfully updated user " + logonName);
return true;
}
catch (Exception ex)
{
Console.WriteLine("Exception raised while updating password of user " + logonName + " ... " + ex.ToString());
return false;
}
}
public bool DeleteUser(string logonName)
{
try
{
UserPrincipal usr = UserPrincipal.FindByIdentity(userPrincipalContext, logonName);
if (usr == null)
{
Console.WriteLine("Cannot delete non existing user " + logonName);
return false;
}
usr.Delete();
Console.WriteLine("Successfully deleted user " + logonName);
return true;
}
catch (Exception ex)
{
Console.WriteLine("Exception raised while deleting user " + logonName + " ... " + ex.ToString());
return false;
}
}
}
}