1
0
mirror of https://github.com/bitwarden/directory-connector synced 2025-12-14 07:13:25 +00:00

simulate sync and print results

This commit is contained in:
Kyle Spearrin
2017-05-13 23:08:15 -04:00
parent eaaf9c9da9
commit 4f03e70a8a
3 changed files with 110 additions and 36 deletions

View File

@@ -43,9 +43,10 @@ namespace Bit.Console
Con.WriteLine("2. Log out");
Con.WriteLine("3. Configure directory connection");
Con.WriteLine("4. Configure sync");
Con.WriteLine("5. Sync directory");
Con.WriteLine("6. Start/stop background service");
Con.WriteLine("7. Exit");
Con.WriteLine("5. Simulate directory sync");
Con.WriteLine("6. Sync directory");
Con.WriteLine("7. Start/stop background service");
Con.WriteLine("8. Exit");
Con.WriteLine();
Con.Write("What would you like to do? ");
selection = Con.ReadLine();
@@ -75,6 +76,12 @@ namespace Bit.Console
await ConfigSyncAsync();
break;
case "5":
case "print":
case "sim":
case "simulate":
await PrintAsync();
break;
case "6":
case "sync":
await SyncAsync();
break;
@@ -543,7 +550,7 @@ namespace Bit.Console
if(result.Success)
{
Con.ForegroundColor = ConsoleColor.Green;
Con.WriteLine("Syncing complete ({0} users, {1} groups).", result.UserCount, result.GroupCount);
Con.WriteLine("Syncing complete ({0} users, {1} groups).", result.Users.Count, result.Groups.Count);
Con.ResetColor();
}
else
@@ -556,6 +563,58 @@ namespace Bit.Console
}
}
private static async Task PrintAsync()
{
if(!Core.Services.AuthService.Instance.Authenticated)
{
Con.WriteLine("You are not logged in.");
}
else if(Core.Services.SettingsService.Instance.Server == null)
{
Con.WriteLine("Server is not configured.");
}
else
{
var force = false;
if(_usingArgs)
{
var parameters = ParseParameters();
force = parameters.ContainsKey("f");
}
Con.WriteLine("Querying...");
Con.WriteLine();
var result = await Sync.GatherAsync(force);
if(result.Success)
{
Con.WriteLine("Groups:");
foreach(var group in result.Groups)
{
Con.WriteLine(" {0} - {1}", group.Name, group.DistinguishedName);
}
Con.WriteLine();
Con.WriteLine("Users:");
foreach(var user in result.Users)
{
Con.WriteLine(" {0}", user.Email);
foreach(var group in user.Groups)
{
Con.WriteLine(" {0}", group);
}
}
}
else
{
Con.ForegroundColor = ConsoleColor.Red;
Con.WriteLine("Querying failed.");
Con.WriteLine(result.ErrorMessage);
Con.ResetColor();
}
}
}
private static string ReadSecureLine()
{
var input = string.Empty;

View File

@@ -10,7 +10,7 @@ namespace Bit.Core.Models
{
public bool Success { get; set; }
public string ErrorMessage { get; set; }
public int GroupCount { get; set; }
public int UserCount { get; set; }
public List<GroupEntry> Groups { get; set; }
public List<UserEntry> Users { get; set; }
}
}

View File

@@ -12,6 +12,46 @@ namespace Bit.Core.Utilities
public static class Sync
{
public static async Task<SyncResult> SyncAllAsync(bool force = false)
{
var now = DateTime.UtcNow;
var gatherResult = await GatherAsync(force);
if(!gatherResult.Success)
{
return gatherResult;
}
var request = new ImportRequest(gatherResult.Groups, gatherResult.Users);
var response = await ApiService.Instance.PostImportAsync(request);
if(response.Succeeded)
{
if(SettingsService.Instance.Sync.SyncGroups)
{
SettingsService.Instance.LastGroupSyncDate = now;
}
if(SettingsService.Instance.Sync.SyncUsers)
{
SettingsService.Instance.LastUserSyncDate = now;
}
return new SyncResult
{
Success = true,
Groups = gatherResult.Groups,
Users = gatherResult.Users
};
}
else
{
return new SyncResult
{
Success = false,
ErrorMessage = response.Errors.FirstOrDefault()?.Message
};
}
}
public static async Task<SyncResult> GatherAsync(bool force = false)
{
if(!AuthService.Instance.Authenticated || !AuthService.Instance.OrganizationSet)
{
@@ -40,8 +80,6 @@ namespace Bit.Core.Utilities
};
}
var now = DateTime.UtcNow;
List<GroupEntry> groups = null;
if(SettingsService.Instance.Sync.SyncGroups)
{
@@ -56,36 +94,13 @@ namespace Bit.Core.Utilities
FlattenGroupsToUsers(groups, null, groups, users);
var request = new ImportRequest(groups, users);
var response = await ApiService.Instance.PostImportAsync(request);
if(response.Succeeded)
{
if(SettingsService.Instance.Sync.SyncGroups)
{
SettingsService.Instance.LastGroupSyncDate = now;
}
if(SettingsService.Instance.Sync.SyncUsers)
{
SettingsService.Instance.LastUserSyncDate = now;
}
return new SyncResult
{
Success = true,
GroupCount = groups.Count,
UserCount = users.Count
Groups = groups,
Users = users
};
}
else
{
return new SyncResult
{
Success = false,
ErrorMessage = response.Errors.FirstOrDefault()?.Message
};
}
}
private static Task<List<GroupEntry>> GetGroupsAsync(bool force = false)
{