private void btnSave_Click(object sender, EventArgs e)
        {
            if (IsPhoneNumber(phoneNumberTxt.Text)) //validate the phone number
            {
                try
                {
                    user.Details.FirstName    = forenameTxt.Text;
                    user.Details.MiddleNames  = middleNameTxt.Text;
                    user.Details.LastName     = surnameTxt.Text;
                    user.Details.Address      = addressTxt.Text;
                    user.Details.PhoneNumber  = phoneNumberTxt.Text;
                    user.Details.EmailAddress = emailTxt.Text != String.Empty ? new MailAddress(emailTxt.Text).ToString() : String.Empty;

                    user.GuineaPigData.Age         = Convert.ToInt32(ageTxt.Text);
                    user.GuineaPigData.Name        = guineaPigNameTxt.Text;
                    user.GuineaPigData.Colour1     = colour1Txt.Text;
                    user.GuineaPigData.Colour2     = colour2Txt.Text;
                    user.GuineaPigData.CuteRanking = Convert.ToInt32(cuteRankingTxt.Text);

                    var userService = new UserService();
                    userService.UpdateUser(user);

                    MessageBox.Show(user.Username + " details were updated.", "Details Updated", MessageBoxButtons.OK);
                    this.Hide();
                    var mainUi = new MainUI(user.Id);
                    mainUi.Show();
                }
                catch (FormatException fe) //validate formats, ensure we have proper email addresses and numbers where there should be (i.e. age should be 4, not "four")
                {
                    MessageBox.Show(fe.Message, "Error", MessageBoxButtons.OK);
                }
            }
            else
            {
                MessageBox.Show("Input was not a valid phone number.", "Invalid number.", MessageBoxButtons.OK);
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            //validate the new password
            if (newPassDupeTxt.Text == "" || newPassTxt.Text == "" || oldPassTxt.Text == "") //first, we check that the user has entered data
            {
                MessageBox.Show("All fields must contain data", "Error", MessageBoxButtons.OK);
            }
            else if (newPassTxt.Text != newPassDupeTxt.Text)
            {
                MessageBox.Show("Passwords do not match", "Error", MessageBoxButtons.OK); //secondly, we check the newly entered password is matched in the duplicated input box
            }
            else if (oldPassTxt.Text != user.Password)                                    //third, we check that the password was correct
            {
                passAttempts++;                                                           //increment the number of attempts the password has been attempted

                MessageBox.Show("Old password is incorrect.", "Error", MessageBoxButtons.OK);

                if (passAttempts == 3) //if the password attempts is 3, then we exit the system
                {
                    MessageBox.Show("Password has been entered incorrectly 3 times. System will log out");
                    this.Hide();
                    var login = new Login();
                    login.Show();
                }
            }
            else if (newPassTxt.Text == user.Password) //check that the new password does not equal the users current password
            {
                MessageBox.Show("The new password is the same as the old password.", "Error", MessageBoxButtons.OK);
            }
            else if (user.Details != null)
            {
                if (user.Details.FirstName == newPassTxt.Text || user.Details.MiddleNames == newPassTxt.Text || user.Details.LastName == newPassTxt.Text)
                {
                    MessageBox.Show("Password should not match any of your names, as that is not a strong password", "Invalid Password", MessageBoxButtons.OK);
                }
                else if (newPassTxt.Text.Contains(user.Details.FirstName) || newPassTxt.Text.Contains(user.Details.MiddleNames) || newPassTxt.Text.Contains(user.Details.LastName))
                {
                    MessageBox.Show("Password should not match or contain any of your names, as that is not a strong password", "Invalid Password", MessageBoxButtons.OK);
                }
            }
            else if (user.GuineaPigData != null)
            {
                if (user.GuineaPigData.Name == newPassTxt.Text)
                {
                    MessageBox.Show("Password should not match any of your guinea pig's name, as that is not a strong password", "Invalid Password", MessageBoxButtons.OK);
                }
                else if (newPassTxt.Text.Contains(user.GuineaPigData.Name))
                {
                    MessageBox.Show("Password should not match or contain your guinea pig's name, as that is not a strong password", "Invalid Password", MessageBoxButtons.OK);
                }
            }
            else if (gen.DetermineStrength(newPassTxt.Text) == PasswordStrength.Weak) //check the password strength
            {
                MessageBox.Show("Password is too weak to be saved.", "Invalid Password", MessageBoxButtons.OK);
            }
            else if (gen.CheckPasswordSimilarities(user.Password, newPassTxt.Text) > 10) //ensure the password is not similar to the old password
            {
                MessageBox.Show("Password is too similar to the previous password.", "Invalid Password", MessageBoxButtons.OK);
            }
            else if (passService.CheckPasswords(user.Username, newPassTxt.Text)) //check that the password has not been used before
            {
                MessageBox.Show("This password has been used before and it cannot be used again.", "Invalid Password", MessageBoxButtons.OK);
            }
            else
            {
                //if user passes all validation, we can change the password
                user.Password = newPassTxt.Text;

                if (passGenCheck.Checked) //if the password was generated, we need to save it to a file
                {
                    gen.SavePasswordToFile(newPassTxt.Text, user.Username);
                }

                service.UpdateUser(user);                           //we can then update the user
                passService.Update(user.Username, newPassTxt.Text); //update the password archive and then return to the mainUI
                MessageBox.Show("Password was changed", "Password Updated", MessageBoxButtons.OK);
                this.Hide();
                var previousForm = new MainUI(user.Id);
                previousForm.Show();
            }
        }
Ejemplo n.º 3
0
        private void createUserBtn_Click(object sender, EventArgs e)
        {
            //validate the password, output relevant messages to inform the user of the situation
            if (usernameTxt.Text == "" || passwordTxt.Text == "" || retypedPassTxt.Text == "") //first check we have data in the input fields
            {
                MessageBox.Show("Username and password fields are required to register.", "error", MessageBoxButtons.OK);
            }
            else if (passwordTxt.Text != retypedPassTxt.Text) //check that the new password and the retyped version match
            {
                MessageBox.Show("Passwords do not match.");
            }
            else if (!userService.CheckUsernameIsUnique(usernameTxt.Text)) //check the username is unique
            {
                MessageBox.Show("Username already exists.", "Error", MessageBoxButtons.OK);
            }
            else if (ValidateUsername(usernameTxt.Text)) //check the username does not contain special characters
            {
                MessageBox.Show("Username cannot contain special characters", "Error", MessageBoxButtons.OK);
            }
            else if (helper.CheckAgainstPhrases(passwordTxt.Text) == PasswordStrength.Weak)
            {
                MessageBox.Show("Password matches common phrases or the example password.", "Error", MessageBoxButtons.OK);
            }
            else if (strength == PasswordStrength.Weak) //check the password strength is not weak
            {
                MessageBox.Show("Password is too weak", "error", MessageBoxButtons.OK);
            }
            else
            {
                //then we can create the user, and add them to db. a new id is generated at the service layer.
                var user = new User
                {
                    Username     = usernameTxt.Text.ToString(),
                    Password     = passwordTxt.Text.ToString(),
                    PasswordUses = 0,
                };

                userService.AddUser(user);
                var passService = new PasswordService();

                var newPassword = new PasswordArchive //create a new password archive and store the users first password
                {
                    ID        = user.Id,
                    Username  = user.Username,
                    Passwords = new System.Collections.Generic.List <string>(),
                };

                newPassword.Passwords.Add(user.Password);

                passService.Put(newPassword);

                if (generatePassCheck.Checked) //if the password was generated, save the password to a file
                {
                    var passGen = new PasswordGenerator();
                    passGen.SavePasswordToFile(passwordTxt.Text, usernameTxt.Text);
                    MessageBox.Show("Generated Password was saved to project folders", "Password Saved", MessageBoxButtons.OK);
                }

                MessageBox.Show(generatePassCheck.Checked ? "User created! Password was saved to a file.": "User Created!", "User Created", MessageBoxButtons.OK);
                this.Hide();
                var mainUi = new MainUI(user.Id);
                mainUi.Show();
            }
        }
        private void loginBtn_Click(object sender, EventArgs e)
        {
            if (usernameTxt.Text == "" || passwordTxt.Text == "")//check we have data to work with in the fields
            {
                MessageBox.Show("All fields must have an entry to attempt to login", "Invalid", MessageBoxButtons.OK);
            }
            else
            {
                var userService = new UserService();
                var user        = userService.FindUser(usernameTxt.Text.ToString(), passwordTxt.Text.ToString());
                if (user == null)
                {
                    //if the user inputs incorrect data, dont let them know if it was the password or username they got wrong.
                    MessageBox.Show("Login failed. Check your username and password entries.", "Error", MessageBoxButtons.OK);
                    loginAttempts++;        //increment login attempts

                    if (loginAttempts == 3) //if login attempts ==3, lock system for 30 seconds, disable fields and display timer.
                    {
                        usernameTxt.Enabled = false;
                        passwordTxt.Enabled = false;
                        loginBtn.Enabled    = false;
                        timeTxt.Visible     = true;
                        var now = DateTime.Now;
                        while ((DateTime.Now - now).TotalSeconds <= 30) //when we have surpassed 30 seconds, we re-enable everything
                        {
                            timeTxt.Text = (DateTime.Now - now).TotalSeconds.ToString();
                            timeTxt.Refresh();
                        }

                        usernameTxt.Enabled = true;
                        passwordTxt.Enabled = true;
                        loginBtn.Enabled    = true;
                        timeTxt.Visible     = false;
                        loginAttempts       = 0;
                    }
                }
                else
                {
                    //if login is succesful...

                    user.PasswordUses++; //incremment password uses
                    userService.UpdateUser(user);

                    if (user.PasswordUses >= 5) //if password has been used to login in at least five times, prompt the user to change it (kinda like a 72 day change password)
                    {
                        var result = MessageBox.Show("It has been at least 5 days since you last changed your password. Would you like to change it now?", "Password", MessageBoxButtons.YesNo);

                        if (result == DialogResult.Yes)
                        {
                            this.Hide();
                            var changePassword = new ChangePassword(user.Id); //if user wants to change their password take them straight to the changePassword UI
                            changePassword.Show();
                        }
                        else //otherwise take them to the mainUI
                        {
                            this.Show();
                            var mainUi = new MainUI(user.Id);
                            mainUi.Show();
                        }
                    }
                    else
                    {
                        this.Show();
                        var mainUi = new MainUI(user.Id);
                        mainUi.Show();
                    }
                }
            }
        }