Skip to content

Commit

Permalink
Psp 3051 (#1088)
Browse files Browse the repository at this point in the history
* remove organization logic from keycloak service as it is no longer used.

* Bump patch number.

Co-authored-by: Smith <[email protected]>
  • Loading branch information
devinleighsmith and Smith authored Mar 10, 2022
1 parent 8bd6df6 commit 4ef79ae
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 69 deletions.
4 changes: 2 additions & 2 deletions backend/api/Pims.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<UserSecretsId>0ef6255f-9ea0-49ec-8c65-c172304b4926</UserSecretsId>
<Version>1.1.0-21.45</Version>
<AssemblyVersion>1.1.0.21</AssemblyVersion>
<Version>1.1.1-21.45</Version>
<AssemblyVersion>1.1.1.21</AssemblyVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<ProjectGuid>16BC0468-78F6-4C91-87DA-7403C919E646</ProjectGuid>
</PropertyGroup>
Expand Down
66 changes: 2 additions & 64 deletions backend/dal.keycloak/Partials/PimsKeycloakUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,27 +124,6 @@ public partial class PimsKeycloakService : IPimsKeycloakService
addRoleIds = user.PimsUserRoles.Except(euser.PimsUserRoles, new UserRoleRoleIdComparer()).Select(r => r.RoleId).ToArray();
removeRoleIds = euser.PimsUserRoles.Except(user.PimsUserRoles, new UserRoleRoleIdComparer()).Select(r => r.RoleId).ToArray();

IEnumerable<long> addOrganizationIds;
IEnumerable<long> removeOrganizationIds;
addOrganizationIds = user.PimsUserOrganizations.Except(euser.PimsUserOrganizations, new UserOrganizationOrganizationIdComparer()).Select(r => r.OrganizationId).ToArray();
removeOrganizationIds = euser.PimsUserOrganizations.Except(user.PimsUserOrganizations, new UserOrganizationOrganizationIdComparer()).Select(r => r.OrganizationId).ToArray();
// Make sure child organizations are included.
if (!addOrganizationIds.Any())
{
user.PimsUserOrganizations.ForEach(a =>
{
addOrganizationIds = addOrganizationIds.Concat(_pimsRepository.UserOrganization.GetChildren(a.OrganizationId).Select(a => a.Id).ToArray()).ToArray();
});
}
// Each parent organization should add children organizations.
addOrganizationIds.ForEach(id =>
{
var childOrganizations = _pimsRepository.UserOrganization.GetChildren(id).Select(a => a.Id).ToArray();
addOrganizationIds = addOrganizationIds.Concat(childOrganizations).Distinct().ToArray();
});
// Don't incorrectly remove child organizations.
removeOrganizationIds = removeOrganizationIds.Except(addOrganizationIds).ToArray();

// Update Roles.
removeRoleIds.ForEach(r =>
{
Expand All @@ -159,26 +138,11 @@ public partial class PimsKeycloakService : IPimsKeycloakService
euser.PimsUserRoles.Add(new Entity.PimsUserRole(euser, role));
});

// Update Organizations
addOrganizationIds.ForEach(oId =>
{
var organization = _pimsRepository.UserOrganization.Find(oId) ?? throw new KeyNotFoundException("Cannot assign an organization to a user, when the organization does not exist.");
var roleId = user.PimsUserOrganizations.FirstOrDefault(o => o.OrganizationId == oId).RoleId;
var role = _pimsRepository.Role.Find(roleId);
euser.PimsUserOrganizations.Add(new Entity.PimsUserOrganization() { User = euser, Organization = organization, Role = role });
});
removeOrganizationIds.ForEach(oId =>
{
var organization = _pimsRepository.UserOrganization.Find(oId) ?? throw new KeyNotFoundException("Cannot remove an organization from a user, when the organization does not exist.");
var userOrganization = euser.PimsUserOrganizations.FirstOrDefault(r => r.OrganizationId == organization.Id);
euser.PimsUserOrganizations.Remove(userOrganization);
});

return await SaveUserChanges(user, euser, kuser, true);
}

/// <summary>
/// Update the specified user in keycloak and PIMS, only add new organizations and roles.
/// Update the specified user in keycloak and PIMS, only add and roles.
/// </summary>
/// <param name="user"></param>
/// <exception type="KeyNotFoundException">User does not exist in keycloak or PIMS.</exception>
Expand All @@ -189,14 +153,6 @@ public partial class PimsKeycloakService : IPimsKeycloakService
var euser = _pimsRepository.User.GetTracking(update.Id);

IEnumerable<long> addRoleIds = update.PimsUserRoles.Except(euser.PimsUserRoles, new UserRoleRoleIdComparer()).Select(r => r.RoleId).ToArray();
IEnumerable<long> addOrganizationIds = update.PimsUserOrganizations.Except(euser.PimsUserOrganizations, new OrganizationOrganizationIdComparer()).Select(a => a.OrganizationId).ToArray();
addOrganizationIds = update.PimsUserOrganizations.Except(euser.PimsUserOrganizations, new UserOrganizationOrganizationIdComparer()).Select(r => r.OrganizationId).ToArray();
// Each parent organization should add children organizations.
addOrganizationIds.ForEach(id =>
{
var childOrganizations = _pimsRepository.UserOrganization.GetChildren(id).Select(a => a.Id).ToArray();
addOrganizationIds = addOrganizationIds.Concat(childOrganizations).Distinct().ToArray();
});

