-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActiveDirectoryOrganizationalUnit.cs
77 lines (68 loc) · 3.55 KB
/
ActiveDirectoryOrganizationalUnit.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
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Dynamic;
namespace ActiveDirectoryUtilities
{
// This class represents an Organizational Unit (OU) of ActiveDirectory
public class ActiveDirectoryOrganizationalUnit: DirectoryEntry
{
// Username and password of the user that will be used to perform the operations against ActiveDirectory
public string OperatingUsername { get; set; }
public string OperatingUserPassword { get; set; }
// Any child OUs
public List<ActiveDirectoryOrganizationalUnit> OrganizationalUnits { get; set; }
// If there are any users of the unit, they would be listed here
public List<DirectoryEntry> Users {get; set; }
protected ActiveDirectoryOrganizationalUnit()
{
}
// Inherit base constructors
public ActiveDirectoryOrganizationalUnit(string operatingUsername, string operatingUserPassword, string path, bool retrieveDescendants = false, int? depth = null)
: base(path, operatingUsername, operatingUserPassword)
{
OperatingUsername = operatingUsername;
OperatingUserPassword = operatingUserPassword;
if (string.IsNullOrWhiteSpace(OperatingUsername) || string.IsNullOrWhiteSpace(OperatingUserPassword))
{
throw new ArgumentException("OperatingUsername and OperatingPassword are both required");
}
OrganizationalUnits = new List<ActiveDirectoryOrganizationalUnit>();
Users = new List<DirectoryEntry>();
Populate(retrieveDescendants, depth);
}
public ActiveDirectoryOrganizationalUnit(ActiveDirectory activeDirectory, string path, bool retrieveDescendants = false, int? depth = null)
: this(activeDirectory.OperatingUsername, activeDirectory.OperatingUserPassword, path, retrieveDescendants, depth)
{
}
// Populates the organizational unit and optionally its descendants.
// `path` is the ActiveDirectory path of the organization unit. If none is provided, the whole ActiveDirectory OU will be returned
// `retrieveDescendants` specifies whether or not the function should retrieve the descendants of the unit
// `depth` specifies how many levels of descendants the function should retrieve
public ActiveDirectoryOrganizationalUnit Populate(bool retrieveDescendants = false, int? depth = null)
{
// Clear current users and OUs
Users = new List<DirectoryEntry>();
OrganizationalUnits = new List<ActiveDirectoryOrganizationalUnit>();
foreach (DirectoryEntry result in Children)
{
switch (result.SchemaClassName.ToLower())
{
case "user":
if (result.NativeGuid == null) continue;
var flags = (int)result.Properties["userAccountControl"].Value;
if (Convert.ToBoolean(flags & 0x0002)) continue;
Users.Add(result);
break;
case "organizationalunit":
if (retrieveDescendants && (depth == null || (int)depth >= 0))
{
OrganizationalUnits.Add(new ActiveDirectoryOrganizationalUnit(OperatingUsername, OperatingUserPassword, result.Path, true, depth - 1));
}
break;
}
}
return this;
}
}
}