mirror of
https://github.com/bitwarden/server.git
synced 2025-04-04 12:40:22 -05:00
only create security tasks when a task doesn't exist for the submitted cipher (#5558)
This commit is contained in:
parent
d4b0058372
commit
6178bb2db1
@ -5,6 +5,7 @@ using Bit.Core;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Core.Utilities;
|
||||
using Bit.Core.Vault.Commands.Interfaces;
|
||||
using Bit.Core.Vault.Entities;
|
||||
using Bit.Core.Vault.Enums;
|
||||
using Bit.Core.Vault.Queries;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
@ -89,11 +90,28 @@ public class SecurityTaskController : Controller
|
||||
public async Task<ListResponseModel<SecurityTasksResponseModel>> BulkCreateTasks(Guid orgId,
|
||||
[FromBody] BulkCreateSecurityTasksRequestModel model)
|
||||
{
|
||||
var securityTasks = await _createManyTasksCommand.CreateAsync(orgId, model.Tasks);
|
||||
// Retrieve existing pending security tasks for the organization
|
||||
var pendingSecurityTasks = await _getTasksForOrganizationQuery.GetTasksAsync(orgId, SecurityTaskStatus.Pending);
|
||||
|
||||
await _createManyTaskNotificationsCommand.CreateAsync(orgId, securityTasks);
|
||||
// Get the security tasks that are already associated with a cipher within the submitted model
|
||||
var existingTasks = pendingSecurityTasks.Where(x => model.Tasks.Any(y => y.CipherId == x.CipherId)).ToList();
|
||||
|
||||
var response = securityTasks.Select(x => new SecurityTasksResponseModel(x)).ToList();
|
||||
// Get tasks that need to be created
|
||||
var tasksToCreateFromModel = model.Tasks.Where(x => !existingTasks.Any(y => y.CipherId == x.CipherId)).ToList();
|
||||
|
||||
ICollection<SecurityTask> newSecurityTasks = new List<SecurityTask>();
|
||||
|
||||
if (tasksToCreateFromModel.Count != 0)
|
||||
{
|
||||
newSecurityTasks = await _createManyTasksCommand.CreateAsync(orgId, tasksToCreateFromModel);
|
||||
}
|
||||
|
||||
// Combine existing tasks and newly created tasks
|
||||
var allTasks = existingTasks.Concat(newSecurityTasks);
|
||||
|
||||
await _createManyTaskNotificationsCommand.CreateAsync(orgId, allTasks);
|
||||
|
||||
var response = allTasks.Select(x => new SecurityTasksResponseModel(x)).ToList();
|
||||
return new ListResponseModel<SecurityTasksResponseModel>(response);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user