var roleIds = update.PimsUserRoles.Select(r => r.RoleId);
foreach (var roleId in roleIds)
Expand All @@ -214,15 +170,6 @@ public partial class PimsKeycloakService : IPimsKeycloakService
euser.PimsUserRoles.Add(new Entity.PimsUserRole(euser, role));
});

// Update Organizations
addOrganizationIds.ForEach(oId =>
{
var organization = _pimsRepository.UserOrganization.Find(oId) ?? throw new KeyNotFoundException("Cannot assign an organization to a user, when the organization does not exist.");
var roleId = update.PimsUserOrganizations.FirstOrDefault(o => o.OrganizationId == oId).RoleId;
var role = _pimsRepository.Role.Find(roleId);
euser.PimsUserOrganizations.Add(new Entity.PimsUserOrganization() { UserId = update.UserId, Organization = organization, Role = role });
});

return await SaveUserChanges(update, euser, kuser);
}

Expand Down Expand Up @@ -253,7 +200,6 @@ public partial class PimsKeycloakService : IPimsKeycloakService
euser.Person.PimsContactMethods.Add(new PimsContactMethod(c.Person, c.Organization, c.ContactMethodTypeCode, c.ContactMethodValue));
});


euser = _pimsRepository.User.UpdateOnly(euser);

// Now update keycloak
Expand All @@ -280,10 +226,9 @@ public partial class PimsKeycloakService : IPimsKeycloakService

kmodel.Attributes = new Dictionary<string, string[]>
{
["organizations"] = _pimsRepository.User.GetOrganizations(euser.GuidIdentifierValue.Value).Select(a => a.ToString()).ToArray(),
["displayName"] = new[] { update.BusinessIdentifierValue }
};
_logger.LogInformation($"Updating keycloak organization attribute '{kmodel.Attributes["organizations"]}' for user '{euser.BusinessIdentifierValue}'.");
_logger.LogInformation($"Updating keycloak user '{euser.BusinessIdentifierValue}'.");
await _keycloakService.UpdateUserAsync(kmodel);

return _pimsRepository.User.Get(euser.Id);
Expand All @@ -307,13 +252,6 @@ public partial class PimsKeycloakService : IPimsKeycloakService

user.PimsUserRoles.Clear();
user.PimsUserRoles.Add(new Entity.PimsUserRole() { UserId = user.Id, RoleId = update.RoleId.Value});
update.PimsAccessRequestOrganizations.ToArray().ForEach(aro =>
{
if (!user.PimsUserOrganizations.Any(a => a.OrganizationId == aro.OrganizationId))
{
user.PimsUserOrganizations.Add(new Entity.PimsUserOrganization() { UserId = update.UserId, OrganizationId = aro.OrganizationId.Value, RoleId = update.RoleId.Value });
}
});
await AppendToUserAsync(user);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public async Task UpdateUserAsync_Success()
addUserRole.Role.KeycloakGroupId = Guid.NewGuid();
pimsServiceMock.Setup(m => m.Role.Find(addUserRole.Role.RoleId)).Returns(addUserRole.Role);
pimsServiceMock.Setup(m => m.UserOrganization.GetChildren(It.IsAny<long>())).Returns(Array.Empty<Entity.PimsOrganization>());
pimsServiceMock.Setup(m => m.User.GetOrganizations(It.IsAny<Guid>())).Returns(euser.GetOrganizations().Select(a => a.Id));

// Act
var result = await service.UpdateUserAsync(user);
Expand All @@ -86,7 +85,6 @@ public async Task UpdateUserAsync_Success()
keycloakServiceMock.Verify(m => m.RemoveGroupFromUserAsync(euser.GuidIdentifierValue.Value, new Guid(kuser.Groups.First())), Times.Once);
keycloakServiceMock.Verify(m => m.AddGroupToUserAsync(euser.GuidIdentifierValue.Value, addUserRole.Role.KeycloakGroupId.Value), Times.Once);
pimsServiceMock.Verify(m => m.User.UpdateOnly(It.IsAny<Entity.PimsUser>()), Times.Once);
pimsServiceMock.Verify(m => m.User.GetOrganizations(It.IsAny<Guid>()), Times.Once);
pimsServiceMock.Verify(m => m.User.RemoveRole(It.IsAny<Entity.PimsUser>(), removeRole.RoleId), Times.Once);
keycloakServiceMock.Verify(m => m.UpdateUserAsync(It.IsAny<Pims.Keycloak.Models.UserModel>()), Times.Once);
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "frontend",
"version": "1.1.0-21.45",
"version": "1.1.1-21.45",
"private": true,
"dependencies": {
"@bcgov/bc-sans": "1.0.1",
Expand Down

0 comments on commit 4ef79ae

Please sign in to comment.