mirror of
https://github.com/bitwarden/server.git
synced 2025-04-04 12:40:22 -05:00
Add OrganizationUpdateKeysCommand
This commit is contained in:
parent
8fd48374dc
commit
cc2741735f
@ -0,0 +1,13 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
|
||||
public interface IOrganizationUpdateKeysCommand
|
||||
{
|
||||
/// <summary>
|
||||
/// Update the keys for an organization.
|
||||
/// </summary>
|
||||
/// <param name="orgId">The ID of the organization to update.</param>
|
||||
/// <param name="publicKey">The public key for the organization.</param>
|
||||
/// <param name="privateKey">The private key for the organization.</param>
|
||||
/// <returns>The updated organization.</returns>
|
||||
Task<Organization> UpdateOrganizationKeysAsync(Guid orgId, string publicKey, string privateKey);
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.Context;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
|
||||
public class OrganizationUpdateKeysCommand : IOrganizationUpdateKeysCommand
|
||||
{
|
||||
private readonly ICurrentContext _currentContext;
|
||||
private readonly IOrganizationRepository _organizationRepository;
|
||||
private readonly IOrganizationService _organizationService;
|
||||
|
||||
public const string OrganizationKeysAlreadyExistErrorMessage = "Organization Keys already exist.";
|
||||
|
||||
public OrganizationUpdateKeysCommand(
|
||||
ICurrentContext currentContext,
|
||||
IOrganizationRepository organizationRepository,
|
||||
IOrganizationService organizationService)
|
||||
{
|
||||
_currentContext = currentContext;
|
||||
_organizationRepository = organizationRepository;
|
||||
_organizationService = organizationService;
|
||||
}
|
||||
|
||||
public async Task<Organization> UpdateOrganizationKeysAsync(Guid organizationId, string publicKey, string privateKey)
|
||||
{
|
||||
if (!await _currentContext.ManageResetPassword(organizationId))
|
||||
{
|
||||
throw new UnauthorizedAccessException();
|
||||
}
|
||||
|
||||
// If the keys already exist, error out
|
||||
var organization = await _organizationRepository.GetByIdAsync(organizationId);
|
||||
if (organization.PublicKey != null && organization.PrivateKey != null)
|
||||
{
|
||||
throw new BadRequestException(OrganizationKeysAlreadyExistErrorMessage);
|
||||
}
|
||||
|
||||
// Update org with generated public/private key
|
||||
organization.PublicKey = publicKey;
|
||||
organization.PrivateKey = privateKey;
|
||||
|
||||
await _organizationService.UpdateAsync(organization);
|
||||
|
||||
return organization;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user