/
User.cs
419 lines (341 loc) · 15.8 KB
/
User.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Security.Principal;
namespace System.DirectoryServices.AccountManagement
{
[DirectoryRdnPrefix("CN")]
public class UserPrincipal : AuthenticablePrincipal
{
//
// Public constructors
//
public UserPrincipal(PrincipalContext context) : base(context)
{
if (context == null)
throw new ArgumentException(SR.NullArguments);
this.ContextRaw = context;
this.unpersisted = true;
}
public UserPrincipal(PrincipalContext context, string samAccountName, string password, bool enabled) : this(context)
{
if (samAccountName == null || password == null)
throw new ArgumentException(SR.NullArguments);
if (Context.ContextType != ContextType.ApplicationDirectory)
this.SamAccountName = samAccountName;
this.Name = samAccountName;
this.SetPassword(password);
this.Enabled = enabled;
}
//
// Public properties
//
// GivenName
private string _givenName; // the actual property value
private LoadState _givenNameChanged = LoadState.NotSet; // change-tracking
public string GivenName
{
get
{
return HandleGet<string>(ref _givenName, PropertyNames.UserGivenName, ref _givenNameChanged);
}
set
{
if (!GetStoreCtxToUse().IsValidProperty(this, PropertyNames.UserGivenName))
throw new InvalidOperationException(SR.InvalidPropertyForStore);
HandleSet<string>(ref _givenName, value, ref _givenNameChanged,
PropertyNames.UserGivenName);
}
}
// MiddleName
private string _middleName; // the actual property value
private LoadState _middleNameChanged = LoadState.NotSet; // change-tracking
public string MiddleName
{
get
{
return HandleGet<string>(ref _middleName, PropertyNames.UserMiddleName, ref _middleNameChanged);
}
set
{
if (!GetStoreCtxToUse().IsValidProperty(this, PropertyNames.UserMiddleName))
throw new InvalidOperationException(SR.InvalidPropertyForStore);
HandleSet<string>(ref _middleName, value, ref _middleNameChanged,
PropertyNames.UserMiddleName);
}
}
// Surname
private string _surname; // the actual property value
private LoadState _surnameChanged = LoadState.NotSet; // change-tracking
public string Surname
{
get
{
return HandleGet<string>(ref _surname, PropertyNames.UserSurname, ref _surnameChanged);
}
set
{
if (!GetStoreCtxToUse().IsValidProperty(this, PropertyNames.UserSurname))
throw new InvalidOperationException(SR.InvalidPropertyForStore);
HandleSet<string>(ref _surname, value, ref _surnameChanged,
PropertyNames.UserSurname);
}
}
// EmailAddress
private string _emailAddress; // the actual property value
private LoadState _emailAddressChanged = LoadState.NotSet; // change-tracking
public string EmailAddress
{
get
{
return HandleGet<string>(ref _emailAddress, PropertyNames.UserEmailAddress, ref _emailAddressChanged);
}
set
{
if (!GetStoreCtxToUse().IsValidProperty(this, PropertyNames.UserEmailAddress))
throw new InvalidOperationException(SR.InvalidPropertyForStore);
HandleSet<string>(ref _emailAddress, value, ref _emailAddressChanged,
PropertyNames.UserEmailAddress);
}
}
// VoiceTelephoneNumber
private string _voiceTelephoneNumber; // the actual property value
private LoadState _voiceTelephoneNumberChanged = LoadState.NotSet; // change-tracking
public string VoiceTelephoneNumber
{
get
{
return HandleGet<string>(ref _voiceTelephoneNumber, PropertyNames.UserVoiceTelephoneNumber, ref _voiceTelephoneNumberChanged);
}
set
{
if (!GetStoreCtxToUse().IsValidProperty(this, PropertyNames.UserVoiceTelephoneNumber))
throw new InvalidOperationException(SR.InvalidPropertyForStore);
HandleSet<string>(ref _voiceTelephoneNumber, value, ref _voiceTelephoneNumberChanged,
PropertyNames.UserVoiceTelephoneNumber);
}
}
// EmployeeId
private string _employeeID; // the actual property value
private LoadState _employeeIDChanged = LoadState.NotSet; // change-tracking
public string EmployeeId
{
get
{
return HandleGet<string>(ref _employeeID, PropertyNames.UserEmployeeID, ref _employeeIDChanged);
}
set
{
if (!GetStoreCtxToUse().IsValidProperty(this, PropertyNames.UserEmployeeID))
throw new InvalidOperationException(SR.InvalidPropertyForStore);
HandleSet<string>(ref _employeeID, value, ref _employeeIDChanged,
PropertyNames.UserEmployeeID);
}
}
public override AdvancedFilters AdvancedSearchFilter { get { return rosf; } }
public static UserPrincipal Current
{
get
{
PrincipalContext context;
// Get the correct PrincipalContext to query against, depending on whether we're running
// as a local or domain user
if (Utils.IsSamUser())
{
GlobalDebug.WriteLineIf(GlobalDebug.Info, "User", "Current: is local user");
context = new PrincipalContext(ContextType.Machine);
}
else
{
GlobalDebug.WriteLineIf(GlobalDebug.Info, "User", "Current: is not local user");
context = new PrincipalContext(ContextType.Domain);
}
// Construct a query for the current user, using a SID IdentityClaim
IntPtr pSid = IntPtr.Zero;
UserPrincipal user = null;
try
{
pSid = Utils.GetCurrentUserSid();
byte[] sid = Utils.ConvertNativeSidToByteArray(pSid);
SecurityIdentifier sidObj = new SecurityIdentifier(sid, 0);
GlobalDebug.WriteLineIf(GlobalDebug.Info, "User", "Current: using SID " + sidObj.ToString());
user = UserPrincipal.FindByIdentity(context, IdentityType.Sid, sidObj.ToString());
}
finally
{
if (pSid != IntPtr.Zero)
System.Runtime.InteropServices.Marshal.FreeHGlobal(pSid);
}
// We're running as the user, we know they must exist, but perhaps we don't have access
// to their user object
if (user == null)
{
GlobalDebug.WriteLineIf(GlobalDebug.Warn, "User", "Current: found no user");
throw new NoMatchingPrincipalException(SR.UserCouldNotFindCurrent);
}
return user;
}
}
//
// Public methods
//
public static new PrincipalSearchResult<UserPrincipal> FindByLockoutTime(PrincipalContext context, DateTime time, MatchType type)
{
return FindByLockoutTime<UserPrincipal>(context, time, type);
}
public static new PrincipalSearchResult<UserPrincipal> FindByLogonTime(PrincipalContext context, DateTime time, MatchType type)
{
return FindByLogonTime<UserPrincipal>(context, time, type);
}
public static new PrincipalSearchResult<UserPrincipal> FindByExpirationTime(PrincipalContext context, DateTime time, MatchType type)
{
return FindByExpirationTime<UserPrincipal>(context, time, type);
}
public static new PrincipalSearchResult<UserPrincipal> FindByBadPasswordAttempt(PrincipalContext context, DateTime time, MatchType type)
{
return FindByBadPasswordAttempt<UserPrincipal>(context, time, type);
}
public static new PrincipalSearchResult<UserPrincipal> FindByPasswordSetTime(PrincipalContext context, DateTime time, MatchType type)
{
return FindByPasswordSetTime<UserPrincipal>(context, time, type);
}
public static new UserPrincipal FindByIdentity(PrincipalContext context, string identityValue)
{
return (UserPrincipal)FindByIdentityWithType(context, typeof(UserPrincipal), identityValue);
}
public static new UserPrincipal FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
{
return (UserPrincipal)FindByIdentityWithType(context, typeof(UserPrincipal), identityType, identityValue);
}
public PrincipalSearchResult<Principal> GetAuthorizationGroups()
{
return new PrincipalSearchResult<Principal>(GetAuthorizationGroupsHelper());
}
//
// Internal "constructor": Used for constructing Users returned by a query
//
internal static UserPrincipal MakeUser(PrincipalContext ctx)
{
UserPrincipal u = new UserPrincipal(ctx);
u.unpersisted = false;
return u;
}
//
// Private implementation
//
private ResultSet GetAuthorizationGroupsHelper()
{
// Make sure we're not disposed or deleted.
CheckDisposedOrDeleted();
// Unpersisted principals are not members of any group
if (this.unpersisted)
{
GlobalDebug.WriteLineIf(GlobalDebug.Info, "User", "GetAuthorizationGroupsHelper: unpersisted, using EmptySet");
return new EmptySet();
}
StoreCtx storeCtx = GetStoreCtxToUse();
Debug.Assert(storeCtx != null);
GlobalDebug.WriteLineIf(
GlobalDebug.Info,
"User",
"GetAuthorizationGroupsHelper: retrieving AZ groups from StoreCtx of type=" + storeCtx.GetType().ToString() +
", base path=" + storeCtx.BasePath);
ResultSet resultSet = storeCtx.GetGroupsMemberOfAZ(this);
return resultSet;
}
//
// Load/Store implementation
//
//
// Loading with query results
//
internal override void LoadValueIntoProperty(string propertyName, object value)
{
if (value == null)
{
GlobalDebug.WriteLineIf(GlobalDebug.Info, "User", "LoadValueIntoProperty: name=" + propertyName + " value= null");
}
else
{
GlobalDebug.WriteLineIf(GlobalDebug.Info, "User", "LoadValueIntoProperty: name=" + propertyName + " value=" + value.ToString());
}
switch (propertyName)
{
case (PropertyNames.UserGivenName):
_givenName = (string)value;
_givenNameChanged = LoadState.Loaded;
break;
case (PropertyNames.UserMiddleName):
_middleName = (string)value;
_middleNameChanged = LoadState.Loaded;
break;
case (PropertyNames.UserSurname):
_surname = (string)value;
_surnameChanged = LoadState.Loaded;
break;
case (PropertyNames.UserEmailAddress):
_emailAddress = (string)value;
_emailAddressChanged = LoadState.Loaded;
break;
case (PropertyNames.UserVoiceTelephoneNumber):
_voiceTelephoneNumber = (string)value;
_voiceTelephoneNumberChanged = LoadState.Loaded;
break;
case (PropertyNames.UserEmployeeID):
_employeeID = (string)value;
_employeeIDChanged = LoadState.Loaded;
break;
default:
base.LoadValueIntoProperty(propertyName, value);
break;
}
}
//
// Getting changes to persist (or to build a query from a QBE filter)
//
// Given a property name, returns true if that property has changed since it was loaded, false otherwise.
internal override bool GetChangeStatusForProperty(string propertyName)
{
GlobalDebug.WriteLineIf(GlobalDebug.Info, "User", "GetChangeStatusForProperty: name=" + propertyName);
return propertyName switch
{
PropertyNames.UserGivenName => _givenNameChanged == LoadState.Changed,
PropertyNames.UserMiddleName => _middleNameChanged == LoadState.Changed,
PropertyNames.UserSurname => _surnameChanged == LoadState.Changed,
PropertyNames.UserEmailAddress => _emailAddressChanged == LoadState.Changed,
PropertyNames.UserVoiceTelephoneNumber => _voiceTelephoneNumberChanged == LoadState.Changed,
PropertyNames.UserEmployeeID => _employeeIDChanged == LoadState.Changed,
_ => base.GetChangeStatusForProperty(propertyName),
};
}
// Given a property name, returns the current value for the property.
internal override object GetValueForProperty(string propertyName)
{
GlobalDebug.WriteLineIf(GlobalDebug.Info, "User", "GetValueForProperty: name=" + propertyName);
return propertyName switch
{
PropertyNames.UserGivenName => _givenName,
PropertyNames.UserMiddleName => _middleName,
PropertyNames.UserSurname => _surname,
PropertyNames.UserEmailAddress => _emailAddress,
PropertyNames.UserVoiceTelephoneNumber => _voiceTelephoneNumber,
PropertyNames.UserEmployeeID => _employeeID,
_ => base.GetValueForProperty(propertyName),
};
}
// Reset all change-tracking status for all properties on the object to "unchanged".
internal override void ResetAllChangeStatus()
{
GlobalDebug.WriteLineIf(GlobalDebug.Info, "User", "ResetAllChangeStatus");
_givenNameChanged = (_givenNameChanged == LoadState.Changed) ? LoadState.Loaded : LoadState.NotSet;
_middleNameChanged = (_middleNameChanged == LoadState.Changed) ? LoadState.Loaded : LoadState.NotSet;
_surnameChanged = (_surnameChanged == LoadState.Changed) ? LoadState.Loaded : LoadState.NotSet;
_emailAddressChanged = (_emailAddressChanged == LoadState.Changed) ? LoadState.Loaded : LoadState.NotSet;
_voiceTelephoneNumberChanged = (_voiceTelephoneNumberChanged == LoadState.Changed) ? LoadState.Loaded : LoadState.NotSet;
_employeeIDChanged = (_employeeIDChanged == LoadState.Changed) ? LoadState.Loaded : LoadState.NotSet;
base.ResetAllChangeStatus();
}
}
}