Beispiel #1
0
        /// <summary>
        ///     Adds a new User to the database.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            UserObject user = new UserObject();

            user = businessLogicLayer.CheckUserInsert(emailInput.Text.ToLower());
            try
            {
                if ((firstNameInput.Text.Equals("")) || (lastNameInput.Text.Equals("")) || (telephoneInput.Text.Equals("")) || (emailInput.Text.Equals("")) || (roleInput.Text.Equals("")) ||
                    (passwordInput.Password.Equals("")) || (passwordConfirmInput.Password.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;
                }
                // Used 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 ((!passwordInput.Password.Equals(passwordConfirmInput.Password)))
                {
                    MessageBox.Show("Please make sure the inputted passwords match.");
                    passwordInput.Focus();
                    return;
                }
                else if ((!Regex.IsMatch(passwordInput.Password, @"^[a-zA-Z0-9]{6,14}")) || (!Regex.IsMatch(passwordConfirmInput.Password, @"^[a-zA-Z0-9]{6,14}")))
                {
                    MessageBox.Show("Please make sure the password is only alphanumerical and between 6-14 characters long.");
                    passwordInput.Focus();
                    return;
                }
                else if (emailInput.Text.ToLower().Equals(user.email))
                {
                    MessageBox.Show("A user with that email already exists.");
                    emailInput.Focus();
                    return;
                }
                else
                {
                    user = businessLogicLayer.InsertNewUser(firstNameInput.Text.ToLower(), lastNameInput.Text.ToLower(), long.Parse(telephoneInput.Text),
                                                            emailInput.Text.ToLower(), roleInput.Text.ToLower(), passwordInput.Password);
                    MessageBox.Show(firstNameInput.Text + " " + lastNameInput.Text + " has been added to 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());
            }
        }