public async Task <Account> CreatePersonalAccountAsync(AccountAddModel personalAccount) { if (personalAccount == null) { throw new ArgumentNullException(nameof(personalAccount)); } _dataProtectionService.Validate(); var account = new Account() { Id = Guid.NewGuid().ToString(), Name = personalAccount.Name, Urls = Validation.VerifyUrls(personalAccount.Urls), Apps = personalAccount.Apps, Login = await ValidateAccountNameAndLoginAsync(personalAccount.EmployeeId, personalAccount.Name, personalAccount.GetLogin()), AccountType = AccountType.Personal, LoginType = personalAccount.LoginType, CreatedAt = DateTime.UtcNow, PasswordUpdatedAt = DateTime.UtcNow, OtpUpdatedAt = Validation.VerifyOtpSecret(personalAccount.OtpSecret) != null ? new DateTime?(DateTime.UtcNow) : null, Password = _dataProtectionService.Encrypt(personalAccount.Password), OtpSecret = _dataProtectionService.Encrypt(personalAccount.OtpSecret), UpdateInActiveDirectory = personalAccount.UpdateInActiveDirectory, EmployeeId = personalAccount.EmployeeId, StorageId = new StorageId().Data }; Employee employee = await GetEmployeeByIdAsync(personalAccount.EmployeeId); List <HardwareVaultTask> tasks = new List <HardwareVaultTask>(); foreach (var vault in employee.HardwareVaults) { tasks.Add(_hardwareVaultTaskService.GetAccountCreateTask(vault.Id, account.Id, account.Password, account.OtpSecret)); } using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await _accountService.AddAsync(account); await SetAsPrimaryAccountIfEmptyAsync(account.EmployeeId, account.Id); if (tasks.Count > 0) { await _hardwareVaultTaskService.AddRangeTasksAsync(tasks); await _hardwareVaultService.UpdateNeedSyncAsync(employee.HardwareVaults, true); } transactionScope.Complete(); } return(account); }
public async Task ExecuteRemoteTasks(string vaultId, Device remoteDevice, bool primaryAccountOnly) { _dataProtectionService.Validate(); var vault = await _hardwareVaultService.GetVaultByIdAsync(vaultId); if (vault == null) { throw new HESException(HESCode.HardwareVaultNotFound); } // Tasks query var query = _hardwareVaultTaskService .TaskQuery() .Include(t => t.HardwareVault) .Include(t => t.Account) .Where(t => t.HardwareVaultId == vaultId); if (primaryAccountOnly) { query = query.Where(x => x.AccountId == vault.Employee.PrimaryAccountId || x.Operation == TaskOperation.Primary); } query = query.OrderBy(x => x.CreatedAt).AsNoTracking(); var tasks = await query.ToListAsync(); while (tasks.Any()) { foreach (var task in tasks) { task.Password = _dataProtectionService.Decrypt(task.Password); task.OtpSecret = _dataProtectionService.Decrypt(task.OtpSecret); await ExecuteRemoteTask(remoteDevice, task); await TaskCompleted(task.Id); } tasks = await query.ToListAsync(); } await _hardwareVaultService.UpdateNeedSyncAsync(vault, false); await _synchronizationService.HardwareVaultStateChanged(vault.Id); }