mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 08:02:49 -05:00
[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
This commit is contained in:
@ -1,13 +1,10 @@
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Data;
|
||||
|
||||
namespace Bit.Core.Services;
|
||||
|
||||
public interface IGroupService
|
||||
{
|
||||
Task SaveAsync(Group group, IEnumerable<SelectionReadOnly> collections = null);
|
||||
Task SaveAsync(Group group, EventSystemUser systemUser, IEnumerable<SelectionReadOnly> collections = null);
|
||||
[Obsolete("IDeleteGroupCommand should be used instead. To be removed by EC-608.")]
|
||||
Task DeleteAsync(Group group);
|
||||
[Obsolete("IDeleteGroupCommand should be used instead. To be removed by EC-608.")]
|
||||
|
@ -1,8 +1,6 @@
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Models.Business;
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Core.Repositories;
|
||||
|
||||
namespace Bit.Core.Services;
|
||||
@ -10,96 +8,17 @@ namespace Bit.Core.Services;
|
||||
public class GroupService : IGroupService
|
||||
{
|
||||
private readonly IEventService _eventService;
|
||||
private readonly IOrganizationRepository _organizationRepository;
|
||||
private readonly IOrganizationUserRepository _organizationUserRepository;
|
||||
private readonly IGroupRepository _groupRepository;
|
||||
private readonly IReferenceEventService _referenceEventService;
|
||||
|
||||
public GroupService(
|
||||
IEventService eventService,
|
||||
IOrganizationRepository organizationRepository,
|
||||
IOrganizationUserRepository organizationUserRepository,
|
||||
IGroupRepository groupRepository,
|
||||
IReferenceEventService referenceEventService)
|
||||
IGroupRepository groupRepository)
|
||||
{
|
||||
_eventService = eventService;
|
||||
_organizationRepository = organizationRepository;
|
||||
_organizationUserRepository = organizationUserRepository;
|
||||
_groupRepository = groupRepository;
|
||||
_referenceEventService = referenceEventService;
|
||||
}
|
||||
|
||||
public async Task SaveAsync(Group group,
|
||||
IEnumerable<SelectionReadOnly> collections = null)
|
||||
{
|
||||
await GroupRepositorySaveAsync(group, systemUser: null, collections);
|
||||
}
|
||||
|
||||
public async Task SaveAsync(Group group, EventSystemUser systemUser,
|
||||
IEnumerable<SelectionReadOnly> collections = null)
|
||||
{
|
||||
await GroupRepositorySaveAsync(group, systemUser, collections);
|
||||
}
|
||||
|
||||
private async Task GroupRepositorySaveAsync(Group group, EventSystemUser? systemUser, IEnumerable<SelectionReadOnly> collections = null)
|
||||
{
|
||||
var org = await _organizationRepository.GetByIdAsync(group.OrganizationId);
|
||||
if (org == null)
|
||||
{
|
||||
throw new BadRequestException("Organization not found");
|
||||
}
|
||||
|
||||
if (!org.UseGroups)
|
||||
{
|
||||
throw new BadRequestException("This organization cannot use groups.");
|
||||
}
|
||||
|
||||
if (group.Id == default(Guid))
|
||||
{
|
||||
group.CreationDate = group.RevisionDate = DateTime.UtcNow;
|
||||
|
||||
if (collections == null)
|
||||
{
|
||||
await _groupRepository.CreateAsync(group);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _groupRepository.CreateAsync(group, collections);
|
||||
}
|
||||
|
||||
if (systemUser.HasValue)
|
||||
{
|
||||
await _eventService.LogGroupEventAsync(group, Enums.EventType.Group_Created, systemUser.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _eventService.LogGroupEventAsync(group, Enums.EventType.Group_Created);
|
||||
}
|
||||
|
||||
await _referenceEventService.RaiseEventAsync(new ReferenceEvent(ReferenceEventType.GroupCreated, org));
|
||||
}
|
||||
else
|
||||
{
|
||||
group.RevisionDate = DateTime.UtcNow;
|
||||
|
||||
if (collections == null)
|
||||
{
|
||||
await _groupRepository.ReplaceAsync(group);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _groupRepository.ReplaceAsync(group, collections);
|
||||
}
|
||||
|
||||
if (systemUser.HasValue)
|
||||
{
|
||||
await _eventService.LogGroupEventAsync(group, Enums.EventType.Group_Updated, systemUser.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _eventService.LogGroupEventAsync(group, Enums.EventType.Group_Updated);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Obsolete("IDeleteGroupCommand should be used instead. To be removed by EC-608.")]
|
||||
|
Reference in New Issue
Block a user