public async Task <List <string> > EditSharedAccountPwdAsync(SharedAccount sharedAccount, AccountPassword accountPassword) { if (sharedAccount == null) { throw new ArgumentNullException(nameof(sharedAccount)); } if (accountPassword == null) { throw new ArgumentNullException(nameof(accountPassword)); } _dataProtectionService.Validate(); // Update Shared Account sharedAccount.Password = _dataProtectionService.Encrypt(accountPassword.Password); sharedAccount.PasswordChangedAt = DateTime.UtcNow; // Get all accounts where used this shared account var accounts = await _accountService .Query() .Include(x => x.Employee.HardwareVaults) .Where(x => x.SharedAccountId == sharedAccount.Id && x.Deleted == false) .AsNoTracking() .ToListAsync(); List <HardwareVaultTask> tasks = new List <HardwareVaultTask>(); foreach (var account in accounts) { account.UpdatedAt = DateTime.UtcNow; account.PasswordUpdatedAt = DateTime.UtcNow; foreach (var hardwareVault in account.Employee.HardwareVaults) { tasks.Add(_hardwareVaultTaskService.GetAccountPwdUpdateTask(hardwareVault.Id, account.Id, sharedAccount.Password)); } } using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await _sharedAccountRepository.UpdateOnlyPropAsync(sharedAccount, new string[] { nameof(SharedAccount.Password), nameof(SharedAccount.PasswordChangedAt) }); await _accountService.UpdateOnlyPropAsync(accounts, new string[] { nameof(Account.UpdatedAt), nameof(Account.PasswordUpdatedAt) }); await _hardwareVaultTaskService.AddRangeTasksAsync(tasks); transactionScope.Complete(); } return(accounts.SelectMany(x => x.Employee.HardwareVaults.Select(s => s.Id)).ToList()); }
public async Task EditPersonalAccountPwdAsync(Account account, AccountPassword accountPassword) { if (account == null) { throw new ArgumentNullException(nameof(account)); } if (accountPassword == null) { throw new ArgumentNullException(nameof(accountPassword)); } _dataProtectionService.Validate(); var employee = await GetEmployeeByIdAsync(account.EmployeeId); account.UpdatedAt = DateTime.UtcNow; account.PasswordUpdatedAt = DateTime.UtcNow; // Update password field if there are no vaults if (employee.HardwareVaults.Count == 0) { account.Password = _dataProtectionService.Encrypt(accountPassword.Password); } // Create tasks if there are vaults List <HardwareVaultTask> tasks = new List <HardwareVaultTask>(); foreach (var vault in employee.HardwareVaults) { tasks.Add(_hardwareVaultTaskService.GetAccountPwdUpdateTask(vault.Id, account.Id, _dataProtectionService.Encrypt(accountPassword.Password))); } using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await _accountService.UpdateOnlyPropAsync(account, new string[] { nameof(Account.UpdatedAt), nameof(Account.PasswordUpdatedAt), nameof(Account.Password) }); if (tasks.Count > 0) { await _hardwareVaultTaskService.AddRangeTasksAsync(tasks); await _hardwareVaultService.UpdateNeedSyncAsync(employee.HardwareVaults, true); } transactionScope.Complete(); } }