Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Commands/AzureAD/AddAzureADGroupMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,29 @@ protected override void ExecuteCmdlet()
{
group = Identity.GetGroup(GraphRequestHelper);
}

if (group != null)
{

if (RemoveExisting.ToBool())
{
Microsoft365GroupsUtility.ClearMembers(GraphRequestHelper, new System.Guid(group.Id));
}

Guid emptyGuid = Guid.Empty;

var userArray = Users.Where(x => !Guid.TryParse(x, out emptyGuid)).ToArray();

if (userArray.Length > 0)
{
Microsoft365GroupsUtility.AddMembers(GraphRequestHelper, new Guid(group.Id), userArray, RemoveExisting.ToBool());
Microsoft365GroupsUtility.AddMembers(GraphRequestHelper, new Guid(group.Id), userArray);
}

var secGroups = Users.Where(x => Guid.TryParse(x, out emptyGuid)).Select(x => emptyGuid).ToArray();

if (secGroups.Length > 0)
{
Microsoft365GroupsUtility.AddDirectoryMembers(GraphRequestHelper, new Guid(group.Id), secGroups, RemoveExisting.ToBool());
Microsoft365GroupsUtility.AddDirectoryMembers(GraphRequestHelper, new Guid(group.Id), secGroups);
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/Commands/AzureAD/AddAzureADGroupOwner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,26 @@ protected override void ExecuteCmdlet()

if (group != null)
{


if (RemoveExisting.ToBool())
{
Microsoft365GroupsUtility.ClearOwnersAsync(GraphRequestHelper, new System.Guid(group.Id));
}

Guid emptyGuid = Guid.Empty;

var userArray = Users.Where(x => !Guid.TryParse(x, out emptyGuid)).ToArray();

if (userArray.Length > 0)
{
Microsoft365GroupsUtility.AddOwners(GraphRequestHelper, new System.Guid(group.Id), userArray, RemoveExisting.ToBool());
Microsoft365GroupsUtility.AddOwners(GraphRequestHelper, new System.Guid(group.Id), userArray);
}

var secGroups = Users.Where(x => Guid.TryParse(x, out emptyGuid)).Select(x => emptyGuid).ToArray();

if (secGroups.Length > 0)
{
Microsoft365GroupsUtility.AddDirectoryOwners(GraphRequestHelper, new System.Guid(group.Id), secGroups, RemoveExisting.ToBool());
Microsoft365GroupsUtility.AddDirectoryOwners(GraphRequestHelper, new System.Guid(group.Id), secGroups);
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/Commands/Microsoft365Groups/AddMicrosoft365GroupMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ public class AddMicrosoft365GroupMember : PnPGraphCmdlet

protected override void ExecuteCmdlet()
{
Microsoft365GroupsUtility.AddMembers(GraphRequestHelper, Identity.GetGroupId(GraphRequestHelper), Users, RemoveExisting);
System.Guid groupId = Identity.GetGroupId(GraphRequestHelper);

if (RemoveExisting.ToBool())
{
Microsoft365GroupsUtility.UpdateMembersAsync(GraphRequestHelper, groupId, Users);
}
else
{
Microsoft365GroupsUtility.AddMembers(GraphRequestHelper, groupId, Users);
}
}
}
}
11 changes: 10 additions & 1 deletion src/Commands/Microsoft365Groups/AddMicrosoft365GroupOwner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ public class AddMicrosoft365GroupOwner : PnPGraphCmdlet

protected override void ExecuteCmdlet()
{
Microsoft365GroupsUtility.AddOwners(GraphRequestHelper, Identity.GetGroupId(GraphRequestHelper), Users, RemoveExisting);
System.Guid groupId = Identity.GetGroupId(GraphRequestHelper);

if (RemoveExisting.ToBool())
{
Microsoft365GroupsUtility.UpdateOwners(GraphRequestHelper, groupId, Users);
}
else
{
Microsoft365GroupsUtility.AddOwners(GraphRequestHelper, groupId, Users);
}
}
}
}
24 changes: 12 additions & 12 deletions src/Commands/Utilities/Microsoft365GroupsUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,24 +289,24 @@ internal static void PermanentlyDeleteDeletedGroup(ApiRequestHelper requestHelpe
requestHelper.Delete($"v1.0/directory/deleteditems/microsoft.graph.group/{groupId}");
}

