Пример #1
0
 private void CheckUsernameFormat(string username, string?paramName)
 {
     if (!_usernameValidator.Validate(username, out var message))
     {
         throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resource.ExceptionUsernameBadFormat, message), paramName);
     }
 }
Пример #2
0
        public async Task DeleteUserAsync(string username)
        {
            if (username == null)
            {
                throw new ArgumentNullException(nameof(username));
            }

            if (!_usernameValidator.Validate(username, out var message))
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resource.ExceptionUsernameBadFormat, message), nameof(username));
            }

            var user = await _databaseContext.Users.Where(u => u.Username == username).SingleOrDefaultAsync();

            if (user is null)
            {
                throw new EntityNotExistException(EntityTypes.User, new Dictionary <string, object>
                {
                    ["username"] = username
                });
            }

            if (user.Id == 1)
            {
                throw new InvalidOperationOnRootUserException(Resource.ExceptionDeleteRootUser);
            }

            await _timelinePostService.DeleteAllPostsOfUserAsync(user.Id);

            _databaseContext.Users.Remove(user);

            await _databaseContext.SaveChangesAsync();

            _logger.LogWarning(Resource.LogDeleteUser, user.Username, user.Id);
        }
Пример #3
0
        public void UserNameValidate_Should_Return_CanOnlyCertainCharacters(string userName)
        {
            Mock <ISettingManager>  mockSettingManager  = new Mock <ISettingManager>();
            Mock <IResourceManager> mockResourceManager = new Mock <IResourceManager>();
            Mock <ISettingContainer <MembershipSetting> > mockMembershipSettingContainer = new Mock <ISettingContainer <MembershipSetting> >();

            mockMembershipSettingContainer.SetupGet(m => m.Default)
            .Returns(new MembershipSetting()
            {
                AllowOnlyAlphanumericUserNames = false,
            });
            mockSettingManager.Setup(m => m.GetContainer <MembershipSetting>()).Returns(mockMembershipSettingContainer.Object);
            mockResourceManager.Setup(m => m.GetString(It.IsAny <string>())).Returns <string>(x => x);

            UsernameValidator validator = new UsernameValidator(mockSettingManager.Object, mockResourceManager.Object);

            Assert.False(validator.Validate(userName).Succeeded);
            Assert.Equal(validator.Validate(userName).Errors.First(), "InvalidUsername");
        }
Пример #4
0
 protected override (bool, string) DoValidate(string value)
 {
     if (value.StartsWith('@'))
     {
         return(_usernameValidator.Validate(value.Substring(1)));
     }
     else
     {
         return(_timelineNameValidator.Validate(value));
     }
 }
Пример #5
0
        public void UserNameValidate_Should_Return_Valid(string userName)
        {
            Mock <ISettingManager>  mockSettingManager  = new Mock <ISettingManager>();
            Mock <IResourceManager> mockResourceManager = new Mock <IResourceManager>();
            Mock <ISettingContainer <MembershipSetting> > mockMembershipSettingContainer = new Mock <ISettingContainer <MembershipSetting> >();

            mockMembershipSettingContainer.SetupGet(m => m.Default)
            .Returns(new MembershipSetting()
            {
                AllowOnlyAlphanumericUserNames = false
            });
            mockSettingManager.Setup(m => m.GetContainer <MembershipSetting>()).Returns(mockMembershipSettingContainer.Object);
            mockResourceManager.Setup(m => m.GetString(It.IsAny <string>())).Returns(string.Empty);

            UsernameValidator validator = new UsernameValidator(mockSettingManager.Object, mockResourceManager.Object);

            Assert.True(validator.Validate(userName).Succeeded);
        }
Пример #6
0
        public void Login(string username, string password)
        {
            //Contract.Requires(username.Contains("b"), "aaaa");
            IValidator usernameValidator = new UsernameValidator();
            IValidator passwordValidator = new PasswordValidator();


            try
            {
                if (usernameValidator.Validate(username) && passwordValidator.Validate(password))
                {
                    Customer  customer = _customerManager.Login(txtUsername.Text, txtPassword.Text);
                    IMainForm mainForm = MainForm.GetSingletonInstance();
                    this.Hide();
                    mainForm.Start(customer, this);
                }
            }
            catch (CustomerCouldNotFindException)
            {
                MessageBox.Show("Customer could not find!");
                txtUsername.Text = "";
                txtPassword.Text = "";
            }
            catch (UsernameValidationException ex)
            {
                MessageBox.Show(ex.Message_);
            }
            catch (PasswordValidationException)
            {
                MessageBox.Show("Invalid password!");
            }
            catch (WrongPasswordException)
            {
                MessageBox.Show("Wrong password! Please enter correct password.");
                txtPassword.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Пример #7
0
        public async Task <long> GetUserIdByUsernameAsync(string username)
        {
            if (username == null)
            {
                throw new ArgumentNullException(nameof(username));
            }

            if (!_usernameValidator.Validate(username, out var message))
            {
                throw new ArgumentException(message);
            }

            var entity = await _database.Users.Where(user => user.Username == username).Select(u => new { u.Id }).SingleOrDefaultAsync();

            if (entity == null)
            {
                throw CreateUserNotExistException(username);
            }

            return(entity.Id);
        }