1
0
mirror of https://github.com/bitwarden/server.git synced 2025-05-21 03:24:31 -05:00
bitwarden/src/Core/OrganizationFeatures/Groups/UpdateGroupCommand.cs
Rui Tomé e042360c00
[EC-654] Create commands for Group Create and Group Update (#2442)
* [EC-654] Add CreateGroupCommand and UpdateGroupCommand

Added new CQRS commands CreateGroupCommand and UpdateGroupCommand
Updated GroupService to use new commands
Edited existing GroupServiceTests and added new tests for the new commands

* [EC-654] dotnet format

* [EC-654] Replace GroupService.SaveAsync with CreateGroup and UpdateGroup commands

* [EC-654] Add assertions to check calls on IReferenceEventService

* [EC-654] Use AssertHelper.AssertRecent for DateTime properties

* [EC-654] Extracted database reads from CreateGroupCommand and UpdateGroupCommand. Added unit tests.

* [EC-654] Changed CreateGroupCommand and UpdateGroupCommand Validate method to private
2022-12-12 09:59:48 +00:00

67 lines
2.0 KiB
C#

using Bit.Core.Entities;
using Bit.Core.Enums;
using Bit.Core.Exceptions;
using Bit.Core.Models.Data;
using Bit.Core.OrganizationFeatures.Groups.Interfaces;
using Bit.Core.Repositories;
using Bit.Core.Services;
namespace Bit.Core.OrganizationFeatures.Groups;
public class UpdateGroupCommand : IUpdateGroupCommand
{
private readonly IEventService _eventService;
private readonly IGroupRepository _groupRepository;
public UpdateGroupCommand(
IEventService eventService,
IGroupRepository groupRepository)
{
_eventService = eventService;
_groupRepository = groupRepository;
}
public async Task UpdateGroupAsync(Group group, Organization organization,
IEnumerable<SelectionReadOnly> collections = null)
{
Validate(organization);
await GroupRepositoryUpdateGroupAsync(group, collections);
await _eventService.LogGroupEventAsync(group, Enums.EventType.Group_Updated);
}
public async Task UpdateGroupAsync(Group group, Organization organization, EventSystemUser systemUser,
IEnumerable<SelectionReadOnly> collections = null)
{
Validate(organization);
await GroupRepositoryUpdateGroupAsync(group, collections);
await _eventService.LogGroupEventAsync(group, Enums.EventType.Group_Updated, systemUser);
}
private async Task GroupRepositoryUpdateGroupAsync(Group group, IEnumerable<SelectionReadOnly> collections = null)
{
group.RevisionDate = DateTime.UtcNow;
if (collections == null)
{
await _groupRepository.ReplaceAsync(group);
}
else
{
await _groupRepository.ReplaceAsync(group, collections);
}
}
private static void Validate(Organization organization)
{
if (organization == null)
{
throw new BadRequestException("Organization not found");
}
if (!organization.UseGroups)
{
throw new BadRequestException("This organization cannot use groups.");
}
}
}