Skip to content

Commit

Permalink
fix(registration): delete idp on application decline
Browse files Browse the repository at this point in the history
Refs: TEST-1642
  • Loading branch information
Phil91 committed Dec 4, 2023
1 parent 64b36a0 commit b113d4f
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Extensions;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Repositories;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Entities;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;
using Org.Eclipse.TractusX.Portal.Backend.Processes.ApplicationChecklist.Library;
using Org.Eclipse.TractusX.Portal.Backend.Provisioning.Library;
using Org.Eclipse.TractusX.Portal.Backend.SdFactory.Library.BusinessLogic;
using Org.Eclipse.TractusX.Portal.Backend.SdFactory.Library.Models;
using System.Text.RegularExpressions;
Expand All @@ -49,6 +51,7 @@ public sealed class RegistrationBusinessLogic : IRegistrationBusinessLogic
private readonly IApplicationChecklistService _checklistService;
private readonly IClearinghouseBusinessLogic _clearinghouseBusinessLogic;
private readonly ISdFactoryBusinessLogic _sdFactoryBusinessLogic;
private readonly IProvisioningManager _provisioningManager;
private readonly ILogger<RegistrationBusinessLogic> _logger;

public RegistrationBusinessLogic(
Expand All @@ -58,6 +61,7 @@ public RegistrationBusinessLogic(
IApplicationChecklistService checklistService,
IClearinghouseBusinessLogic clearinghouseBusinessLogic,
ISdFactoryBusinessLogic sdFactoryBusinessLogic,
IProvisioningManager provisioningManager,
ILogger<RegistrationBusinessLogic> logger)
{
_portalRepositories = portalRepositories;
Expand All @@ -66,6 +70,7 @@ public RegistrationBusinessLogic(
_checklistService = checklistService;
_clearinghouseBusinessLogic = clearinghouseBusinessLogic;
_sdFactoryBusinessLogic = sdFactoryBusinessLogic;
_provisioningManager = provisioningManager;
_logger = logger;
}

Expand Down Expand Up @@ -419,7 +424,11 @@ public async Task DeclineRegistrationVerification(Guid applicationId, string com
throw new ArgumentException($"CompanyApplication {applicationId} is not in status SUBMITTED", nameof(applicationId));
}

var (companyId, companyName, processId) = result;
var (companyId, companyName, processId, idps, identityData) = result;
if (idps.Count() != 1)
{
throw new UnexpectedConditionException($"There should only be one idp for application {applicationId}");
}

var context = await _checklistService
.VerifyChecklistEntryAndProcessSteps(
Expand All @@ -443,6 +452,13 @@ public async Task DeclineRegistrationVerification(Guid applicationId, string com
},
null);

var (idpAlias, idpType) = idps.Single();
if (idpType == IdentityProviderTypeId.SHARED)
{
await _provisioningManager.DeleteSharedIdpRealmAsync(idpAlias).ConfigureAwait(false);
}
await _provisioningManager.DeleteCentralIdentityProviderAsync(idpAlias).ConfigureAwait(false);

_portalRepositories.GetInstance<IApplicationRepository>().AttachAndModifyCompanyApplication(applicationId, application =>
{
application.ApplicationStatusId = CompanyApplicationStatusId.DECLINED;
Expand All @@ -453,6 +469,12 @@ public async Task DeclineRegistrationVerification(Guid applicationId, string com
company.CompanyStatusId = CompanyStatusId.REJECTED;
});

foreach (var userEntityId in identityData.Where(x => x.UserEntityId != null).Select(x => x.UserEntityId))
{
await _provisioningManager.DeleteCentralRealmUserAsync(userEntityId).ConfigureAwait(false);
}
_portalRepositories.GetInstance<IUserRepository>().AttachAndModifyIdentities(identityData.Select(x => new ValueTuple<Guid, Action<Identity>>(x.IdentityId, identity => { identity.UserStatusId = UserStatusId.DELETED; })));

if (processId != null)
{
_portalRepositories.GetInstance<IProcessStepRepository>().CreateProcessStepRange(Enumerable.Repeat(new ValueTuple<ProcessStepTypeId, ProcessStepStatusId, Guid>(ProcessStepTypeId.TRIGGER_CALLBACK_OSP_DECLINED, ProcessStepStatusId.TODO, processId.Value), 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ public async Task<NoContentResult> ApproveApplication([FromRoute] Guid applicati
/// <response code="400">Either the CompanyApplication is not in status SUBMITTED, or there is no checklist entry of type Registration_Verification.</response>
/// <response code="404">Application ID not found.</response>
[HttpPost]
[Authorize(Roles = "decline_new_partner")]
[Authorize(Policy = PolicyTypes.CompanyUser)]
// [Authorize(Roles = "decline_new_partner")]
// [Authorize(Policy = PolicyTypes.CompanyUser)]
[Route("applications/{applicationId}/decline")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status400BadRequest)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,13 +426,15 @@ public IAsyncEnumerable<Guid> GetSubmittedApplicationIdsByBpn(string bpn) =>
/// </summary>
/// <param name="applicationId">Id of the application</param>
/// <returns>Returns the company id</returns>
public Task<(Guid CompanyId, string CompanyName, Guid? NetworkRegistrationProcessId)> GetCompanyIdNameForSubmittedApplication(Guid applicationId) =>
public Task<(Guid CompanyId, string CompanyName, Guid? NetworkRegistrationProcessId, IEnumerable<(string IamAlias, IdentityProviderTypeId TypeId)> Idps, IEnumerable<(Guid IdentityId, string? UserEntityId)> IdentityIds)> GetCompanyIdNameForSubmittedApplication(Guid applicationId) =>
_dbContext.CompanyApplications
.Where(x => x.Id == applicationId && x.ApplicationStatusId == CompanyApplicationStatusId.SUBMITTED)
.Select(x => new ValueTuple<Guid, string, Guid?>(
.Select(x => new ValueTuple<Guid, string, Guid?, IEnumerable<(string, IdentityProviderTypeId)>, IEnumerable<(Guid, string?)>>(
x.CompanyId,
x.Company!.Name,
x.Company!.NetworkRegistration!.ProcessId))
x.Company!.NetworkRegistration!.ProcessId,
x.Company.IdentityProviders.Where(idp => idp.IdentityProviderTypeId != IdentityProviderTypeId.MANAGED).Select(idp => new ValueTuple<string, IdentityProviderTypeId>(idp.IamIdentityProvider!.IamIdpAlias, idp.IdentityProviderTypeId)),
x.Company.Identities.Where(i => i.UserStatusId != UserStatusId.DELETED).Select(i => new ValueTuple<Guid, string?>(i.Id, i.UserEntityId))))
.SingleOrDefaultAsync();

public Task<bool> IsValidApplicationForCompany(Guid applicationId, Guid companyId) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public interface IApplicationRepository
/// </summary>
/// <param name="applicationId">Id of the application</param>
/// <returns>The id of the company for the given application</returns>
Task<(Guid CompanyId, string CompanyName, Guid? NetworkRegistrationProcessId)> GetCompanyIdNameForSubmittedApplication(Guid applicationId);
Task<(Guid CompanyId, string CompanyName, Guid? NetworkRegistrationProcessId, IEnumerable<(string IamAlias, IdentityProviderTypeId TypeId)> Idps, IEnumerable<(Guid IdentityId, string? UserEntityId)> IdentityIds)> GetCompanyIdNameForSubmittedApplication(Guid applicationId);

Task<bool> IsValidApplicationForCompany(Guid applicationId, Guid companyId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,5 @@ public interface IUserRepository
CompanyUserAssignedIdentityProvider AddCompanyUserAssignedIdentityProvider(Guid companyUserId, Guid identityProviderId, string providerId, string userName);
IAsyncEnumerable<CompanyUserIdentityProviderProcessData> GetUserAssignedIdentityProviderForNetworkRegistration(Guid networkRegistrationId);
IAsyncEnumerable<(Guid ServiceAccountId, string ClientClientId)> GetNextServiceAccountsWithoutUserEntityId();
void AttachAndModifyIdentities(IEnumerable<(Guid IdentityId, Action<Identity> Modify)> identityData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,16 @@ public IAsyncEnumerable<CompanyUserIdentityProviderProcessData> GetUserAssignedI
.Select(x => new ValueTuple<Guid, string>(x.Id, x.CompanyServiceAccount!.ClientClientId!))
.Take(2)
.ToAsyncEnumerable();

public void AttachAndModifyIdentities(IEnumerable<(Guid IdentityId, Action<Identity> Modify)> identityData)
{
var initial = identityData.Select(x =>
{
var identity = new Identity(x.IdentityId, default, Guid.Empty, default, default);
return (Identity: identity, x.Modify);
}
).ToList();
_dbContext.AttachRange(initial.Select(x => x.Identity));
initial.ForEach(x => x.Modify(x.Identity));
}
}
Loading

0 comments on commit b113d4f

Please sign in to comment.