-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
160 lines (139 loc) · 6.31 KB
/
Program.cs
File metadata and controls
160 lines (139 loc) · 6.31 KB
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
using Microsoft.Extensions.Configuration;
using SimpletextingAPI.Services;
using SimpletextingAPI.Models;
using System.Linq;
using SimpletextingAPI;
// Load configuration
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
#if DEBUG
if (File.Exists("appsettings.Development.json"))
configurationBuilder.AddJsonFile("appsettings.Development.json");
#endif
var configuration = configurationBuilder.Build();
var domainController = configuration["DC"] ?? "";
var domain = configuration["Domain"] ?? "";
var username = configuration["Username"];
var password = configuration["Password"];
var ou = configuration["OU"] ?? $"OU=Users,DC={domain.Replace(".",",DC=")}";
var apiKey = configuration["ApiKey"] ?? "";
bool dryRun = Boolean.Parse(configuration["DryRun"] ?? "True");
bool debugMode = Boolean.Parse(configuration["Debug"] ?? "False");
var allUserListIds = configuration.GetSection("ListIds").Get<List<string>>();
if (Utils.IsBlank(apiKey))
{
Console.WriteLine("Must supply apiKey");
return;
}
if (allUserListIds == null || !allUserListIds.Any())
{
Console.WriteLine("Must specify at least one list Id");
return;
}
if (Utils.IsBlank(domain))
{
Console.WriteLine("Must specify an AD domain");
return;
}
// If specifiying a username you must supply password. If supplying a password you must specify a username.
if (Utils.IsBlank(username) && !Utils.IsBlank(password) || Utils.IsBlank(password) && !Utils.IsBlank(username))
{
Console.WriteLine($"Must supply a {(Utils.IsBlank(password) ? "password" : "username")}.");
}
if (dryRun)
{
Console.WriteLine("Starting a DRY RUN. Nothing will be written to the API.");
}
// Fetch Active Directory users
List<User> adUsers = [];
if (Utils.IsBlank(domainController))
{
if (Utils.IsBlank(username) || Utils.IsBlank(password))
adUsers = AdHelper.FetchAdUsers(ou);
else
adUsers = AdHelper.FetchAdUsers(username!, password!, ou);
}
else if (Utils.IsBlank(username) || Utils.IsBlank(password))
{
adUsers = AdHelper.FetchAdUsers(domainController, ou);
}
else
{
adUsers = AdHelper.FetchAdUsers(domainController, username!, password!, ou);
}
adUsers = adUsers.Where(u => !Utils.IsBlank(u.ContactPhone)).ToList();
adUsers.ForEach(u => u.ListNames = Utils.IsBlank(u.Office) ? allUserListIds : allUserListIds.Union(new List<string>() { u.Office! }).ToList() );
Console.WriteLine($"Found {adUsers.Count} existing enabled users with mobile numbers in Active Directory.");
// Fetch SimpleTexting users
List<User> apiUsers = await ApiHelper.FetchApiUsers(apiKey);
apiUsers.ForEach(u => u.ListNames = u.Lists.Select(l => l.Name).ToList());
Console.WriteLine($"Found {apiUsers.Count} existing contacts in SimpleTexting.");
// Discover available lists from users
List<ContactList> lists = await ApiHelper.FetchApiContactLists(apiKey);
Console.WriteLine($"Found {lists.Count} existing lists in SimpleTexting.");
// Add any non-existant lists
var adLists = adUsers.Select(u => u.Office).ToList().Distinct();
List<string> newLists = [.. adLists.Where(al => !lists.Select(l => l.Name).ToList().Distinct().Contains(al))];
if (newLists.Count > 0)
{
Console.WriteLine($"Found {newLists.Count} distinct offices from AD Users that do not match up with a list from SimpleTexting to be added:");
Console.WriteLine(string.Join("\n", newLists));
}
// New users
var usersToAdd = adUsers.Where(ad => !apiUsers.Any(api => api.ContactPhone == ad.ContactPhone)).ToList();
if (usersToAdd.Count > 0)
{
Console.WriteLine($"Found {usersToAdd.Count} new users not in SimpleTexting to be added:");
usersToAdd.OrderBy(u => u.LastName).ToList().ForEach(u => Console.WriteLine($"{u.FirstName} {u.LastName} ({u.Office}): {u.ContactPhone}"));
}
// Removed users
List<User> usersRemoved = apiUsers.Where(api => !adUsers.Any(ad => ad.ContactPhone == api.ContactPhone)).ToList();
if (usersRemoved.Count > 0)
{
Console.WriteLine($"Found {usersRemoved.Count} users in SimpleTexting no longer active in AD or no longer matching a mobile number to be removed:");
usersRemoved.OrderBy(u => u.LastName).ToList().ForEach(u => Console.WriteLine($"{u.FirstName} {u.LastName} ({u.ListString}): {u.ContactPhone}"));
}
// Users with updated name or new office
var usersToUpdate = adUsers.Where(ad =>
apiUsers.Any(api => api.ContactPhone == ad.ContactPhone &&
(api.FirstName != ad.FirstName || api.LastName != ad.LastName || !Utils.IsBlank(ad.Office) && !api.ListNames.Contains(ad.Office!)))).ToList();
if (usersToUpdate.Count > 0)
{
Console.WriteLine($"Found {usersToUpdate.Count} existing users with different Names or Offices in AD than SimpleTexting to be updated:");
foreach (var uu in usersToUpdate.OrderBy(u => u.LastName))
{
var old = apiUsers.Where(u => u.ContactPhone == uu.ContactPhone).First();
Console.WriteLine($"{old.FirstName} {old.LastName} ({old.ListString}) -> {uu.FirstName} {uu.LastName} ({uu.ListString})");
}
}
// Add list names to each user
usersToAdd.ForEach(user => { user.ListNames = allUserListIds.Union(lists.Where(l => l.Name == user.Office).Select(l => l.Name)).ToList(); });
usersToUpdate.ForEach(user => { user.ListNames = allUserListIds.Union(lists.Where(l => l.Name == user.Office).Select(l => l.Name)).ToList(); });
if (debugMode)
{
// Print AD users
Console.WriteLine("\nActive Directory Users:");
foreach (var userObj in adUsers.OrderBy(u => u.LastName))
{
Console.WriteLine($"Name: {userObj.FirstName} {userObj.LastName}, Phone: {userObj.ContactPhone}");
}
// Print SimpleTexting users
Console.WriteLine("\nSimpleTexting Users:");
foreach (var apiUser in apiUsers.OrderBy(u => u.LastName))
{
Console.WriteLine($"Name: {apiUser.FirstName} {apiUser.LastName}, Phone: {apiUser.ContactPhone}");
}
}
// Write to the API
if (!dryRun)
{
Console.WriteLine($"Adding {newLists.Count} new lists");
await ApiHelper.CreateLists(apiKey, newLists);
Console.WriteLine($"Removing {usersRemoved.Count} phone numbers (users).");
await ApiHelper.RemoveUsers(apiKey, usersRemoved);
Console.WriteLine($"Updating {usersToUpdate.Count} existing users");
await ApiHelper.UpdateUsers(apiKey, usersToUpdate);
Console.WriteLine($"Adding {usersToAdd.Count} new users");
await ApiHelper.AddUsers(apiKey, usersToAdd);
}