mirror of
https://github.com/bitwarden/directory-connector
synced 2025-12-10 21:33:20 +00:00
refactor group assignment. sync disabled users
This commit is contained in:
@@ -665,17 +665,17 @@ namespace Bit.Console
|
|||||||
foreach(var group in result.Groups)
|
foreach(var group in result.Groups)
|
||||||
{
|
{
|
||||||
Con.WriteLine(" {0} - {1}", group.Name, group.Id);
|
Con.WriteLine(" {0} - {1}", group.Name, group.Id);
|
||||||
|
foreach(var user in group.Users)
|
||||||
|
{
|
||||||
|
Con.WriteLine(" {0}", user);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Con.WriteLine();
|
Con.WriteLine();
|
||||||
Con.WriteLine("Users:");
|
Con.WriteLine("Users:");
|
||||||
foreach(var user in result.Users)
|
foreach(var user in result.Users)
|
||||||
{
|
{
|
||||||
Con.WriteLine(" {0}", user.Email);
|
Con.WriteLine(" {0}{1}", user.Email, user.Disabled ? " (disabled)" : null);
|
||||||
foreach(var group in user.Groups)
|
|
||||||
{
|
|
||||||
Con.WriteLine(" {0}", group);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -64,6 +64,7 @@
|
|||||||
<Compile Include="Enums\DirectoryType.cs" />
|
<Compile Include="Enums\DirectoryType.cs" />
|
||||||
<Compile Include="Enums\OrganizationUserType.cs" />
|
<Compile Include="Enums\OrganizationUserType.cs" />
|
||||||
<Compile Include="Enums\OrganizationUserStatusType.cs" />
|
<Compile Include="Enums\OrganizationUserStatusType.cs" />
|
||||||
|
<Compile Include="Enums\UserAccountControl.cs" />
|
||||||
<Compile Include="Models\ApiError.cs" />
|
<Compile Include="Models\ApiError.cs" />
|
||||||
<Compile Include="Models\ApiResult.cs" />
|
<Compile Include="Models\ApiResult.cs" />
|
||||||
<Compile Include="Models\Entry.cs" />
|
<Compile Include="Models\Entry.cs" />
|
||||||
|
|||||||
15
src/Core/Enums/UserAccountControl.cs
Normal file
15
src/Core/Enums/UserAccountControl.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Bit.Core.Enums
|
||||||
|
{
|
||||||
|
[Flags]
|
||||||
|
public enum UserAccountControl : int
|
||||||
|
{
|
||||||
|
AccountDisabled = 0x00000002,
|
||||||
|
LockOut = 0x00000010,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,11 +17,12 @@ namespace Bit.Core.Models
|
|||||||
{
|
{
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public HashSet<string> Members { get; set; } = new HashSet<string>();
|
public HashSet<string> Members { get; set; } = new HashSet<string>();
|
||||||
|
public HashSet<string> Users { get; set; } = new HashSet<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public class UserEntry : Entry
|
public class UserEntry : Entry
|
||||||
{
|
{
|
||||||
public string Email { get; set; }
|
public string Email { get; set; }
|
||||||
public HashSet<string> Groups { get; set; } = new HashSet<string>();
|
public bool Disabled { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,10 +20,12 @@ namespace Bit.Core.Models
|
|||||||
{
|
{
|
||||||
Name = entry.Name;
|
Name = entry.Name;
|
||||||
ExternalId = entry.Id;
|
ExternalId = entry.Id;
|
||||||
|
Users = entry.Users;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string ExternalId { get; set; }
|
public string ExternalId { get; set; }
|
||||||
|
public IEnumerable<string> Users { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class User
|
public class User
|
||||||
@@ -31,11 +33,11 @@ namespace Bit.Core.Models
|
|||||||
public User(UserEntry entry)
|
public User(UserEntry entry)
|
||||||
{
|
{
|
||||||
Email = entry.Email;
|
Email = entry.Email;
|
||||||
ExternalGroupIds = entry.Groups;
|
Disabled = entry.Disabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Email { get; set; }
|
public string Email { get; set; }
|
||||||
public IEnumerable<string> ExternalGroupIds { get; set; }
|
public bool Disabled { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ namespace Bit.Core.Models
|
|||||||
{
|
{
|
||||||
public bool Success { get; set; }
|
public bool Success { get; set; }
|
||||||
public string ErrorMessage { get; set; }
|
public string ErrorMessage { get; set; }
|
||||||
public List<GroupEntry> Groups { get; set; }
|
public List<GroupEntry> Groups { get; set; } = new List<GroupEntry>();
|
||||||
public List<UserEntry> Users { get; set; }
|
public List<UserEntry> Users { get; set; } = new List<UserEntry>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -134,13 +134,14 @@ namespace Bit.Core.Services
|
|||||||
|
|
||||||
var entries = new List<UserEntry>();
|
var entries = new List<UserEntry>();
|
||||||
|
|
||||||
var users = await _graphClient.Users.Request().Select("id,mail,userPrincipalName").GetAsync();
|
var users = await _graphClient.Users.Request().Select("id,mail,userPrincipalName,accountEnabled").GetAsync();
|
||||||
foreach(var user in users)
|
foreach(var user in users)
|
||||||
{
|
{
|
||||||
var entry = new UserEntry
|
var entry = new UserEntry
|
||||||
{
|
{
|
||||||
Id = user.Id,
|
Id = user.Id,
|
||||||
Email = user.Mail ?? user.UserPrincipalName
|
Email = user.Mail ?? user.UserPrincipalName,
|
||||||
|
Disabled = !user.AccountEnabled.GetValueOrDefault(true)
|
||||||
};
|
};
|
||||||
|
|
||||||
if(entry.Email.Contains("#"))
|
if(entry.Email.Contains("#"))
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using Bit.Core.Models;
|
using Bit.Core.Enums;
|
||||||
|
using Bit.Core.Models;
|
||||||
using Bit.Core.Utilities;
|
using Bit.Core.Utilities;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -226,10 +227,26 @@ namespace Bit.Core.Services
|
|||||||
user.CreationDate = item.Properties.ParseDateTime(SettingsService.Instance.Sync.CreationDateAttribute);
|
user.CreationDate = item.Properties.ParseDateTime(SettingsService.Instance.Sync.CreationDateAttribute);
|
||||||
user.RevisionDate = item.Properties.ParseDateTime(SettingsService.Instance.Sync.RevisionDateAttribute);
|
user.RevisionDate = item.Properties.ParseDateTime(SettingsService.Instance.Sync.RevisionDateAttribute);
|
||||||
|
|
||||||
users.Add(user);
|
user.Disabled = EntryDisabled(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Task.FromResult(users);
|
return Task.FromResult(users);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool EntryDisabled(SearchResult item)
|
||||||
|
{
|
||||||
|
if(!item.Properties.Contains("userAccountControl"))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
UserAccountControl control;
|
||||||
|
if(!Enum.TryParse(item.Properties["userAccountControl"].ToString(), out control))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (control & UserAccountControl.AccountDisabled) == UserAccountControl.AccountDisabled;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace Bit.Core.Utilities
|
|||||||
var groups = entriesResult.Item1;
|
var groups = entriesResult.Item1;
|
||||||
var users = entriesResult.Item2;
|
var users = entriesResult.Item2;
|
||||||
|
|
||||||
FlattenGroupsToUsers(groups, null, groups, users);
|
FlattenUsersToGroups(groups, null, groups, users);
|
||||||
|
|
||||||
if(!sendToServer)
|
if(!sendToServer)
|
||||||
{
|
{
|
||||||
@@ -81,7 +81,7 @@ namespace Bit.Core.Utilities
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void FlattenGroupsToUsers(List<GroupEntry> currentGroups, List<UserEntry> currentGroupsUsers,
|
private static void FlattenUsersToGroups(List<GroupEntry> currentGroups, List<UserEntry> currentGroupsUsers,
|
||||||
List<GroupEntry> allGroups, List<UserEntry> allUsers)
|
List<GroupEntry> allGroups, List<UserEntry> allUsers)
|
||||||
{
|
{
|
||||||
foreach(var group in currentGroups)
|
foreach(var group in currentGroups)
|
||||||
@@ -91,9 +91,9 @@ namespace Bit.Core.Utilities
|
|||||||
|
|
||||||
foreach(var user in usersInThisGroup)
|
foreach(var user in usersInThisGroup)
|
||||||
{
|
{
|
||||||
if(!user.Groups.Contains(group.Id))
|
if(!group.Users.Contains(user.Email))
|
||||||
{
|
{
|
||||||
user.Groups.Add(group.Id);
|
group.Users.Add(user.Email);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,9 +101,9 @@ namespace Bit.Core.Utilities
|
|||||||
{
|
{
|
||||||
foreach(var user in currentGroupsUsers)
|
foreach(var user in currentGroupsUsers)
|
||||||
{
|
{
|
||||||
if(!user.Groups.Contains(group.Id))
|
if(!group.Users.Contains(user.Email))
|
||||||
{
|
{
|
||||||
user.Groups.Add(group.Id);
|
group.Users.Add(user.Email);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ namespace Bit.Core.Utilities
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Recurse it
|
// Recurse it
|
||||||
FlattenGroupsToUsers(groupsInThisGroup, usersInThisGroup, allGroups, allUsers);
|
FlattenUsersToGroups(groupsInThisGroup, usersInThisGroup, allGroups, allUsers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user