mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 23:52:50 -05:00
[SM-895] Enforce project maximums (#3214)
* Add ProjectLimitQuery * Add query to DI * Add unit tests * Add query to controller * Add controller unit tests * add integration tests * rename query and variables * More renaming
This commit is contained in:
@ -7,6 +7,7 @@ using Bit.Core.Exceptions;
|
||||
using Bit.Core.SecretsManager.AuthorizationRequirements;
|
||||
using Bit.Core.SecretsManager.Commands.Projects.Interfaces;
|
||||
using Bit.Core.SecretsManager.Entities;
|
||||
using Bit.Core.SecretsManager.Queries.Projects.Interfaces;
|
||||
using Bit.Core.SecretsManager.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Core.Utilities;
|
||||
@ -22,6 +23,7 @@ public class ProjectsController : Controller
|
||||
private readonly ICurrentContext _currentContext;
|
||||
private readonly IUserService _userService;
|
||||
private readonly IProjectRepository _projectRepository;
|
||||
private readonly IMaxProjectsQuery _maxProjectsQuery;
|
||||
private readonly ICreateProjectCommand _createProjectCommand;
|
||||
private readonly IUpdateProjectCommand _updateProjectCommand;
|
||||
private readonly IDeleteProjectCommand _deleteProjectCommand;
|
||||
@ -31,6 +33,7 @@ public class ProjectsController : Controller
|
||||
ICurrentContext currentContext,
|
||||
IUserService userService,
|
||||
IProjectRepository projectRepository,
|
||||
IMaxProjectsQuery maxProjectsQuery,
|
||||
ICreateProjectCommand createProjectCommand,
|
||||
IUpdateProjectCommand updateProjectCommand,
|
||||
IDeleteProjectCommand deleteProjectCommand,
|
||||
@ -39,6 +42,7 @@ public class ProjectsController : Controller
|
||||
_currentContext = currentContext;
|
||||
_userService = userService;
|
||||
_projectRepository = projectRepository;
|
||||
_maxProjectsQuery = maxProjectsQuery;
|
||||
_createProjectCommand = createProjectCommand;
|
||||
_updateProjectCommand = updateProjectCommand;
|
||||
_deleteProjectCommand = deleteProjectCommand;
|
||||
@ -74,6 +78,13 @@ public class ProjectsController : Controller
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
var (max, atMax) = await _maxProjectsQuery.GetByOrgIdAsync(organizationId);
|
||||
if (atMax != null && atMax.Value)
|
||||
{
|
||||
throw new BadRequestException($"You have reached the maximum number of projects ({max}) for this plan.");
|
||||
}
|
||||
|
||||
var userId = _userService.GetProperUserId(User).Value;
|
||||
var result = await _createProjectCommand.CreateAsync(project, userId, _currentContext.ClientType);
|
||||
|
||||
|
@ -0,0 +1,6 @@
|
||||
namespace Bit.Core.SecretsManager.Queries.Projects.Interfaces;
|
||||
|
||||
public interface IMaxProjectsQuery
|
||||
{
|
||||
Task<(short? max, bool? atMax)> GetByOrgIdAsync(Guid organizationId);
|
||||
}
|
Reference in New Issue
Block a user