1
0
mirror of https://github.com/bitwarden/server synced 2025-12-22 03:03:33 +00:00
Files
server/src/Core/Services/Implementations/GroupService.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

63 lines
2.4 KiB
C#

using Bit.Core.Entities;
using Bit.Core.Enums;
using Bit.Core.Exceptions;
using Bit.Core.Repositories;
namespace Bit.Core.Services;
public class GroupService : IGroupService
{
private readonly IEventService _eventService;
private readonly IOrganizationUserRepository _organizationUserRepository;
private readonly IGroupRepository _groupRepository;
public GroupService(
IEventService eventService,
IOrganizationUserRepository organizationUserRepository,
IGroupRepository groupRepository)
{
_eventService = eventService;
_organizationUserRepository = organizationUserRepository;
_groupRepository = groupRepository;
}
[Obsolete("IDeleteGroupCommand should be used instead. To be removed by EC-608.")]
public async Task DeleteAsync(Group group)
{
await _groupRepository.DeleteAsync(group);
await _eventService.LogGroupEventAsync(group, EventType.Group_Deleted);
}
[Obsolete("IDeleteGroupCommand should be used instead. To be removed by EC-608.")]
public async Task DeleteAsync(Group group, EventSystemUser systemUser)
{
await _groupRepository.DeleteAsync(group);
await _eventService.LogGroupEventAsync(group, EventType.Group_Deleted, systemUser);
}
public async Task DeleteUserAsync(Group group, Guid organizationUserId)
{
var orgUser = await GroupRepositoryDeleteUserAsync(group, organizationUserId, systemUser: null);
await _eventService.LogOrganizationUserEventAsync(orgUser, EventType.OrganizationUser_UpdatedGroups);
}
public async Task DeleteUserAsync(Group group, Guid organizationUserId, EventSystemUser systemUser)
{
var orgUser = await GroupRepositoryDeleteUserAsync(group, organizationUserId, systemUser);
await _eventService.LogOrganizationUserEventAsync(orgUser, EventType.OrganizationUser_UpdatedGroups, systemUser);
}
private async Task<OrganizationUser> GroupRepositoryDeleteUserAsync(Group group, Guid organizationUserId, EventSystemUser? systemUser)
{
var orgUser = await _organizationUserRepository.GetByIdAsync(organizationUserId);
if (orgUser == null || orgUser.OrganizationId != group.OrganizationId)
{
throw new NotFoundException();
}
await _groupRepository.DeleteUserAsync(group.Id, organizationUserId);
return orgUser;
}
}