2024-12-12 14:27:31 -05:00
|
|
|
|
using Bit.Api.Models.Response;
|
2025-02-05 16:56:01 -05:00
|
|
|
|
using Bit.Api.Vault.Models.Request;
|
2024-12-12 14:27:31 -05:00
|
|
|
|
using Bit.Api.Vault.Models.Response;
|
|
|
|
|
|
using Bit.Core;
|
|
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
|
using Bit.Core.Utilities;
|
2024-12-13 14:50:20 -05:00
|
|
|
|
using Bit.Core.Vault.Commands.Interfaces;
|
2024-12-12 14:27:31 -05:00
|
|
|
|
using Bit.Core.Vault.Enums;
|
|
|
|
|
|
using Bit.Core.Vault.Queries;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.Vault.Controllers;
|
|
|
|
|
|
|
|
|
|
|
|
[Route("tasks")]
|
|
|
|
|
|
[Authorize("Application")]
|
|
|
|
|
|
[RequireFeature(FeatureFlagKeys.SecurityTasks)]
|
|
|
|
|
|
public class SecurityTaskController : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IUserService _userService;
|
|
|
|
|
|
private readonly IGetTaskDetailsForUserQuery _getTaskDetailsForUserQuery;
|
2024-12-13 14:50:20 -05:00
|
|
|
|
private readonly IMarkTaskAsCompleteCommand _markTaskAsCompleteCommand;
|
2025-01-09 12:40:12 -08:00
|
|
|
|
private readonly IGetTasksForOrganizationQuery _getTasksForOrganizationQuery;
|
2025-02-05 16:56:01 -05:00
|
|
|
|
private readonly ICreateManyTasksCommand _createManyTasksCommand;
|
2024-12-12 14:27:31 -05:00
|
|
|
|
|
2024-12-13 14:50:20 -05:00
|
|
|
|
public SecurityTaskController(
|
|
|
|
|
|
IUserService userService,
|
|
|
|
|
|
IGetTaskDetailsForUserQuery getTaskDetailsForUserQuery,
|
2025-01-09 12:40:12 -08:00
|
|
|
|
IMarkTaskAsCompleteCommand markTaskAsCompleteCommand,
|
2025-02-05 16:56:01 -05:00
|
|
|
|
IGetTasksForOrganizationQuery getTasksForOrganizationQuery,
|
|
|
|
|
|
ICreateManyTasksCommand createManyTasksCommand)
|
2024-12-12 14:27:31 -05:00
|
|
|
|
{
|
|
|
|
|
|
_userService = userService;
|
|
|
|
|
|
_getTaskDetailsForUserQuery = getTaskDetailsForUserQuery;
|
2024-12-13 14:50:20 -05:00
|
|
|
|
_markTaskAsCompleteCommand = markTaskAsCompleteCommand;
|
2025-01-09 12:40:12 -08:00
|
|
|
|
_getTasksForOrganizationQuery = getTasksForOrganizationQuery;
|
2025-02-05 16:56:01 -05:00
|
|
|
|
_createManyTasksCommand = createManyTasksCommand;
|
2024-12-12 14:27:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Retrieves security tasks for the current user.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="status">Optional filter for task status. If not provided returns tasks of all statuses.</param>
|
|
|
|
|
|
/// <returns>A list response model containing the security tasks for the user.</returns>
|
|
|
|
|
|
[HttpGet("")]
|
|
|
|
|
|
public async Task<ListResponseModel<SecurityTasksResponseModel>> Get([FromQuery] SecurityTaskStatus? status)
|
|
|
|
|
|
{
|
|
|
|
|
|
var userId = _userService.GetProperUserId(User).Value;
|
|
|
|
|
|
var securityTasks = await _getTaskDetailsForUserQuery.GetTaskDetailsForUserAsync(userId, status);
|
|
|
|
|
|
var response = securityTasks.Select(x => new SecurityTasksResponseModel(x)).ToList();
|
|
|
|
|
|
return new ListResponseModel<SecurityTasksResponseModel>(response);
|
|
|
|
|
|
}
|
2024-12-13 14:50:20 -05:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Marks a task as complete. The user must have edit permission on the cipher associated with the task.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="taskId">The unique identifier of the task to complete</param>
|
|
|
|
|
|
[HttpPatch("{taskId:guid}/complete")]
|
|
|
|
|
|
public async Task<IActionResult> Complete(Guid taskId)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _markTaskAsCompleteCommand.CompleteAsync(taskId);
|
|
|
|
|
|
return NoContent();
|
|
|
|
|
|
}
|
2025-01-09 12:40:12 -08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Retrieves security tasks for an organization. Restricted to organization administrators.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="organizationId">The organization Id</param>
|
|
|
|
|
|
/// <param name="status">Optional filter for task status. If not provided, returns tasks of all statuses.</param>
|
|
|
|
|
|
[HttpGet("organization")]
|
|
|
|
|
|
public async Task<ListResponseModel<SecurityTasksResponseModel>> ListForOrganization(
|
|
|
|
|
|
[FromQuery] Guid organizationId, [FromQuery] SecurityTaskStatus? status)
|
|
|
|
|
|
{
|
|
|
|
|
|
var securityTasks = await _getTasksForOrganizationQuery.GetTasksAsync(organizationId, status);
|
|
|
|
|
|
var response = securityTasks.Select(x => new SecurityTasksResponseModel(x)).ToList();
|
|
|
|
|
|
return new ListResponseModel<SecurityTasksResponseModel>(response);
|
|
|
|
|
|
}
|
2025-02-05 16:56:01 -05:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Bulk create security tasks for an organization.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="orgId"></param>
|
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
|
/// <returns>A list response model containing the security tasks created for the organization.</returns>
|
|
|
|
|
|
[HttpPost("{orgId:guid}/bulk-create")]
|
|
|
|
|
|
public async Task<ListResponseModel<SecurityTasksResponseModel>> BulkCreateTasks(Guid orgId,
|
|
|
|
|
|
[FromBody] BulkCreateSecurityTasksRequestModel model)
|
|
|
|
|
|
{
|
|
|
|
|
|
var securityTasks = await _createManyTasksCommand.CreateAsync(orgId, model.Tasks);
|
|
|
|
|
|
var response = securityTasks.Select(x => new SecurityTasksResponseModel(x)).ToList();
|
|
|
|
|
|
return new ListResponseModel<SecurityTasksResponseModel>(response);
|
|
|
|
|
|
}
|
2024-12-12 14:27:31 -05:00
|
|
|
|
}
|