예제 #1
0
        private void btnUserSettings_Click(object sender, EventArgs e)
        {
            frmFormSeparator Seperator = new frmFormSeparator();

            Seperator.Show();
            frmEditUser EditUser = new frmEditUser();

            EditUser.Show();
            btnAddSensor.BackColor    = Color.Transparent;
            btnUserSettings.BackColor = Color.FromArgb(58, 131, 79);
        }
        private void btnDeleteUser_Click(object sender, EventArgs e)
        {
            User   user                   = new User();
            string username               = txtUsername.Text;
            string password               = txtPassword.Text;
            string email                  = txtEmailDelete.Text;
            string securityQuestion       = "";
            string securityQuestionAnswer = "";

            string[] fields = lblDeleteCurrentSecurityQA.Text.Split('-');
            securityQuestion       = fields[0];
            securityQuestionAnswer = fields[1];

            string loggedUser = user.getCurrentLoggedUser();

            string[] loggedUserDetails = loggedUser.Split(';');
            string   usernameLogged    = loggedUserDetails[0];
            string   passwordLogged    = loggedUserDetails[1];

            if ((username != usernameLogged) && (password != passwordLogged))
            {
                if (MessageBox.Show("Are you sure you want to delete this user?", "Confirm User Deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    user = new User(username, password, email, securityQuestion, securityQuestionAnswer, selectedID);
                    user.deleteUsers();
                    MessageBox.Show("User was succefully deleted!", "Successful Deletion", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    frmEditUser EditUser = new frmEditUser();
                    EditUser.Show();

                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("This user is currently logged into the system and therefore cannot be deleted", "Deletion Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void btnUpdateUser_Click(object sender, EventArgs e)
        {
            string username               = txtUsernameRegister.Text;
            string password               = "";
            string confirmPassword        = "";
            string email                  = "";
            string confirmEmail           = "";
            string securityQuestion       = "";
            string securityQuestionAnswer = "";

            bool passwordValid   = false;
            bool emailValid      = false;
            bool securityQAValid = false;

            if (txtPasswordRegister.Text.Length >= 9) // password must be 9 or more characters long
            {
                password        = txtPasswordRegister.Text;
                confirmPassword = txtRePasswordRegister.Text;
                passwordValid   = true;
            }
            else
            {
                MessageBox.Show("Password must be at least 9 characters long", "Invalid Password Length", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                lblEnterPassword.ForeColor = Color.Red;
                txtPasswordRegister.Clear();
                txtRePasswordRegister.Clear();
            }

            if (txtEmail.Text.Contains("@"))
            {
                email        = txtEmail.Text;
                confirmEmail = txtConfirmEmail.Text;
                emailValid   = true;
            }
            else
            {
                MessageBox.Show("Email is invalid!", "Invalid Email", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                lblEnterEmail.ForeColor = Color.Red;
                txtEmail.Clear();
                txtConfirmEmail.Clear();
            }

            if ((pnlNewSecurityQA.Visible == true))
            {
                if (cmbSecurityQuestion.SelectedIndex != 0)
                {
                    securityQuestion = cmbSecurityQuestion.SelectedItem.ToString();

                    if (txtNewSecurityAnswer.Text != "")
                    {
                        securityQuestionAnswer = txtNewSecurityAnswer.Text;
                    }
                    else
                    {
                        MessageBox.Show("Incorrect Security Answer! Please try again.", "Invalid Security Answer", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtNewSecurityAnswer.Clear();
                        lblEnterAnswer.ForeColor = Color.Red;
                    }

                    if ((securityQuestion != "") && (securityQuestionAnswer != ""))
                    {
                        securityQAValid = true;
                    }
                }
                else
                {
                    string[] fields = lblCurrentQA.Text.Split('-');
                    securityQuestion       = fields[0];
                    securityQuestionAnswer = fields[1];
                    securityQAValid        = true;
                }
            }
            else
            {
                string[] fields = lblCurrentQA.Text.Split('-');
                securityQuestion       = fields[0];
                securityQuestionAnswer = fields[1];
                securityQAValid        = true;
            }

            if (password == confirmPassword)
            {
                if (email == confirmEmail)
                {
                    if ((passwordValid == true) && (emailValid == true) && (securityQAValid == true))
                    {
                        User user = new User(username, password, email, securityQuestion, securityQuestionAnswer, selectedID);
                        user.updateUsers();

                        MessageBox.Show("User was successfully updated!", "Successful Update", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        frmEditUser EditUser = new frmEditUser();
                        EditUser.Show();
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("Retry email entries", "Email Mismatch", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    lblEnterEmail.ForeColor   = Color.Red;
                    lblConfirmEmail.ForeColor = Color.Red;
                    txtEmail.Clear();
                    txtConfirmEmail.Clear();
                }
            }
            else
            {
                MessageBox.Show("Retry password entries", "Password Mismatch", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                lblEnterPassword.ForeColor   = Color.Red;
                lblReEnterPassword.ForeColor = Color.Red;
                txtPasswordRegister.Clear();
                txtRePasswordRegister.Clear();
            }
        }