Example #1
0
        public async Task <ActionResult <SharedAccount> > GetSharedAccountById(string id)
        {
            var sharedAccount = await _sharedAccountService.GetSharedAccountByIdAsync(id);

            if (sharedAccount == null)
            {
                return(NotFound());
            }

            return(sharedAccount);
        }
Example #2
0
        public async Task <Account> AddSharedAccountAsync(string employeeId, string sharedAccountId)
        {
            if (employeeId == null)
            {
                throw new ArgumentNullException(nameof(employeeId));
            }

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

            _dataProtectionService.Validate();

            var sharedAccount = await _sharedAccountService.GetSharedAccountByIdAsync(sharedAccountId);

            if (sharedAccount == null)
            {
                throw new Exception("Shared Account not found");
            }

            var exist = await _accountService.ExistAsync(x => x.EmployeeId == employeeId &&
                                                         x.Name == sharedAccount.Name &&
                                                         x.Login == sharedAccount.Login &&
                                                         x.Deleted == false);

            if (exist)
            {
                throw new Exception("An account with the same name and login exists");
            }

            var account = new Account
            {
                Id                = Guid.NewGuid().ToString(),
                Name              = sharedAccount.Name,
                Urls              = sharedAccount.Urls,
                Apps              = sharedAccount.Apps,
                Login             = sharedAccount.Login,
                AccountType       = AccountType.Shared,
                LoginType         = sharedAccount.LoginType,
                CreatedAt         = DateTime.UtcNow,
                PasswordUpdatedAt = DateTime.UtcNow,
                OtpUpdatedAt      = sharedAccount.OtpSecret != null ? new DateTime?(DateTime.UtcNow) : null,
                EmployeeId        = employeeId,
                SharedAccountId   = sharedAccountId,
                Password          = sharedAccount.Password,
                OtpSecret         = sharedAccount.OtpSecret,
                StorageId         = new StorageId().Data
            };

            var employee = await GetEmployeeByIdAsync(employeeId);

            var tasks = new List <HardwareVaultTask>();

            foreach (var vault in employee.HardwareVaults)
            {
                tasks.Add(_hardwareVaultTaskService.GetAccountCreateTask(vault.Id, account.Id, sharedAccount.Password, sharedAccount.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);
        }