예제 #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 EditPersonalAccountAsync(AccountEditModel personalAccount)
        {
            if (personalAccount == null)
            {
                throw new ArgumentNullException(nameof(personalAccount));
            }

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

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

            var employee = await GetEmployeeByIdAsync(personalAccount.EmployeeId);

            if (employee == null)
            {
                throw new HESException(HESCode.EmployeeNotFound);
            }

            var account = await _accountService.GetAccountByIdAsync(personalAccount.Id);

            if (account == null)
            {
                throw new HESException(HESCode.AccountNotFound);
            }

            account = personalAccount.SetNewValue(account);

            // Create tasks if there are vaults
            List <HardwareVaultTask> tasks = new List <HardwareVaultTask>();

            foreach (var vault in employee.HardwareVaults)
            {
                tasks.Add(_hardwareVaultTaskService.GetAccountUpdateTask(vault.Id, personalAccount.Id));
            }

            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                await _accountService.UpdateOnlyPropAsync(account, new string[] { nameof(Account.Name), nameof(Account.Login), nameof(Account.Urls), nameof(Account.Apps), nameof(Account.UpdatedAt) });

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

                    await _hardwareVaultService.UpdateNeedSyncAsync(employee.HardwareVaults, true);
                }

                transactionScope.Complete();
            }
        }