Skip to content

Instantly share code, notes, and snippets.

@Sam7
Created March 9, 2018 04:47
Show Gist options
  • Save Sam7/ee57bce4435b2839191d498b684dc07a to your computer and use it in GitHub Desktop.
Save Sam7/ee57bce4435b2839191d498b684dc07a to your computer and use it in GitHub Desktop.
private static void RemoveGroups(BackOfficeIdentityUser autoLinkUser, IReadOnlyUserGroup[] groupsToRemove)
{
// remove old groups
// for some reason it only works if we adjust the groups first and then the roles.
// only works when both are changed and only in that order :S
var groups = autoLinkUser.Groups.ToList();
foreach (var adGroup in groupsToRemove) groups.RemoveAll(x => x.Alias.Equals(adGroup.Alias));
autoLinkUser.Groups = groups.ToArray();
// the same for roles
foreach (var adGroup in groupsToRemove)
{
var userRole = autoLinkUser.Roles.FirstOrDefault(x => x.RoleId.Equals(adGroup.Alias));
if (userRole == null) continue;
autoLinkUser.Roles.Remove(userRole);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment