mirror of
https://github.com/bitwarden/server.git
synced 2025-07-04 09:32:48 -05:00
[AC-1750] AC Team code ownership moves - Groups (#3358)
This commit is contained in:
24
src/Core/AdminConsole/Entities/Group.cs
Normal file
24
src/Core/AdminConsole/Entities/Group.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Models;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Core.AdminConsole.Entities;
|
||||
|
||||
public class Group : ITableObject<Guid>, IExternal
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid OrganizationId { get; set; }
|
||||
[MaxLength(100)]
|
||||
public string Name { get; set; }
|
||||
public bool AccessAll { get; set; }
|
||||
[MaxLength(300)]
|
||||
public string ExternalId { get; set; }
|
||||
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
|
||||
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;
|
||||
|
||||
public void SetNewId()
|
||||
{
|
||||
Id = CoreHelpers.GenerateComb();
|
||||
}
|
||||
}
|
7
src/Core/AdminConsole/Entities/GroupUser.cs
Normal file
7
src/Core/AdminConsole/Entities/GroupUser.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace Bit.Core.AdminConsole.Entities;
|
||||
|
||||
public class GroupUser
|
||||
{
|
||||
public Guid GroupId { get; set; }
|
||||
public Guid OrganizationUserId { get; set; }
|
||||
}
|
9
src/Core/AdminConsole/Models/Business/ImportedGroup.cs
Normal file
9
src/Core/AdminConsole/Models/Business/ImportedGroup.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
|
||||
namespace Bit.Core.AdminConsole.Models.Business;
|
||||
|
||||
public class ImportedGroup
|
||||
{
|
||||
public Group Group { get; set; }
|
||||
public HashSet<string> ExternalUserIds { get; set; }
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using System.Data;
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
|
||||
namespace Bit.Core.AdminConsole.Models.Data;
|
||||
|
||||
public class GroupWithCollections : Group
|
||||
{
|
||||
public DataTable Collections { get; set; }
|
||||
}
|
@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
22
src/Core/AdminConsole/Repositories/IGroupRepository.cs
Normal file
22
src/Core/AdminConsole/Repositories/IGroupRepository.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Core.Repositories;
|
||||
|
||||
namespace Bit.Core.AdminConsole.Repositories;
|
||||
|
||||
public interface IGroupRepository : IRepository<Group, Guid>
|
||||
{
|
||||
Task<Tuple<Group, ICollection<CollectionAccessSelection>>> GetByIdWithCollectionsAsync(Guid id);
|
||||
Task<ICollection<Group>> GetManyByOrganizationIdAsync(Guid organizationId);
|
||||
Task<ICollection<Tuple<Group, ICollection<CollectionAccessSelection>>>> GetManyWithCollectionsByOrganizationIdAsync(
|
||||
Guid organizationId);
|
||||
Task<ICollection<Group>> GetManyByManyIds(IEnumerable<Guid> groupIds);
|
||||
Task<ICollection<Guid>> GetManyIdsByUserIdAsync(Guid organizationUserId);
|
||||
Task<ICollection<Guid>> GetManyUserIdsByIdAsync(Guid id);
|
||||
Task<ICollection<GroupUser>> GetManyGroupUsersByOrganizationIdAsync(Guid organizationId);
|
||||
Task CreateAsync(Group obj, IEnumerable<CollectionAccessSelection> collections);
|
||||
Task ReplaceAsync(Group obj, IEnumerable<CollectionAccessSelection> collections);
|
||||
Task DeleteUserAsync(Guid groupId, Guid organizationUserId);
|
||||
Task UpdateUsersAsync(Guid groupId, IEnumerable<Guid> organizationUserIds);
|
||||
Task DeleteManyAsync(IEnumerable<Guid> groupIds);
|
||||
}
|
14
src/Core/AdminConsole/Services/IGroupService.cs
Normal file
14
src/Core/AdminConsole/Services/IGroupService.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.Enums;
|
||||
|
||||
namespace Bit.Core.AdminConsole.Services;
|
||||
|
||||
public interface IGroupService
|
||||
{
|
||||
[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.")]
|
||||
Task DeleteAsync(Group group, EventSystemUser systemUser);
|
||||
Task DeleteUserAsync(Group group, Guid organizationUserId);
|
||||
Task DeleteUserAsync(Group group, Guid organizationUserId, EventSystemUser systemUser);
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.AdminConsole.Repositories;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
|
||||
namespace Bit.Core.AdminConsole.Services.Implementations;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user