//Saves changes to master user //SQLite boolean held as in. 0 = false, 1 = true private void Save_Click(object sender, EventArgs e) { int master = 0; //Used to hold sqlite bool figure int userId = Convert.ToInt32(Dropdown_1.SelectedValue); if (Master_User.Checked == true) { master = 1; } //Updates master user change if (UserDatabase.UpdateMasterUser(userId, master) == true) { MessageBox.Show("Update successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); Dropdown_1.DataSource = null; Master_User.Enabled = false; Delete.Enabled = false; Save.Enabled = false; } }
//********************Non Event Methods******************** //Creates New user private void Register_Click(object sender, EventArgs e) { //Error message for not filling in mandatory fields if (string.IsNullOrWhiteSpace(Input_1.Text) == true || string.IsNullOrWhiteSpace(Input_2.Text) == true || string.IsNullOrWhiteSpace(Input_3.Text) == true || string.IsNullOrWhiteSpace(Input_4.Text) == true || string.IsNullOrWhiteSpace(Input_5.Text) == true || string.IsNullOrWhiteSpace(Input_6.Text) == true || string.IsNullOrWhiteSpace(Input_7.Text) == true) { MessageBox.Show("All fields need to be filled in", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Error message if password doesn't match re enter password if (Input_4.Text != Input_5.Text) { MessageBox.Show("Password and re-enter password don't match", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Error messages if the username is the same or if the first and last name are the same string query = "SELECT USER_First_Name, USER_Last_Name, USER_Username FROM [USERS];"; using (Global.connection = new SQLiteConnection(Global.connectionString)) using (SQLiteCommand cmd = new SQLiteCommand(query, Global.connection)) { Global.connection.Open(); SQLiteDataReader reader = cmd.ExecuteReader(); try { while (reader.Read()) { string firstName = Convert.ToString(reader["USER_First_Name"]); string lastName = Convert.ToString(reader["USER_Last_Name"]); string userName = Convert.ToString(reader["USER_Username"]); if (Input_3.Text == userName) { MessageBox.Show("This username already exists. Please choose a different username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Users may have the same name but is unlikely. Gives user the option to continue if (Input_1.Text == firstName && Input_2.Text == lastName) { DialogResult areYouSure = MessageBox.Show("There is already a user with the same name:\n" + "UserName: "******"\nName: " + firstName + " " + lastName + "\n\n" + "Are you sure you would like to continue?", "Are you sure", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (areYouSure == DialogResult.No) { return; //cancels the event action } } } Global.connection.Close(); } catch (SQLiteException ex) { MessageBox.Show(ex.ToString()); } } //If no error message is prompted user is created UserDatabase.AddUser(Input_1.Text, Input_2.Text, Input_3.Text, Input_4.Text, Input_6.Text, Input_7.Text); MessageBox.Show("User successully created\n\nUsername: "******"\nPassword: "******"Register successful", MessageBoxButtons.OK, MessageBoxIcon.Information); //returns to login screen this.Hide(); Login_Screen login = new Login_Screen(); login.ShowDialog(); login.Focus(); }
//Checks entered username and loads secret question for user to enter in secret answer private void Submit_1_Click(object sender, EventArgs e) { UserDatabase.LoadSecretQuestion(this, Input_1, Input_2, Input_3, Submit_1, Submit_2); this.CenterToScreen(); }
//Shows a list of users private void Dropdown_2_DropDown(object sender, EventArgs e) { UserDatabase.PopulateUserDropdown(Dropdown_2); }
private void Submit_1_Click(object sender, EventArgs e) { string username = null; string hashedUserOldPassword = null; string databaseOldPassword = null; string hashedNewPassword = null; Boolean passwordMatch = false; //Error message for not filling in mandatory fields if (string.IsNullOrWhiteSpace(Input_1.Text) == true || string.IsNullOrWhiteSpace(Input_2.Text) == true || string.IsNullOrWhiteSpace(Input_3.Text) == true) { MessageBox.Show("All Fields Need to be Filled in", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Error message if password doesn't match re enter password if (Input_2.Text != Input_3.Text) { MessageBox.Show("Password and Re-Enter Password Don't Match", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } hashedUserOldPassword = Security.HashSHA1(Input_1.Text); hashedNewPassword = Security.HashSHA1(Input_2.Text); string query = "SELECT USER_Username, USER_Password FROM [USERS] WHERE USER_UserId = " + Global.userId + ";"; using (Global.connection = new SQLiteConnection(Global.connectionString)) using (SQLiteCommand cmd = new SQLiteCommand(query, Global.connection)) { try { Global.connection.Open(); SQLiteDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { databaseOldPassword = Convert.ToString(reader["USER_Password"]); username = Convert.ToString(reader["USER_Username"]); if (hashedUserOldPassword == databaseOldPassword) { passwordMatch = true; break; } } Global.connection.Close(); } catch (SQLiteException ex) { MessageBox.Show(ex.ToString()); } if (passwordMatch == true) { if (UserDatabase.ResetPassword(username, hashedNewPassword) == true) { MessageBox.Show("Password Changed Successfully", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Hide(); Change_User_Details change = new Change_User_Details(); change.ShowDialog(); change.Focus(); } else { MessageBox.Show("There was an Error Changing Your Password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("That is not your old password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
//Saves details of user private void Save_Click(object sender, EventArgs e) { //Error message for not filling in mandatory fields if (string.IsNullOrWhiteSpace(Input_1.Text) == true || string.IsNullOrWhiteSpace(Input_2.Text) == true || string.IsNullOrWhiteSpace(Input_3.Text) == true || string.IsNullOrWhiteSpace(Input_4.Text) == true || string.IsNullOrWhiteSpace(Input_5.Text) == true) { MessageBox.Show("All fields need to be filled in", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Error messages if the username is the same or if the first and last name are the same string query = "SELECT USER_First_Name, USER_Last_Name, USER_Username FROM [USERS] " + "WHERE USER_UserId NOT IN (" + Global.userId + ");"; using (Global.connection = new SQLiteConnection(Global.connectionString)) using (SQLiteCommand cmd = new SQLiteCommand(query, Global.connection)) { Global.connection.Open(); SQLiteDataReader reader = cmd.ExecuteReader(); try { while (reader.Read()) { string firstName = Convert.ToString(reader["USER_First_Name"]); string lastName = Convert.ToString(reader["USER_Last_Name"]); string userName = Convert.ToString(reader["USER_Username"]); //Error message if username exists if (Input_3.Text == userName) { MessageBox.Show("This username already exists. Please choose a different username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (Input_1.Text == firstName && Input_2.Text == lastName) { DialogResult areYouSure = MessageBox.Show("There is already a user with the same name:\n" + "UserName: "******"\nName: " + firstName + " " + lastName + "\n\n" + "Are you sure you would like to continue?", "Are You Sure", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (areYouSure == DialogResult.No) { return; //cancels the event action } } } Global.connection.Close(); } catch (SQLiteException ex) { MessageBox.Show(ex.ToString()); } //Updates user if (UserDatabase.UpdateUser(Input_1.Text, Input_2.Text, Input_3.Text, Input_4.Text, Input_5.Text) == true) { MessageBox.Show("Update successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); Input_1.ReadOnly = true; Input_2.ReadOnly = true; Input_3.ReadOnly = true; Input_4.ReadOnly = true; Input_5.ReadOnly = true; Edit_Details.Enabled = true; Save.Visible = false; Global.userFirstName = Input_1.Text; Global.userLastName = Input_2.Text; } } }