Пример #1
0
        public async Task <List <string> > EditSharedAccountAsync(SharedAccountEditModel sharedAccountModel)
        {
            if (sharedAccountModel == null)
            {
                throw new ArgumentNullException(nameof(sharedAccountModel));
            }

            _dataProtectionService.Validate();
            await ValidateAccountNameAndLoginAsync(sharedAccountModel.Name, sharedAccountModel.GetLogin(), sharedAccountModel.Id);

            sharedAccountModel.Urls = Validation.VerifyUrls(sharedAccountModel.Urls);

            var sharedAccount = await GetSharedAccountByIdAsync(sharedAccountModel.Id);

            if (sharedAccount == null)
            {
                throw new HESException(HESCode.SharedAccountNotFound);
            }

            sharedAccount = sharedAccountModel.SetNewValue(sharedAccount);

            // 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.Name      = sharedAccount.Name;
                account.Urls      = sharedAccount.Urls;
                account.Apps      = sharedAccount.Apps;
                account.Login     = sharedAccount.Login;
                account.UpdatedAt = DateTime.UtcNow;

                foreach (var hardwareVault in account.Employee.HardwareVaults)
                {
                    tasks.Add(_hardwareVaultTaskService.GetAccountUpdateTask(hardwareVault.Id, account.Id));
                }
            }

            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                await _sharedAccountRepository.UpdateOnlyPropAsync(sharedAccount, new string[] { nameof(SharedAccount.Name), nameof(SharedAccount.Urls), nameof(SharedAccount.Apps), nameof(SharedAccount.Login) });

                await _accountService.UpdateOnlyPropAsync(accounts, new string[] { nameof(Account.Name), nameof(Account.Urls), nameof(Account.Apps), nameof(Account.Login), nameof(Account.UpdatedAt) });

                await _hardwareVaultTaskService.AddRangeTasksAsync(tasks);

                transactionScope.Complete();
            }

            return(accounts.SelectMany(x => x.Employee.HardwareVaults.Select(s => s.Id)).ToList());
        }
Пример #2
0
        public async Task AddHardwareVaultAsync(string employeeId, string vaultId)
        {
            if (employeeId == null)
            {
                throw new ArgumentNullException(nameof(employeeId));
            }

            if (vaultId == null)
            {
                throw new ArgumentNullException(nameof(vaultId));
            }

            _dataProtectionService.Validate();

            var employee = await GetEmployeeByIdAsync(employeeId);

            if (employee == null)
            {
                throw new Exception("Employee not found");
            }

            if (employee.HardwareVaults.Count > 0)
            {
                throw new Exception("Cannot add more than one hardware vault.");
            }

            var vault = await _hardwareVaultService.GetVaultByIdAsync(vaultId);

            if (vault == null)
            {
                throw new Exception($"Vault {vault} not found");
            }

            if (vault.Status != VaultStatus.Ready)
            {
                throw new Exception($"Vault {vaultId} in a status that does not allow to reserve.");
            }

            vault.EmployeeId      = employeeId;
            vault.Status          = VaultStatus.Reserved;
            vault.IsStatusApplied = false;
            vault.MasterPassword  = _dataProtectionService.Encrypt(GenerateMasterPassword());

            var accounts = await GetAccountsByEmployeeIdAsync(employeeId);

            var tasks = new List <HardwareVaultTask>();

            // Create a task for accounts that were created without a vault
            foreach (var account in accounts.Where(x => x.Password != null))
            {
                tasks.Add(_hardwareVaultTaskService.GetAccountCreateTask(vault.Id, account.Id, account.Password, account.OtpSecret));
            }

            if (tasks.Count > 0)
            {
                vault.NeedSync = true;
            }

            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                await _hardwareVaultService.UpdateVaultAsync(vault);

                await _hardwareVaultService.CreateVaultActivationAsync(vaultId);

                if (tasks.Count > 0)
                {
                    await _hardwareVaultTaskService.AddRangeTasksAsync(tasks);
                }

                transactionScope.Complete();
            }
        }