internal static void AddOwners(ApiRequestHelper requestHelper, Guid groupId, string[] users, bool removeExisting)
internal static void AddOwners(ApiRequestHelper requestHelper, Guid groupId, string[] users)
{
AddUsersToGroup(requestHelper, "owners", groupId, users, removeExisting);
AddUsersToGroup(requestHelper, "owners", groupId, users);
}

internal static void AddDirectoryOwners(ApiRequestHelper requestHelper, Guid groupId, Guid[] users, bool removeExisting)
internal static void AddDirectoryOwners(ApiRequestHelper requestHelper, Guid groupId, Guid[] users)
{
AddDirectoryObjectsToGroup(requestHelper, "owners", groupId, users, removeExisting);
AddDirectoryObjectsToGroup(requestHelper, "owners", groupId, users);
}

internal static void AddMembers(ApiRequestHelper requestHelper, Guid groupId, string[] users, bool removeExisting)
internal static void AddMembers(ApiRequestHelper requestHelper, Guid groupId, string[] users)
{
AddUsersToGroup(requestHelper, "members", groupId, users, removeExisting);
AddUsersToGroup(requestHelper, "members", groupId, users);
}

internal static void AddDirectoryMembers(ApiRequestHelper requestHelper, Guid groupId, Guid[] users, bool removeExisting)
internal static void AddDirectoryMembers(ApiRequestHelper requestHelper, Guid groupId, Guid[] users)
{
AddDirectoryObjectsToGroup(requestHelper, "members", groupId, users, removeExisting);
AddDirectoryObjectsToGroup(requestHelper, "members", groupId, users);
}

internal static string GetUserGraphUrlForUPN(string upn)
Expand All @@ -318,7 +318,7 @@ internal static string GetUserGraphUrlForUPN(string upn)
return $"users/{escapedUpn}";
}

private static void AddUsersToGroup(ApiRequestHelper requestHelper, string groupName, Guid groupId, string[] users, bool removeExisting)
private static void AddUsersToGroup(ApiRequestHelper requestHelper, string groupName, Guid groupId, string[] users)
{
foreach (var user in users)
{
Expand All @@ -338,7 +338,7 @@ private static void AddUsersToGroup(ApiRequestHelper requestHelper, string group
}
}

private static void AddDirectoryObjectsToGroup(ApiRequestHelper requestHelper, string groupName, Guid groupId, Guid[] directoryObjects, bool removeExisting)
private static void AddDirectoryObjectsToGroup(ApiRequestHelper requestHelper, string groupName, Guid groupId, Guid[] directoryObjects)
{
foreach (var dirObject in directoryObjects)
{
Expand Down Expand Up @@ -440,7 +440,7 @@ internal static void UpdateOwners(ApiRequestHelper requestHelper, Guid groupId,
{
if (existingOwners.FirstOrDefault(o => o.UserPrincipalName == owner) == null)
{
AddOwners(requestHelper, groupId, new string[] { owner }, false);
AddOwners(requestHelper, groupId, new string[] { owner });
}
}
foreach (var existingOwner in existingOwners)
Expand All @@ -459,7 +459,7 @@ internal static void UpdateMembersAsync(ApiRequestHelper requestHelper, Guid gro
{
if (existingMembers.FirstOrDefault(o => o.UserPrincipalName == member) == null)
{
AddMembers(requestHelper, groupId, new string[] { member }, false);
AddMembers(requestHelper, groupId, new string[] { member });
}
}
foreach (var existingMember in existingMembers)
Expand Down