mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 08:02:49 -05:00
[AC-1750] AC Team code ownership moves - Groups (#3358)
This commit is contained in:
@ -0,0 +1,119 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.AdminConsole.OrganizationFeatures.Groups.Interfaces;
|
||||
using Bit.Core.AdminConsole.Repositories;
|
||||
using Bit.Core.Context;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Core.Tools.Enums;
|
||||
using Bit.Core.Tools.Models.Business;
|
||||
using Bit.Core.Tools.Services;
|
||||
|
||||
namespace Bit.Core.AdminConsole.OrganizationFeatures.Groups;
|
||||
|
||||
public class CreateGroupCommand : ICreateGroupCommand
|
||||
{
|
||||
private readonly IEventService _eventService;
|
||||
private readonly IGroupRepository _groupRepository;
|
||||
private readonly IOrganizationUserRepository _organizationUserRepository;
|
||||
private readonly IReferenceEventService _referenceEventService;
|
||||
private readonly ICurrentContext _currentContext;
|
||||
|
||||
public CreateGroupCommand(
|
||||
IEventService eventService,
|
||||
IGroupRepository groupRepository,
|
||||
IOrganizationUserRepository organizationUserRepository,
|
||||
IReferenceEventService referenceEventService,
|
||||
ICurrentContext currentContext)
|
||||
{
|
||||
_eventService = eventService;
|
||||
_groupRepository = groupRepository;
|
||||
_organizationUserRepository = organizationUserRepository;
|
||||
_referenceEventService = referenceEventService;
|
||||
_currentContext = currentContext;
|
||||
}
|
||||
|
||||
public async Task CreateGroupAsync(Group group, Organization organization,
|
||||
IEnumerable<CollectionAccessSelection> collections = null,
|
||||
IEnumerable<Guid> users = null)
|
||||
{
|
||||
Validate(organization);
|
||||
await GroupRepositoryCreateGroupAsync(group, organization, collections);
|
||||
|
||||
if (users != null)
|
||||
{
|
||||
await GroupRepositoryUpdateUsersAsync(group, users);
|
||||
}
|
||||
|
||||
await _eventService.LogGroupEventAsync(group, Core.Enums.EventType.Group_Created);
|
||||
}
|
||||
|
||||
public async Task CreateGroupAsync(Group group, Organization organization, EventSystemUser systemUser,
|
||||
IEnumerable<CollectionAccessSelection> collections = null,
|
||||
IEnumerable<Guid> users = null)
|
||||
{
|
||||
Validate(organization);
|
||||
await GroupRepositoryCreateGroupAsync(group, organization, collections);
|
||||
|
||||
if (users != null)
|
||||
{
|
||||
await GroupRepositoryUpdateUsersAsync(group, users, systemUser);
|
||||
}
|
||||
|
||||
await _eventService.LogGroupEventAsync(group, Core.Enums.EventType.Group_Created, systemUser);
|
||||
}
|
||||
|
||||
private async Task GroupRepositoryCreateGroupAsync(Group group, Organization organization, IEnumerable<CollectionAccessSelection> collections = null)
|
||||
{
|
||||
group.CreationDate = group.RevisionDate = DateTime.UtcNow;
|
||||
|
||||
if (collections == null)
|
||||
{
|
||||
await _groupRepository.CreateAsync(group);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _groupRepository.CreateAsync(group, collections);
|
||||
}
|
||||
|
||||
await _referenceEventService.RaiseEventAsync(new ReferenceEvent(ReferenceEventType.GroupCreated, organization, _currentContext));
|
||||
}
|
||||
|
||||
private async Task GroupRepositoryUpdateUsersAsync(Group group, IEnumerable<Guid> userIds,
|
||||
EventSystemUser? systemUser = null)
|
||||
{
|
||||
var usersToAddToGroup = userIds as Guid[] ?? userIds.ToArray();
|
||||
|
||||
await _groupRepository.UpdateUsersAsync(group.Id, usersToAddToGroup);
|
||||
|
||||
var users = await _organizationUserRepository.GetManyAsync(usersToAddToGroup);
|
||||
var eventDate = DateTime.UtcNow;
|
||||
|
||||
if (systemUser.HasValue)
|
||||
{
|
||||
await _eventService.LogOrganizationUserEventsAsync(users.Select(u =>
|
||||
(u, EventType.OrganizationUser_UpdatedGroups, systemUser.Value, (DateTime?)eventDate)));
|
||||
}
|
||||
else
|
||||
{
|
||||
await _eventService.LogOrganizationUserEventsAsync(users.Select(u =>
|
||||
(u, EventType.OrganizationUser_UpdatedGroups, (DateTime?)eventDate)));
|
||||
}
|
||||
}
|
||||
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.AdminConsole.OrganizationFeatures.Groups.Interfaces;
|
||||
using Bit.Core.AdminConsole.Repositories;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Services;
|
||||
|
||||
namespace Bit.Core.AdminConsole.OrganizationFeatures.Groups;
|
||||
|
||||
public class DeleteGroupCommand : IDeleteGroupCommand
|
||||
{
|
||||
private readonly IGroupRepository _groupRepository;
|
||||
private readonly IEventService _eventService;
|
||||
|
||||
public DeleteGroupCommand(IGroupRepository groupRepository, IEventService eventService)
|
||||
{
|
||||
_groupRepository = groupRepository;
|
||||
_eventService = eventService;
|
||||
}
|
||||
|
||||
public async Task DeleteGroupAsync(Guid organizationId, Guid id)
|
||||
{
|
||||
var group = await GroupRepositoryDeleteGroupAsync(organizationId, id);
|
||||
await _eventService.LogGroupEventAsync(group, EventType.Group_Deleted);
|
||||
}
|
||||
|
||||
public async Task DeleteGroupAsync(Guid organizationId, Guid id, EventSystemUser eventSystemUser)
|
||||
{
|
||||
var group = await GroupRepositoryDeleteGroupAsync(organizationId, id);
|
||||
await _eventService.LogGroupEventAsync(group, EventType.Group_Deleted, eventSystemUser);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Group group)
|
||||
{
|
||||
await _groupRepository.DeleteAsync(group);
|
||||
await _eventService.LogGroupEventAsync(group, EventType.Group_Deleted);
|
||||
}
|
||||
|
||||
public async Task DeleteManyAsync(ICollection<Group> groups)
|
||||
{
|
||||
await _eventService.LogGroupEventsAsync(
|
||||
groups.Select(g =>
|
||||
(g, EventType.Group_Deleted, (EventSystemUser?)null, (DateTime?)DateTime.UtcNow)
|
||||
));
|
||||
|
||||
await _groupRepository.DeleteManyAsync(
|
||||
groups.Select(g => g.Id)
|
||||
);
|
||||
}
|
||||
|
||||
private async Task<Group> GroupRepositoryDeleteGroupAsync(Guid organizationId, Guid id)
|
||||
{
|
||||
var group = await _groupRepository.GetByIdAsync(id);
|
||||
if (group == null || group.OrganizationId != organizationId)
|
||||
{
|
||||
throw new NotFoundException("Group not found.");
|
||||
}
|
||||
|
||||
await _groupRepository.DeleteAsync(group);
|
||||
|
||||
return group;
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Data;
|
||||
|
||||
namespace Bit.Core.AdminConsole.OrganizationFeatures.Groups.Interfaces;
|
||||
|
||||
public interface ICreateGroupCommand
|
||||
{
|
||||
Task CreateGroupAsync(Group group, Organization organization,
|
||||
IEnumerable<CollectionAccessSelection> collections = null,
|
||||
IEnumerable<Guid> users = null);
|
||||
|
||||
Task CreateGroupAsync(Group group, Organization organization, EventSystemUser systemUser,
|
||||
IEnumerable<CollectionAccessSelection> collections = null,
|
||||
IEnumerable<Guid> users = null);
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.Enums;
|
||||
|
||||
namespace Bit.Core.AdminConsole.OrganizationFeatures.Groups.Interfaces;
|
||||
|
||||
public interface IDeleteGroupCommand
|
||||
{
|
||||
Task DeleteGroupAsync(Guid organizationId, Guid id);
|
||||
Task DeleteGroupAsync(Guid organizationId, Guid id, EventSystemUser eventSystemUser);
|
||||
Task DeleteAsync(Group group);
|
||||
Task DeleteManyAsync(ICollection<Group> groups);
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Data;
|
||||
|
||||
namespace Bit.Core.AdminConsole.OrganizationFeatures.Groups.Interfaces;
|
||||
|
||||
public interface IUpdateGroupCommand
|
||||
{
|
||||
Task UpdateGroupAsync(Group group, Organization organization,
|
||||
IEnumerable<CollectionAccessSelection> collections = null,
|
||||
IEnumerable<Guid> users = null);
|
||||
|
||||
Task UpdateGroupAsync(Group group, Organization organization, EventSystemUser systemUser,
|
||||
IEnumerable<CollectionAccessSelection> collections = null,
|
||||
IEnumerable<Guid> users = null);
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.AdminConsole.OrganizationFeatures.Groups.Interfaces;
|
||||
using Bit.Core.AdminConsole.Repositories;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
|
||||
namespace Bit.Core.AdminConsole.OrganizationFeatures.Groups;
|
||||
|
||||
public class UpdateGroupCommand : IUpdateGroupCommand
|
||||
{
|
||||
private readonly IEventService _eventService;
|
||||
private readonly IGroupRepository _groupRepository;
|
||||
private readonly IOrganizationUserRepository _organizationUserRepository;
|
||||
|
||||
public UpdateGroupCommand(
|
||||
IEventService eventService,
|
||||
IGroupRepository groupRepository,
|
||||
IOrganizationUserRepository organizationUserRepository)
|
||||
{
|
||||
_eventService = eventService;
|
||||
_groupRepository = groupRepository;
|
||||
_organizationUserRepository = organizationUserRepository;
|
||||
}
|
||||
|
||||
public async Task UpdateGroupAsync(Group group, Organization organization,
|
||||
IEnumerable<CollectionAccessSelection> collections = null,
|
||||
IEnumerable<Guid> userIds = null)
|
||||
{
|
||||
Validate(organization);
|
||||
await GroupRepositoryUpdateGroupAsync(group, collections);
|
||||
|
||||
if (userIds != null)
|
||||
{
|
||||
await GroupRepositoryUpdateUsersAsync(group, userIds);
|
||||
}
|
||||
|
||||
await _eventService.LogGroupEventAsync(group, Core.Enums.EventType.Group_Updated);
|
||||
}
|
||||
|
||||
public async Task UpdateGroupAsync(Group group, Organization organization, EventSystemUser systemUser,
|
||||
IEnumerable<CollectionAccessSelection> collections = null,
|
||||
IEnumerable<Guid> userIds = null)
|
||||
{
|
||||
Validate(organization);
|
||||
await GroupRepositoryUpdateGroupAsync(group, collections);
|
||||
|
||||
if (userIds != null)
|
||||
{
|
||||
await GroupRepositoryUpdateUsersAsync(group, userIds, systemUser);
|
||||
}
|
||||
|
||||
await _eventService.LogGroupEventAsync(group, Core.Enums.EventType.Group_Updated, systemUser);
|
||||
}
|
||||
|
||||
private async Task GroupRepositoryUpdateGroupAsync(Group group, IEnumerable<CollectionAccessSelection> collections = null)
|
||||
{
|
||||
group.RevisionDate = DateTime.UtcNow;
|
||||
|
||||
if (collections == null)
|
||||
{
|
||||
await _groupRepository.ReplaceAsync(group);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _groupRepository.ReplaceAsync(group, collections);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task GroupRepositoryUpdateUsersAsync(Group group, IEnumerable<Guid> userIds, EventSystemUser? systemUser = null)
|
||||
{
|
||||
var newUserIds = userIds as Guid[] ?? userIds.ToArray();
|
||||
var originalUserIds = await _groupRepository.GetManyUserIdsByIdAsync(group.Id);
|
||||
|
||||
await _groupRepository.UpdateUsersAsync(group.Id, newUserIds);
|
||||
|
||||
// We only want to create events OrganizationUserEvents for those that were actually modified.
|
||||
// HashSet.SymmetricExceptWith is a convenient method of finding the difference between lists
|
||||
var changedUserIds = new HashSet<Guid>(originalUserIds);
|
||||
changedUserIds.SymmetricExceptWith(newUserIds);
|
||||
|
||||
// Fetch all changed users for logging the event
|
||||
var users = await _organizationUserRepository.GetManyAsync(changedUserIds);
|
||||
var eventDate = DateTime.UtcNow;
|
||||
|
||||
if (systemUser.HasValue)
|
||||
{
|
||||
await _eventService.LogOrganizationUserEventsAsync(users.Select(u =>
|
||||
(u, EventType.OrganizationUser_UpdatedGroups, systemUser.Value, (DateTime?)eventDate)));
|
||||
}
|
||||
else
|
||||
{
|
||||
await _eventService.LogOrganizationUserEventsAsync(users.Select(u =>
|
||||
(u, EventType.OrganizationUser_UpdatedGroups, (DateTime?)eventDate)));
|
||||
}
|
||||
}
|
||||
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user