Esempio n. 1
0
        private void btnSubmitGroup2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string newPswd      = ValidateUser.ValidatePassword(pbNewPswd.Password);
                string newPswdAgain = pbNewPswdAgain.Password;

                if (newPswd == newPswdAgain)
                {
                    Authentification.ChangePassword(_userId, newPswd);
                    pbNewPswd.Password      = string.Empty;
                    pbNewPswdAgain.Password = string.Empty;
                    DialogHelper.ShowInfo("Heslo bylo úspěšně změněno.");
                    InitializeInterface();
                }
                else
                {
                    pbNewPswdAgain.Password = string.Empty;
                    throw new PasswordsDoNotMatchException();
                }
            }
            catch (InvalidAuthPasswordException ex)
            {
                pbNewPswd.Password = string.Empty; pbNewPswd.Focus();
                DialogHelper.ShowWarning(ex.Message);
            }
            catch (PasswordsDoNotMatchException ex)
            {
                DialogHelper.ShowWarning(ex.Message);
            }
            catch
            {
                DialogHelper.ShowError("Heslo nemohlo být změněno.");
            }
        }
Esempio n. 2
0
        public async Task <User> CreateAsync(User user, string password)
        {
            // validation
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new AppException("Password is required");
            }
            else if (ValidateUser.ValidatePassword(user.Password) == false)
            {
                throw new AppException(
                          "Password have length in range 8-15 character and have at least 1 uppercase, 1 lowercase, 1 digit");
            }

            if (_context.Users.Any(x => x.Username == user.Username))
            {
                throw new AppException("Username \"" + user.Username + "\" is already taken");
            }

            if (user.Email == null)
            {
                throw new AppException("Email is required");
            }
            else if (_context.Users.Any(x => x.Email == user.Email))
            {
                throw new AppException("Email \"" + user.Email + "\" is already taken");
            }
            else if (ValidateUser.IsValidEmail(user.Email) == false)
            {
                throw new AppException("Email is not in right format");
            }

            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(password, out passwordHash, out passwordSalt);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            _context.Users.Add(user);


            bool addCartResut = await _cartService.AddCart(new Cart()
            {
                UserId = user.Id
            });

            if (!addCartResut)
            {
                return(null);
            }


            _context.SaveChanges();

            return(user);
        }
Esempio n. 3
0
        public void Update(User userParam, string password = null)
        {
            var user = _context.Users.Find(userParam.Id);

            if (user == null)
            {
                throw new AppException("User not found");
            }

            // update username if it has changed
            if (!string.IsNullOrWhiteSpace(userParam.Username) && userParam.Username != user.Username)
            {
                // throw error if the new username is already taken
                if (_context.Users.Any(x => x.Username == userParam.Username))
                {
                    throw new AppException("Username " + userParam.Username + " is already taken");
                }

                user.Username = userParam.Username;
            }

            // update user properties if provided
            if (!string.IsNullOrWhiteSpace(userParam.Name))
            {
                user.Name = userParam.Name;
            }

            // update password if provided
            if (!string.IsNullOrWhiteSpace(password))
            {
                byte[] passwordHash, passwordSalt;

                if (ValidateUser.ValidatePassword(user.Password) == false)
                {
                    throw new AppException(
                              "Password have length in range 8-15 character and have at least 1 uppercase, 1 lowercase, 1 digit");
                }

                CreatePasswordHash(password, out passwordHash, out passwordSalt);

                user.PasswordHash = passwordHash;
                user.PasswordSalt = passwordSalt;
            }

            user.Role    = userParam.Role;
            user.Phone   = userParam.Phone;
            user.Address = userParam.Address;

            _context.Users.Update(user);
            _context.SaveChanges();
        }
Esempio n. 4
0
        private void pswdSubmit_Click(object sender, RoutedEventArgs e)
        {
            // Zkontrolovat původní heslo
            bool isUserAuthentificated = false;

            try
            {
                string enteredPswd = ValidateUser.ValidatePassword(pbFormerPswd.Password);
                if (Authentification.CheckUserPassword(enteredPswd))
                {
                    // Heslo ověřeno, pokračujeme dále --> kontrola nového hesla
                    isUserAuthentificated = true;
                }
                else
                {
                    DialogHelper.ShowWarning("Původní heslo nebylo zadáno správně.");
                    pbFormerPswd.Password = string.Empty;
                }
            }
            catch (UserNotLoggedInException ex)
            {
                DialogHelper.ShowError(ex.Message);
            }
            catch (InvalidAuthPasswordException ex)
            {
                DialogHelper.ShowWarning(ex.Message);
            }
            catch
            {
                DialogHelper.ShowError("Uživatel nemohl být ověřen.");
            }

            // Validace nového hesla
            if (isUserAuthentificated)
            {
                try
                {
                    string newPswd      = ValidateUser.ValidateNewPassword(pbNewPswd.Password);
                    string newPswdAgain = pbNewPswdAgain.Password;
                    if (newPswd == newPswdAgain)
                    {
                        Authentification.ChangePassword(Authentification.AuthUser.Id, newPswd);
                        DialogHelper.ShowInfo("Heslo bylo úspěšně změněno.");
                        InitializeInterface();
                    }
                    else
                    {
                        throw new PasswordsDoNotMatchException();
                    }
                }
                catch (InvalidNewPasswordException ex)
                {
                    DialogHelper.ShowWarning(ex.Message);
                }
                catch (PasswordsDoNotMatchException ex)
                {
                    DialogHelper.ShowWarning(ex.Message);
                }
                catch
                {
                    DialogHelper.ShowError("Heslo nemohlo být změněno.");
                }
            }
        }