コード例 #1
0
        /// <summary>
        ///      Deletes a user from the database.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            UserObject user = new UserObject();

            user = businessLogicLayer.CheckUserDelete(firstNameInput.Text.ToLower(), lastNameInput.Text.ToLower(), long.Parse(telephoneInput.Text.ToLower()), emailInput.Text.ToLower(),
                                                      roleInput.Text.ToLower());
            try
            {
                if ((firstNameInput.Text.Equals("")) || (lastNameInput.Text.Equals("")) || (telephoneInput.Text.Equals("")) || (emailInput.Text.Equals("")) || (roleInput.Text.Equals("")))
                {
                    MessageBox.Show("Please input all text boxes.");
                    firstNameInput.Focus();
                    return;
                }
                else if ((!Regex.IsMatch(firstNameInput.Text, @"^[a-zA-Z]+$")) || (!Regex.IsMatch(lastNameInput.Text, @"^[a-zA-Z]+$")))
                {
                    MessageBox.Show("Please input only alphabetical characters for the first and last name text boxes");
                    firstNameInput.Focus();
                    return;
                }
                else if (!Regex.IsMatch(telephoneInput.Text, "^[0-9]{11}$"))
                {
                    MessageBox.Show("Please input only 11 numerical characters in the telephone text box.");
                    telephoneInput.Focus();
                    return;
                }
                // Official Microsoft email regex: https://github.com/Microsoft/referencesource/blob/master/System.ComponentModel.DataAnnotations/DataAnnotations/EmailAddressAttribute.cs
                else if (!Regex.IsMatch(emailInput.Text, @"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$"))
                {
                    MessageBox.Show("Please input a valid email address.");
                    emailInput.Focus();
                    return;
                }
                else if (!(roleInput.Text.ToLower().Equals("worker") || roleInput.Text.ToLower().Equals("manager") || roleInput.Text.ToLower().Equals("admin")))
                {
                    MessageBox.Show("Please input only 'worker', 'manager' or 'admin' in the role text box.");
                    roleInput.Focus();
                    return;
                }
                else if (!(firstNameInput.Text.ToLower().Equals(user.email) || lastNameInput.Text.ToLower().Equals(user.email) || telephoneInput.Text.ToLower().Equals(user.email) ||
                           emailInput.Text.ToLower().Equals(user.email) || roleInput.Text.ToLower().Equals(user.email)))
                {
                    MessageBox.Show("A user with these details does not exist.");
                    emailInput.Focus();
                    return;
                }
                else
                {
                    user = businessLogicLayer.DeleteCurrentUser(firstNameInput.Text.ToLower(), lastNameInput.Text.ToLower(), long.Parse(telephoneInput.Text),
                                                                emailInput.Text.ToLower(), roleInput.Text.ToLower());
                    MessageBox.Show(firstNameInput.Text + " " + lastNameInput.Text + " has been deleted from the system.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error has occurred, please contact your administrator." + "\n\n" + "The error message is: " + "\n\n" + ex.ToString());
            }
        }