mirror of https://github.com/Squidex/squidex.git
Browse Source
* Gateway to restrict total number of apps. * Fixes. * Fix permission check.pull/761/head
committed by
GitHub
14 changed files with 304 additions and 8 deletions
@ -0,0 +1,62 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.Options; |
|||
using Squidex.Domain.Apps.Entities.Apps.Commands; |
|||
using Squidex.Infrastructure.Commands; |
|||
using Squidex.Infrastructure.Translations; |
|||
using Squidex.Infrastructure.Validation; |
|||
using Squidex.Shared.Identity; |
|||
using Squidex.Shared.Users; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Apps.Plans |
|||
{ |
|||
public sealed class RestrictAppsCommandMiddleware : ICommandMiddleware |
|||
{ |
|||
private readonly RestrictAppsOptions usageOptions; |
|||
private readonly IUserResolver userResolver; |
|||
|
|||
public RestrictAppsCommandMiddleware(IOptions<RestrictAppsOptions> usageOptions, IUserResolver userResolver) |
|||
{ |
|||
this.usageOptions = usageOptions.Value; |
|||
this.userResolver = userResolver; |
|||
} |
|||
|
|||
public async Task HandleAsync(CommandContext context, NextDelegate next) |
|||
{ |
|||
if (usageOptions.MaximumNumberOfApps <= 0 || context.Command is not CreateApp createApp || createApp.Actor.IsClient) |
|||
{ |
|||
await next(context); |
|||
return; |
|||
} |
|||
|
|||
var totalApps = 0; |
|||
|
|||
var user = await userResolver.FindByIdAsync(createApp.Actor.Identifier); |
|||
|
|||
if (user != null) |
|||
{ |
|||
totalApps = user.Claims.GetTotalApps(); |
|||
|
|||
if (totalApps >= usageOptions.MaximumNumberOfApps) |
|||
{ |
|||
throw new ValidationException(T.Get("apps.maximumTotalReached")); |
|||
} |
|||
} |
|||
|
|||
await next(context); |
|||
|
|||
if (context.IsCompleted && user != null) |
|||
{ |
|||
var newApps = totalApps + 1; |
|||
|
|||
await userResolver.SetClaimAsync(user.Id, SquidexClaimTypes.TotalApps, newApps.ToString(), true); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Apps.Plans |
|||
{ |
|||
public sealed class RestrictAppsOptions |
|||
{ |
|||
public int MaximumNumberOfApps { get; set; } = 0; |
|||
} |
|||
} |
|||
@ -0,0 +1,178 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Linq; |
|||
using System.Security.Claims; |
|||
using System.Threading.Tasks; |
|||
using FakeItEasy; |
|||
using Microsoft.Extensions.Options; |
|||
using Squidex.Domain.Apps.Entities.Apps.Commands; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.Commands; |
|||
using Squidex.Infrastructure.Validation; |
|||
using Squidex.Shared.Identity; |
|||
using Squidex.Shared.Users; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Apps.Plans |
|||
{ |
|||
public sealed class RestrictAppsCommandMiddlewareTests |
|||
{ |
|||
private readonly IUserResolver userResolver = A.Fake<IUserResolver>(); |
|||
private readonly ICommandBus commandBus = A.Fake<ICommandBus>(); |
|||
private readonly RestrictAppsOptions options = new RestrictAppsOptions(); |
|||
private readonly RestrictAppsCommandMiddleware sut; |
|||
|
|||
public RestrictAppsCommandMiddlewareTests() |
|||
{ |
|||
sut = new RestrictAppsCommandMiddleware(Options.Create(options), userResolver); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_throw_exception_if_number_of_apps_reached() |
|||
{ |
|||
var userId = Guid.NewGuid().ToString(); |
|||
|
|||
var command = new CreateApp |
|||
{ |
|||
Actor = RefToken.User(userId) |
|||
}; |
|||
|
|||
var commandContext = new CommandContext(command, commandBus); |
|||
|
|||
options.MaximumNumberOfApps = 3; |
|||
|
|||
var user = A.Fake<IUser>(); |
|||
|
|||
A.CallTo(() => user.Id) |
|||
.Returns(userId); |
|||
|
|||
A.CallTo(() => user.Claims) |
|||
.Returns(Enumerable.Repeat(new Claim(SquidexClaimTypes.TotalApps, "5"), 1).ToList()); |
|||
|
|||
A.CallTo(() => userResolver.FindByIdAsync(userId)) |
|||
.Returns(user); |
|||
|
|||
var isNextCalled = false; |
|||
|
|||
await Assert.ThrowsAsync<ValidationException>(() => sut.HandleAsync(commandContext, x => |
|||
{ |
|||
isNextCalled = true; |
|||
|
|||
return Task.CompletedTask; |
|||
})); |
|||
|
|||
Assert.False(isNextCalled); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_increment_total_apps_if_maximum_not_reached_and_completed() |
|||
{ |
|||
var userId = Guid.NewGuid().ToString(); |
|||
|
|||
var command = new CreateApp |
|||
{ |
|||
Actor = RefToken.User(userId) |
|||
}; |
|||
|
|||
var commandContext = new CommandContext(command, commandBus); |
|||
|
|||
options.MaximumNumberOfApps = 10; |
|||
|
|||
var user = A.Fake<IUser>(); |
|||
|
|||
A.CallTo(() => user.Id) |
|||
.Returns(userId); |
|||
|
|||
A.CallTo(() => user.Claims) |
|||
.Returns(Enumerable.Repeat(new Claim(SquidexClaimTypes.TotalApps, "5"), 1).ToList()); |
|||
|
|||
A.CallTo(() => userResolver.FindByIdAsync(userId)) |
|||
.Returns(user); |
|||
|
|||
await sut.HandleAsync(commandContext, x => |
|||
{ |
|||
x.Complete(true); |
|||
|
|||
return Task.CompletedTask; |
|||
}); |
|||
|
|||
A.CallTo(() => userResolver.SetClaimAsync(userId, SquidexClaimTypes.TotalApps, "6", true)) |
|||
.MustHaveHappened(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_not_check_usage_if_app_is_created_by_client() |
|||
{ |
|||
var command = new CreateApp |
|||
{ |
|||
Actor = RefToken.Client(Guid.NewGuid().ToString()) |
|||
}; |
|||
|
|||
var commandContext = new CommandContext(command, commandBus); |
|||
|
|||
options.MaximumNumberOfApps = 10; |
|||
|
|||
await sut.HandleAsync(commandContext, x => |
|||
{ |
|||
x.Complete(true); |
|||
|
|||
return Task.CompletedTask; |
|||
}); |
|||
|
|||
A.CallTo(() => userResolver.FindByIdAsync(A<string>._)) |
|||
.MustNotHaveHappened(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_not_check_usage_if_no_maximum_configured() |
|||
{ |
|||
var command = new CreateApp |
|||
{ |
|||
Actor = RefToken.User(Guid.NewGuid().ToString()) |
|||
}; |
|||
|
|||
var commandContext = new CommandContext(command, commandBus); |
|||
|
|||
options.MaximumNumberOfApps = 0; |
|||
|
|||
await sut.HandleAsync(commandContext, x => |
|||
{ |
|||
x.Complete(true); |
|||
|
|||
return Task.CompletedTask; |
|||
}); |
|||
|
|||
A.CallTo(() => userResolver.FindByIdAsync(A<string>._)) |
|||
.MustNotHaveHappened(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_not_check_usage_for_other_commands() |
|||
{ |
|||
var command = new UpdateApp |
|||
{ |
|||
Actor = RefToken.User(Guid.NewGuid().ToString()) |
|||
}; |
|||
|
|||
var commandContext = new CommandContext(command, commandBus); |
|||
|
|||
options.MaximumNumberOfApps = 10; |
|||
|
|||
await sut.HandleAsync(commandContext, x => |
|||
{ |
|||
x.Complete(true); |
|||
|
|||
return Task.CompletedTask; |
|||
}); |
|||
|
|||
A.CallTo(() => userResolver.FindByIdAsync(A<string>._)) |
|||
.MustNotHaveHappened(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue