diff --git a/src/Api/Controllers/EmergencyAccessController.cs b/src/Api/Controllers/EmergencyAccessController.cs index 2f659747ef..ebe2e59f03 100644 --- a/src/Api/Controllers/EmergencyAccessController.cs +++ b/src/Api/Controllers/EmergencyAccessController.cs @@ -87,14 +87,14 @@ namespace Bit.Api.Controllers public async Task Invite([FromBody] EmergencyAccessInviteRequestModel model) { var user = await _userService.GetUserByPrincipalAsync(User); - await _emergencyAccessService.InviteAsync(user, user.Name, model.Email, model.Type.Value, model.WaitTimeDays); + await _emergencyAccessService.InviteAsync(user, model.Email, model.Type.Value, model.WaitTimeDays); } [HttpPost("{id}/reinvite")] public async Task Reinvite(string id) { var user = await _userService.GetUserByPrincipalAsync(User); - await _emergencyAccessService.ResendInviteAsync(user.Id, new Guid(id), user.Name); + await _emergencyAccessService.ResendInviteAsync(user, new Guid(id)); } [HttpPost("{id}/accept")] diff --git a/src/Core/Services/IEmergencyAccessService.cs b/src/Core/Services/IEmergencyAccessService.cs index 1cc1d01709..b156d518c4 100644 --- a/src/Core/Services/IEmergencyAccessService.cs +++ b/src/Core/Services/IEmergencyAccessService.cs @@ -9,8 +9,8 @@ namespace Bit.Core.Services { public interface IEmergencyAccessService { - Task InviteAsync(User invitingUser, string invitingUsersName, string email, EmergencyAccessType type, int waitTime); - Task ResendInviteAsync(Guid invitingUserId, Guid emergencyAccessId, string invitingUsersName); + Task InviteAsync(User invitingUser, string email, EmergencyAccessType type, int waitTime); + Task ResendInviteAsync(User invitingUser, Guid emergencyAccessId); Task AcceptUserAsync(Guid emergencyAccessId, User user, string token, IUserService userService); Task DeleteAsync(Guid emergencyAccessId, Guid grantorId); Task ConfirmUserAsync(Guid emergencyAccessId, string key, Guid grantorId); diff --git a/src/Core/Services/Implementations/EmergencyAccessService.cs b/src/Core/Services/Implementations/EmergencyAccessService.cs index 97f7bcd492..6721603631 100644 --- a/src/Core/Services/Implementations/EmergencyAccessService.cs +++ b/src/Core/Services/Implementations/EmergencyAccessService.cs @@ -45,7 +45,7 @@ namespace Bit.Core.Services _globalSettings = globalSettings; } - public async Task InviteAsync(User invitingUser, string invitingUsersName, string email, EmergencyAccessType type, int waitTime) + public async Task InviteAsync(User invitingUser, string email, EmergencyAccessType type, int waitTime) { if (! await _userService.CanAccessPremium(invitingUser)) { @@ -64,7 +64,7 @@ namespace Bit.Core.Services }; await _emergencyAccessRepository.CreateAsync(emergencyAccess); - await SendInviteAsync(emergencyAccess, invitingUsersName); + await SendInviteAsync(emergencyAccess, NameOrEmail(invitingUser)); return emergencyAccess; } @@ -80,16 +80,16 @@ namespace Bit.Core.Services return emergencyAccess; } - public async Task ResendInviteAsync(Guid invitingUserId, Guid emergencyAccessId, string invitingUsersName) + public async Task ResendInviteAsync(User invitingUser, Guid emergencyAccessId) { var emergencyAccess = await _emergencyAccessRepository.GetByIdAsync(emergencyAccessId); - if (emergencyAccess == null || emergencyAccess.GrantorId != invitingUserId || + if (emergencyAccess == null || emergencyAccess.GrantorId != invitingUser.Id || emergencyAccess.Status != EmergencyAccessStatusType.Invited) { throw new BadRequestException("Emergency Access not valid."); } - await SendInviteAsync(emergencyAccess, invitingUsersName); + await SendInviteAsync(emergencyAccess, NameOrEmail(invitingUser)); } public async Task AcceptUserAsync(Guid emergencyAccessId, User user, string token, IUserService userService) @@ -157,7 +157,7 @@ namespace Bit.Core.Services emergencyAccess.KeyEncrypted = key; emergencyAccess.Email = null; await _emergencyAccessRepository.ReplaceAsync(emergencyAccess); - await _mailService.SendEmergencyAccessConfirmedEmailAsync(grantor.Name, grantee.Email); + await _mailService.SendEmergencyAccessConfirmedEmailAsync(NameOrEmail(grantor), grantee.Email); return emergencyAccess; } @@ -191,7 +191,7 @@ namespace Bit.Core.Services var grantor = await _userRepository.GetByIdAsync(emergencyAccess.GrantorId); - await _mailService.SendEmergencyAccessRecoveryInitiated(emergencyAccess, initiatingUser.Name, grantor.Email); + await _mailService.SendEmergencyAccessRecoveryInitiated(emergencyAccess, NameOrEmail(initiatingUser), grantor.Email); } public async Task ApproveAsync(Guid id, User approvingUser) @@ -208,7 +208,7 @@ namespace Bit.Core.Services await _emergencyAccessRepository.ReplaceAsync(emergencyAccess); var grantee = await _userRepository.GetByIdAsync(emergencyAccess.GranteeId.Value); - await _mailService.SendEmergencyAccessRecoveryApproved(emergencyAccess, approvingUser.Name, grantee.Email); + await _mailService.SendEmergencyAccessRecoveryApproved(emergencyAccess, NameOrEmail(approvingUser), grantee.Email); } public async Task RejectAsync(Guid id, User rejectingUser) @@ -226,7 +226,7 @@ namespace Bit.Core.Services await _emergencyAccessRepository.ReplaceAsync(emergencyAccess); var grantee = await _userRepository.GetByIdAsync(emergencyAccess.GranteeId.Value); - await _mailService.SendEmergencyAccessRecoveryRejected(emergencyAccess, rejectingUser.Name, grantee.Email); + await _mailService.SendEmergencyAccessRecoveryRejected(emergencyAccess, NameOrEmail(rejectingUser), grantee.Email); } public async Task<(EmergencyAccess, User)> TakeoverAsync(Guid id, User requestingUser) @@ -313,5 +313,10 @@ namespace Bit.Core.Services var token = _dataProtector.Protect($"EmergencyAccessInvite {emergencyAccess.Id} {emergencyAccess.Email} {nowMillis}"); await _mailService.SendEmergencyAccessInviteEmailAsync(emergencyAccess, invitingUsersName, token); } + + private string NameOrEmail(User user) + { + return string.IsNullOrWhiteSpace(user.Name) ? user.Email : user.Name; + } } }