2024-04-18 11:42:30 +01:00
#nullable enable
using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Interfaces ;
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.OrganizationFeatures.OrganizationSubscriptions.Interface ;
using Bit.Core.Repositories ;
using Bit.Core.Services ;
namespace Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers ;
public class UpdateOrganizationUserCommand : IUpdateOrganizationUserCommand
{
private readonly IEventService _eventService ;
private readonly IOrganizationService _organizationService ;
private readonly IOrganizationRepository _organizationRepository ;
private readonly IOrganizationUserRepository _organizationUserRepository ;
private readonly ICountNewSmSeatsRequiredQuery _countNewSmSeatsRequiredQuery ;
private readonly IUpdateSecretsManagerSubscriptionCommand _updateSecretsManagerSubscriptionCommand ;
public UpdateOrganizationUserCommand (
IEventService eventService ,
IOrganizationService organizationService ,
IOrganizationRepository organizationRepository ,
IOrganizationUserRepository organizationUserRepository ,
ICountNewSmSeatsRequiredQuery countNewSmSeatsRequiredQuery ,
IUpdateSecretsManagerSubscriptionCommand updateSecretsManagerSubscriptionCommand )
{
_eventService = eventService ;
_organizationService = organizationService ;
_organizationRepository = organizationRepository ;
_organizationUserRepository = organizationUserRepository ;
_countNewSmSeatsRequiredQuery = countNewSmSeatsRequiredQuery ;
_updateSecretsManagerSubscriptionCommand = updateSecretsManagerSubscriptionCommand ;
}
public async Task UpdateUserAsync ( OrganizationUser user , Guid ? savingUserId ,
[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
List < CollectionAccessSelection > ? collections , IEnumerable < Guid > ? groups )
2024-04-18 11:42:30 +01:00
{
if ( user . Id . Equals ( default ( Guid ) ) )
{
throw new BadRequestException ( "Invite the user first." ) ;
}
var originalUser = await _organizationUserRepository . GetByIdAsync ( user . Id ) ;
if ( savingUserId . HasValue )
{
await _organizationService . ValidateOrganizationUserUpdatePermissions ( user . OrganizationId , user . Type , originalUser . Type , user . GetPermissions ( ) ) ;
}
await _organizationService . ValidateOrganizationCustomPermissionsEnabledAsync ( user . OrganizationId , user . Type ) ;
if ( user . Type ! = OrganizationUserType . Owner & &
! await _organizationService . HasConfirmedOwnersExceptAsync ( user . OrganizationId , new [ ] { user . Id } ) )
{
throw new BadRequestException ( "Organization must have at least one confirmed owner." ) ;
}
[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 ( user . AccessAll )
2024-04-18 11:42:30 +01:00
{
throw new BadRequestException ( "The AccessAll property has been deprecated by collection enhancements. Assign the user to collections instead." ) ;
}
[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 ( collections ? . Count > 0 )
2024-04-18 11:42:30 +01:00
{
var invalidAssociations = collections . Where ( cas = > cas . Manage & & ( cas . ReadOnly | | cas . HidePasswords ) ) ;
if ( invalidAssociations . Any ( ) )
{
throw new BadRequestException ( "The Manage property is mutually exclusive and cannot be true while the ReadOnly or HidePasswords properties are also true." ) ;
}
}
// Only autoscale (if required) after all validation has passed so that we know it's a valid request before
// updating Stripe
if ( ! originalUser . AccessSecretsManager & & user . AccessSecretsManager )
{
var additionalSmSeatsRequired = await _countNewSmSeatsRequiredQuery . CountNewSmSeatsRequiredAsync ( user . OrganizationId , 1 ) ;
if ( additionalSmSeatsRequired > 0 )
{
[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
var organization = await _organizationRepository . GetByIdAsync ( user . OrganizationId ) ;
2024-04-18 11:42:30 +01:00
var update = new SecretsManagerSubscriptionUpdate ( organization , true )
. AdjustSeats ( additionalSmSeatsRequired ) ;
await _updateSecretsManagerSubscriptionCommand . UpdateSubscriptionAsync ( update ) ;
}
}
await _organizationUserRepository . ReplaceAsync ( user , collections ) ;
if ( groups ! = null )
{
await _organizationUserRepository . UpdateGroupsAsync ( user . Id , groups ) ;
}
await _eventService . LogOrganizationUserEventAsync ( user , EventType . OrganizationUser_Updated ) ;
}
}