Пример #1
0
        public async Task <bool> AddBricksetPrimaryUser(string username, string password)
        {
            if (string.IsNullOrWhiteSpace(username) ||
                string.IsNullOrWhiteSpace(password) ||
                !await _secureStorageService.IsBricksetApiKeyAcquired().ConfigureAwait(false) ||
                await _secureStorageService.IsBricksetPrimaryUsersDefined().ConfigureAwait(false) ||
                _bricksetUserRepository.Exists(username))
            {
                return(false);
            }

            var bricksetUserHash = await _bricksetApiService.Login(new ParameterLogin
            {
                Username = username,
                Password = password,
                ApiKey   = await _secureStorageService.GetBricksetApiKey().ConfigureAwait(false)
            }).ConfigureAwait(false);

            if (string.IsNullOrWhiteSpace(bricksetUserHash))
            {
                return(false);
            }

            await _secureStorageService.SaveBricksetPrimaryUser(username, bricksetUserHash).ConfigureAwait(false);

            _bricksetUserRepository.Add(BricksetUserType.Primary, username);

            await _userSynchronizationService.SynchronizeBricksetPrimaryUsersSets(username).ConfigureAwait(false);

            return(true);
        }
        public async Task SynchronizeBricksetPrimaryUsersSets(string username = null)
        {
            _messageHub.Publish(new UserSynchronizationServiceStart {
                UserType = BricksetUserType.Primary
            });

            try
            {
                var apiKey = await _onboardingService.GetBricksetApiKey();

                if (string.IsNullOrWhiteSpace(apiKey))
                {
                    return;
                }

                if (string.IsNullOrWhiteSpace(username))
                {
                    var tasks = new List <Task>();

                    _bricksetUserRepository
                    .GetAllUsernames(BricksetUserType.Primary)
                    .ToList()
                    .ForEach(bricksetUsername => tasks.Add(SynchronizeBricksetPrimaryUser(apiKey, bricksetUsername)));

                    _messageHub.Publish(new UsersAcquired {
                        UserType = BricksetUserType.Primary, Count = tasks.Count
                    });

                    await Task.WhenAll(tasks);
                }
                else if (_bricksetUserRepository.Exists(username))
                {
                    _messageHub.Publish(new UsersAcquired {
                        UserType = BricksetUserType.Primary, Count = 1
                    });

                    await SynchronizeBricksetPrimaryUser(apiKey, username);
                }
                else
                {
                    throw new ArgumentException("Parameter was not found the list of primary users", nameof(username));
                }
            }
            catch (AggregateException aggEx)
            {
                _messageHub.Publish(new UserSynchronizationServiceException {
                    UserType = BricksetUserType.Primary, Exceptions = aggEx.InnerExceptions
                });
            }
            catch (Exception ex)
            {
                _messageHub.Publish(new UserSynchronizationServiceException {
                    UserType = BricksetUserType.Primary, Exceptions = new[] { ex }
                });
            }

            _messageHub.Publish(new UserSynchronizationServiceEnd {
                UserType = BricksetUserType.Primary
            });
        }
Пример #3
0
        public void Exists_InvalidUsername_ReturnsFalse(string username)
        {
            var bricksetUserExists = _bricksetUserRepository.Exists(username);

            Check.That(bricksetUserExists).IsFalse();
        }