2024-07-16 10:47:28 +10:00
#nullable enable
using Bit.Core.AdminConsole.Entities ;
2023-10-20 06:37:46 +10:00
using Bit.Core.AdminConsole.OrganizationFeatures.Groups.Interfaces ;
using Bit.Core.AdminConsole.Repositories ;
2022-12-12 09:59:48 +00:00
using Bit.Core.Enums ;
using Bit.Core.Exceptions ;
using Bit.Core.Models.Data ;
using Bit.Core.Repositories ;
using Bit.Core.Services ;
2023-10-20 06:37:46 +10:00
namespace Bit.Core.AdminConsole.OrganizationFeatures.Groups ;
2022-12-12 09:59:48 +00:00
public class UpdateGroupCommand : IUpdateGroupCommand
{
private readonly IEventService _eventService ;
private readonly IGroupRepository _groupRepository ;
2023-01-19 17:00:54 +01:00
private readonly IOrganizationUserRepository _organizationUserRepository ;
2024-07-16 10:47:28 +10:00
private readonly ICollectionRepository _collectionRepository ;
2022-12-12 09:59:48 +00:00
public UpdateGroupCommand (
IEventService eventService ,
2023-01-19 17:00:54 +01:00
IGroupRepository groupRepository ,
2024-07-16 10:47:28 +10:00
IOrganizationUserRepository organizationUserRepository ,
ICollectionRepository collectionRepository )
2022-12-12 09:59:48 +00:00
{
_eventService = eventService ;
_groupRepository = groupRepository ;
2023-01-19 17:00:54 +01:00
_organizationUserRepository = organizationUserRepository ;
2024-07-16 10:47:28 +10:00
_collectionRepository = collectionRepository ;
2022-12-12 09:59:48 +00:00
}
public async Task UpdateGroupAsync ( Group group , Organization organization ,
2024-07-16 10:47:28 +10:00
ICollection < CollectionAccessSelection > ? collections = null ,
IEnumerable < Guid > ? userIds = null )
2022-12-12 09:59:48 +00:00
{
2024-07-16 10:47:28 +10:00
await ValidateAsync ( organization , group , collections , userIds ) ;
await SaveGroupWithCollectionsAsync ( group , collections ) ;
2023-01-19 17:00:54 +01:00
if ( userIds ! = null )
{
2024-07-16 10:47:28 +10:00
await SaveGroupUsersAsync ( group , userIds ) ;
2023-01-19 17:00:54 +01:00
}
2023-10-20 06:37:46 +10:00
await _eventService . LogGroupEventAsync ( group , Core . Enums . EventType . Group_Updated ) ;
2022-12-12 09:59:48 +00:00
}
public async Task UpdateGroupAsync ( Group group , Organization organization , EventSystemUser systemUser ,
2024-07-16 10:47:28 +10:00
ICollection < CollectionAccessSelection > ? collections = null ,
IEnumerable < Guid > ? userIds = null )
2022-12-12 09:59:48 +00:00
{
2024-07-16 10:47:28 +10:00
await ValidateAsync ( organization , group , collections , userIds ) ;
await SaveGroupWithCollectionsAsync ( group , collections ) ;
2023-01-19 17:00:54 +01:00
if ( userIds ! = null )
{
2024-07-16 10:47:28 +10:00
await SaveGroupUsersAsync ( group , userIds , systemUser ) ;
2023-01-19 17:00:54 +01:00
}
2023-10-20 06:37:46 +10:00
await _eventService . LogGroupEventAsync ( group , Core . Enums . EventType . Group_Updated , systemUser ) ;
2022-12-12 09:59:48 +00:00
}
2024-07-16 10:47:28 +10:00
private async Task SaveGroupWithCollectionsAsync ( Group group , IEnumerable < CollectionAccessSelection > ? collections = null )
2022-12-12 09:59:48 +00:00
{
group . RevisionDate = DateTime . UtcNow ;
if ( collections = = null )
{
await _groupRepository . ReplaceAsync ( group ) ;
}
else
{
await _groupRepository . ReplaceAsync ( group , collections ) ;
}
}
2024-07-16 10:47:28 +10:00
private async Task SaveGroupUsersAsync ( Group group , IEnumerable < Guid > userIds , EventSystemUser ? systemUser = null )
2023-01-19 17:00:54 +01:00
{
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 ) ) ) ;
}
}
2024-07-16 10:47:28 +10:00
private async Task ValidateAsync ( Organization organization , Group group , ICollection < CollectionAccessSelection > ? collectionAccess ,
IEnumerable < Guid > ? memberAccess )
2022-12-12 09:59:48 +00:00
{
2024-07-16 10:47:28 +10:00
// Avoid multiple enumeration
memberAccess = memberAccess ? . ToList ( ) ;
if ( organization = = null | | organization . Id ! = group . OrganizationId )
2022-12-12 09:59:48 +00:00
{
2024-07-16 10:47:28 +10:00
throw new NotFoundException ( ) ;
2022-12-12 09:59:48 +00:00
}
if ( ! organization . UseGroups )
{
throw new BadRequestException ( "This organization cannot use groups." ) ;
}
2024-01-22 08:56:20 +10:00
2024-07-16 10:47:28 +10:00
var originalGroup = await _groupRepository . GetByIdAsync ( group . Id ) ;
if ( originalGroup = = null | | originalGroup . OrganizationId ! = group . OrganizationId )
{
throw new NotFoundException ( ) ;
}
if ( collectionAccess ? . Any ( ) = = true )
{
await ValidateCollectionAccessAsync ( originalGroup , collectionAccess ) ;
}
if ( memberAccess ? . Any ( ) = = true )
{
await ValidateMemberAccessAsync ( originalGroup , memberAccess . ToList ( ) ) ;
}
var invalidAssociations = collectionAccess ? . Where ( cas = > cas . Manage & & ( cas . ReadOnly | | cas . HidePasswords ) ) ;
[AC-2646] Remove FC MVP dead code from Core (#4281)
* chore: remove fc refs in CreateGroup and UpdateGroup commands, refs AC-2646
* chore: remove fc refs and update interface to represent usage/get rid of double enumeration warnings, refs AC-2646
* chore: remove org/provider service fc callers, refs AC-2646
* chore: remove collection service fc callers, refs AC-2646
* chore: remove cipher service import ciphers fc callers, refs AC-2646
* fix: UpdateOrganizationUserCommandTests collections to list, refs AC-2646
* fix: update CreateGroupCommandTests, refs AC-2646
* fix: adjust UpdateGroupCommandTests, refs AC-2646
* fix: adjust UpdateOrganizationUserCommandTests for FC always true, refs AC-2646
* fix: update CollectionServiceTests, refs AC-2646
* fix: remove unnecessary test with fc disabled, refs AC-2646
* fix: update tests to account for AccessAll removal and Manager removal, refs AC-2646
* chore: remove dependence on FC flag for tests, refs AC-2646
2024-07-12 12:25:04 -05:00
if ( invalidAssociations ? . Any ( ) ? ? false )
{
throw new BadRequestException ( "The Manage property is mutually exclusive and cannot be true while the ReadOnly or HidePasswords properties are also true." ) ;
2024-01-22 08:56:20 +10:00
}
2022-12-12 09:59:48 +00:00
}
2024-07-16 10:47:28 +10:00
private async Task ValidateCollectionAccessAsync ( Group originalGroup ,
ICollection < CollectionAccessSelection > collectionAccess )
{
var collections = await _collectionRepository
. GetManyByManyIdsAsync ( collectionAccess . Select ( c = > c . Id ) ) ;
var collectionIds = collections . Select ( c = > c . Id ) ;
var missingCollection = collectionAccess
. FirstOrDefault ( cas = > ! collectionIds . Contains ( cas . Id ) ) ;
if ( missingCollection ! = default )
{
throw new NotFoundException ( ) ;
}
var invalidCollection = collections . FirstOrDefault ( c = > c . OrganizationId ! = originalGroup . OrganizationId ) ;
if ( invalidCollection ! = default )
{
// Use generic error message to avoid enumeration
throw new NotFoundException ( ) ;
}
}
private async Task ValidateMemberAccessAsync ( Group originalGroup ,
ICollection < Guid > memberAccess )
{
var members = await _organizationUserRepository . GetManyAsync ( memberAccess ) ;
var memberIds = members . Select ( g = > g . Id ) ;
var missingMemberId = memberAccess . FirstOrDefault ( mId = > ! memberIds . Contains ( mId ) ) ;
if ( missingMemberId ! = default )
{
throw new NotFoundException ( ) ;
}
var invalidMember = members . FirstOrDefault ( m = > m . OrganizationId ! = originalGroup . OrganizationId ) ;
if ( invalidMember ! = default )
{
// Use generic error message to avoid enumeration
throw new NotFoundException ( ) ;
}
}
2022-12-12 09:59:48 +00:00
}