Actually enforce user and group limits

This commit is contained in:
Jordan Brown
2020-12-29 20:39:23 -05:00
parent 38a8fde66c
commit 29485dec97
3 changed files with 38 additions and 0 deletions
@@ -608,5 +608,17 @@ namespace Tgstation.Server.Api.Models
/// </summary>
[Description("Cannot delete the user group as it is not empty!")]
UserGroupNotEmpty,
/// <summary>
/// Attempted to create an <see cref="User"/> but the configured limit has been reached.
/// </summary>
[Description("The user cannot be created because the configured limit has been reached!")]
UserLimitReached,
/// <summary>
/// Attempted to create an <see cref="UserGroup"/> but the configured limit has been reached.
/// </summary>
[Description("The user group cannot be created because the configured limit has been reached!")]
UserGroupLimitReached,
}
}
@@ -139,6 +139,14 @@ namespace Tgstation.Server.Host.Controllers
if (fail != null)
return fail;
var totalUsers = await DatabaseContext
.Users
.AsQueryable()
.CountAsync(cancellationToken)
.ConfigureAwait(false);
if (totalUsers >= generalConfiguration.UserLimit)
return Conflict(new ErrorMessage(ErrorCode.UserLimitReached));
var dbUser = await CreateNewUserFromModel(model, cancellationToken).ConfigureAwait(false);
if (dbUser == null)
return Gone();
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Linq;
using System.Threading;
@@ -8,6 +9,7 @@ using System.Threading.Tasks;
using Tgstation.Server.Api;
using Tgstation.Server.Api.Models;
using Tgstation.Server.Api.Rights;
using Tgstation.Server.Host.Configuration;
using Tgstation.Server.Host.Database;
using Tgstation.Server.Host.Security;
using Z.EntityFramework.Plus;
@@ -20,15 +22,22 @@ namespace Tgstation.Server.Host.Controllers
[Route(Routes.UserGroup)]
public class UserGroupController : ApiController
{
/// <summary>
/// The <see cref="GeneralConfiguration"/> for the <see cref="UserGroupController"/>.
/// </summary>
readonly GeneralConfiguration generalConfiguration;
/// <summary>
/// Initializes a new instance of the <see cref="UserGroupController"/> <see langword="clas"/>.
/// </summary>
/// <param name="databaseContext">The <see cref="IDatabaseContext"/> for the <see cref="ApiController"/></param>
/// <param name="authenticationContextFactory">The <see cref="IAuthenticationContextFactory"/> for the <see cref="ApiController"/></param>
/// <param name="generalConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="generalConfiguration"/>.</param>
/// <param name="logger">The <see cref="ILogger"/> for the <see cref="ApiController"/>.</param>
public UserGroupController(
IDatabaseContext databaseContext,
IAuthenticationContextFactory authenticationContextFactory,
IOptions<GeneralConfiguration> generalConfigurationOptions,
ILogger<UserGroupController> logger)
: base(
databaseContext,
@@ -36,6 +45,7 @@ namespace Tgstation.Server.Host.Controllers
logger,
true)
{
generalConfiguration = generalConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(generalConfigurationOptions));
}
/// <summary>
@@ -56,6 +66,14 @@ namespace Tgstation.Server.Host.Controllers
if (model.Name == null)
return BadRequest(new ErrorMessage(ErrorCode.ModelValidationFailure));
var totalGroups = await DatabaseContext
.Groups
.AsQueryable()
.CountAsync(cancellationToken)
.ConfigureAwait(false);
if (totalGroups >= generalConfiguration.UserGroupLimit)
return Conflict(new ErrorMessage(ErrorCode.UserGroupLimitReached));
var permissionSet = new Models.PermissionSet
{
AdministrationRights = model.PermissionSet?.AdministrationRights ?? AdministrationRights.None,