Skip to content

Commit

Permalink
Updated nuget packages & added endpoint for clearing cache via distri…
Browse files Browse the repository at this point in the history
…buted.
  • Loading branch information
HansDahle committed Oct 16, 2023
1 parent 5902c6d commit 8744b58
Show file tree
Hide file tree
Showing 52 changed files with 312 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Fusion.Integration.Authorization" Version="6.10.6" />
<PackageReference Include="Fusion.Integration.Authorization" Version="7.0.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\api\Fusion.Resources.Database\Fusion.Resources.Database.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using Fusion.Resources.Domain;
using Fusion.AspNetCore.FluentAuthorization;
using Fusion.Authorization;
using Fusion.Integration.Profile.Internal;
using Fusion.Resources.Domain;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
Expand All @@ -12,22 +16,69 @@ public class AdminController : ResourceControllerBase
{

private readonly IOrgUnitCache orgUnitCache;
private readonly IMediator mediator;

public AdminController(IOrgUnitCache orgUnitCache)
public AdminController(IOrgUnitCache orgUnitCache, IMediator mediator)
{

this.orgUnitCache = orgUnitCache;
this.mediator = mediator;
}

[HttpGet("admin/cache/org-units")]

public async Task<ActionResult> CleareCache()
{
#region Authorization

var authResult = await Request.RequireAuthorizationAsync(r =>
{
r.AlwaysAccessWhen().FullControl().FullControlInternal();
r.AnyOf(or =>
{
or.FullControl();
or.FullControlInternal();
or.BeTrustedApplication();
});
});

if (authResult.Unauthorized)
return authResult.CreateForbiddenResponse();

#endregion Authorization

await orgUnitCache.ClearOrgUnitCacheAsync();

return Ok();
}


[HttpPost("admin/cache/reset-internal-cache")]
public async Task<ActionResult> ClearInternalCache()
{
#region Authorization

var authResult = await Request.RequireAuthorizationAsync(r =>
{
r.AlwaysAccessWhen().FullControl().FullControlInternal();
r.AnyOf(or =>
{
or.FullControl();
or.FullControlInternal();
or.BeTrustedApplication();
});
});

if (authResult.Unauthorized)
return authResult.CreateForbiddenResponse();

#endregion Authorization

await mediator.Publish(new DistributedEvents.ResetCacheNotification());

return new OkObjectResult(new { message = "Cache reset has been queued for all instances."});
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public async Task<ActionResult<ApiDepartmentResponsible>> AddDelegatedResourceOw
UpdatedByAzureUniqueId = User.GetAzureUniqueId() ?? User.GetApplicationId()
}.WithReason(request.Reason);

await DispatchAsync(command);
await DispatchCommandAsync(command);

}
catch (RoleDelegationExistsError ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public async Task<ActionResult> DeletePersonAbsence([FromRoute] string personId,
#endregion


await DispatchAsync(new DeletePersonAbsence(id, absenceId));
await DispatchCommandAsync(new DeletePersonAbsence(id, absenceId));

return NoContent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public async Task<ActionResult> DeletePersonalNote(string personId, Guid noteId)
if (!notes.Any(n => n.Id == noteId))
return ApiErrors.NotFound("Could not locate note for user");

await DispatchAsync(new Domain.Commands.DeletePersonNote(noteId, user.azureId));
await DispatchCommandAsync(new Domain.Commands.DeletePersonNote(noteId, user.azureId));

return NoContent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public async Task<ActionResult> ResetAllocationState(string fullDepartmentString
return ApiErrors.NotFound("Could not locate allocation on person");


await DispatchAsync(new Domain.Commands.ResetAllocationState(allocation.Project.OrgProjectId, allocation.PositionId, instanceId));
await DispatchCommandAsync(new Domain.Commands.ResetAllocationState(allocation.Project.OrgProjectId, allocation.PositionId, instanceId));

return NoContent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ public async Task<ActionResult<ApiResourceAllocationRequest>> StartProjectReques

await using var eventTransaction = await notificationClient.BeginTransactionAsync();
await using var transaction = await BeginTransactionAsync();
await DispatchAsync(new Logic.Commands.ResourceAllocationRequest.Initialize(requestId));
await DispatchCommandAsync(new Logic.Commands.ResourceAllocationRequest.Initialize(requestId));
await transaction.CommitAsync();
await eventTransaction.CommitAsync();
}
Expand Down Expand Up @@ -833,7 +833,7 @@ public async Task<ActionResult<ApiResourceAllocationRequest>> StartResourceOwner
{
await using var eventTransaction = await notificationClient.BeginTransactionAsync();
await using var transaction = await BeginTransactionAsync();
await DispatchAsync(new Logic.Commands.ResourceAllocationRequest.Initialize(requestId));
await DispatchCommandAsync(new Logic.Commands.ResourceAllocationRequest.Initialize(requestId));
await transaction.CommitAsync();
await eventTransaction.CommitAsync();
}
Expand Down Expand Up @@ -881,7 +881,7 @@ public async Task<ActionResult> DeleteAllocationRequest(Guid requestId)

await using var eventTransaction = await notificationClient.BeginTransactionAsync();
await using var transaction = await BeginTransactionAsync();
await DispatchAsync(new DeleteInternalRequest(requestId));
await DispatchCommandAsync(new DeleteInternalRequest(requestId));

await transaction.CommitAsync();
await eventTransaction.CommitAsync();
Expand Down Expand Up @@ -921,7 +921,7 @@ public async Task<ActionResult<ApiResourceAllocationRequest>> ProvisionProjectAl
await using var eventTransaction = await notificationClient.BeginTransactionAsync();
await using var scope = await BeginTransactionAsync();

await DispatchAsync(new Logic.Commands.ResourceAllocationRequest.Provision(requestId)
await DispatchCommandAsync(new Logic.Commands.ResourceAllocationRequest.Provision(requestId)
{
ForceProvision = force
});
Expand Down Expand Up @@ -956,7 +956,7 @@ public async Task<ActionResult<ApiResourceAllocationRequest>> ApproveProjectAllo

try
{
await DispatchAsync(new Logic.Commands.ResourceAllocationRequest.Approve(requestId));
await DispatchCommandAsync(new Logic.Commands.ResourceAllocationRequest.Approve(requestId));
await scope.CommitAsync();
await eventTransaction.CommitAsync();
}
Expand Down Expand Up @@ -995,7 +995,7 @@ public async Task<ActionResult<ApiResourceAllocationRequest>> ApproveProjectAllo

try
{
await DispatchAsync(new Logic.Commands.ResourceAllocationRequest.Approve(requestId));
await DispatchCommandAsync(new Logic.Commands.ResourceAllocationRequest.Approve(requestId));
await scope.CommitAsync();
await eventTransaction.CommitAsync();
}
Expand Down Expand Up @@ -1281,7 +1281,7 @@ public async Task<ActionResult<ApiRequestComment>> UpdateRequestComment(Guid req

#endregion Authorization

await DispatchAsync(new UpdateComment(commentId, update.Content));
await DispatchCommandAsync(new UpdateComment(commentId, update.Content));

comment = await DispatchAsync(new GetRequestComment(commentId));
return new ApiRequestComment(comment!);
Expand Down Expand Up @@ -1321,7 +1321,7 @@ public async Task<ActionResult> DeleteRequestComment(Guid requestId, Guid commen

#endregion Authorization

await DispatchAsync(new DeleteComment(commentId));
await DispatchCommandAsync(new DeleteComment(commentId));

return NoContent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected Task<IDbContextTransaction> BeginTransactionAsync()
return scope.BeginTransactionAsync();
}

protected Task DispatchAsync(IRequest command)
protected Task DispatchCommandAsync(IRequest command)
{
var mediator = HttpContext.RequestServices.GetRequiredService<IMediator>();
return mediator.Send(command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public async Task<ActionResult> DeleteResponsibilityMatrix(Guid matrixId)
if (responsibilityMatrix == null)
return FusionApiError.NotFound(matrixId, "Could not locate responsibility matrix");

await DispatchAsync(new DeleteResponsibilityMatrix(matrixId));
await DispatchCommandAsync(new DeleteResponsibilityMatrix(matrixId));

return NoContent();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Fusion.Infrastructure.MediatR.Distributed;
using Fusion.Integration.Profile.Internal;
using Fusion.Resources.Domain;
using MediatR;
using System.Threading;
using System.Threading.Tasks;

namespace Fusion.Resources.Api.DistributedEvents
{
/// <summary>
/// Trigger a reset of all internal caches.
/// </summary>
public class ResetCacheNotification : DistributedNotification
{

public class Handler : INotificationHandler<ResetCacheNotification>
{
private readonly IOrgUnitCache orgUnitCache;
private readonly IProfileCache fusionProfileResolverCache;

public Handler(IOrgUnitCache orgUnitCache, IProfileCache fusionProfileResolverCache)
{
this.orgUnitCache = orgUnitCache;
this.fusionProfileResolverCache = fusionProfileResolverCache;
}

public async Task Handle(ResetCacheNotification notification, CancellationToken cancellationToken)
{
await fusionProfileResolverCache.ClearAsync();
await orgUnitCache.ClearOrgUnitCacheAsync();

}
}
}
}
23 changes: 12 additions & 11 deletions src/backend/api/Fusion.Resources.Api/Fusion.Resources.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Bogus" Version="34.0.2" />
<PackageReference Include="Fusion.Integration" Version="6.10.6" />
<PackageReference Include="Fusion.Events.Client" Version="6.1.0" />
<PackageReference Include="Fusion.Events.Server" Version="6.1.0" />
<PackageReference Include="Fusion.Integration.Authorization" Version="6.10.6" />
<PackageReference Include="Fusion.Integration.Org" Version="6.10.6" />
<PackageReference Include="Fusion.Integration.Roles" Version="6.10.6" />
<PackageReference Include="Fusion.Integration.Notification" Version="6.10.7" />
<PackageReference Include="Fusion.ApiClients.Org" Version="6.0.2" />
<PackageReference Include="Fusion.AspNetCore" Version="6.0.8" />
<PackageReference Include="Fusion.Infrastructure.MediatR" Version="7.0.0" />
<PackageReference Include="Fusion.Integration" Version="7.0.4" />
<PackageReference Include="Fusion.Events.Client" Version="7.0.1" />
<PackageReference Include="Fusion.Events.Server" Version="7.0.0" />
<PackageReference Include="Fusion.Integration.Authorization" Version="7.0.4" />
<PackageReference Include="Fusion.Integration.Org" Version="7.0.4" />
<PackageReference Include="Fusion.Integration.Roles" Version="7.0.4" />
<PackageReference Include="Fusion.Integration.Notification" Version="7.0.4" />
<PackageReference Include="Fusion.ApiClients.Org" Version="7.0.0" />
<PackageReference Include="Fusion.AspNetCore" Version="7.0.1" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="JSM.FluentValidation.AspNet.AsyncFilter" Version="2.0.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.13" />
<PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="3.1.24" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.6" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.11" />
<PackageReference Include="swashbuckle.AspNetCore.Swagger" Version="6.5.0" />
<PackageReference Include="swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
<PackageReference Include="swashbuckle.AspNetCore.SwaggerUi" Version="6.5.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Fusion.Resources.Api.Notifications
{
public partial class InternalRequestNotification
{
public class NotifyRequestCreatorHandler : AsyncRequestHandler<NotifyRequestCreator>
public class NotifyRequestCreatorHandler : IRequestHandler<NotifyRequestCreator>
{
private readonly IFusionNotificationClient notificationClient;
private readonly IMediator mediator;
Expand All @@ -22,7 +22,7 @@ public NotifyRequestCreatorHandler(IFusionNotificationClient notificationClient,
this.notificationClient = notificationClient;
this.mediator = mediator;
}
protected override async Task Handle(NotifyRequestCreator request, CancellationToken cancellationToken)
public async Task Handle(NotifyRequestCreator request, CancellationToken cancellationToken)
{
var allocationRequest = await GetInternalRequestAsync(request.RequestId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Fusion.Resources.Api.Notifications
{
public partial class InternalRequestNotification
{
public class NotifyResourceOwnerHandler : AsyncRequestHandler<NotifyResourceOwner>
public class NotifyResourceOwnerHandler : IRequestHandler<NotifyResourceOwner>
{
private readonly IFusionNotificationClient notificationClient;
private readonly IMediator mediator;
Expand All @@ -22,7 +22,7 @@ public NotifyResourceOwnerHandler(IFusionNotificationClient notificationClient,
this.notificationClient = notificationClient;
this.mediator = mediator;
}
protected override async Task Handle(NotifyResourceOwner request, CancellationToken cancellationToken)
public async Task Handle(NotifyResourceOwner request, CancellationToken cancellationToken)
{
var recipients = await GenerateRecipientsAsync(request.Editor.Person.AzureUniqueId, request.AssignedDepartment);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Fusion.Resources.Api.Notifications
{
public partial class InternalRequestNotification
{
public class NotifyTaskOwnerHandler : AsyncRequestHandler<NotifyTaskOwner>
public class NotifyTaskOwnerHandler : IRequestHandler<NotifyTaskOwner>
{
private readonly IFusionNotificationClient notificationClient;
private readonly IMediator mediator;
Expand All @@ -22,7 +22,7 @@ public NotifyTaskOwnerHandler(IFusionNotificationClient notificationClient, IMed
this.notificationClient = notificationClient;
this.mediator = mediator;
}
protected override async Task Handle(NotifyTaskOwner request, CancellationToken cancellationToken)
public async Task Handle(NotifyTaskOwner request, CancellationToken cancellationToken)
{
var allocationRequest = await GetInternalRequestAsync(request.RequestId);

Expand Down
14 changes: 12 additions & 2 deletions src/backend/api/Fusion.Resources.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Fusion.Resources.Api.HostedServices;
using Fusion.Resources.Api.Middleware;
using Fusion.Resources.Domain;
using Fusion.Resources.Domain.Commands;
using Fusion.Resources.Logic;
using JSM.FluentValidation.AspNet.AsyncFilter;
using MediatR;
using Microsoft.ApplicationInsights.DependencyCollector;
Expand All @@ -19,6 +21,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using SixLabors.ImageSharp;
using System.Reflection;

namespace Fusion.Resources.Api
{
Expand Down Expand Up @@ -127,11 +131,17 @@ public void ConfigureServices(IServiceCollection services)

services.AddResourceDatabase<Authentication.SqlTokenProvider>(Configuration);
services.AddResourceDomain();
services.AddResourceLogic();
services.AddResourcesApplicationServices();

services.AddResourcesAuthorizationHandlers();
services.AddMediatR(typeof(Startup)); // Add notification handlers in api project

// Add mediatn from api, domain and logic assembly.
services.AddMediatR(c => c
.RegisterServicesFromAssemblyContaining<DomainAssemblyMarkerType>()
.RegisterServicesFromAssemblyContaining<LogicAssemblyMarkerType>()
.RegisterServicesFromAssemblyContaining<Startup>());

services.AddMediatRDistributedNotification(setup => setup.ConnectionString = Configuration.GetConnectionString("ServiceBus"));
services.AddHostedService<ExpiredDelegatedRolesHostedService>();

#endregion Resource services
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Fusion.Integration.Profile" Version="6.0.7" />
<PackageReference Include="Fusion.Integration.Profile" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
Expand Down
Loading

0 comments on commit 8744b58

Please sign in to comment.