private void BtnCancel_Click(object sender, RoutedEventArgs e) { // Clear the login form Lbl_ErrorLabel.Visibility = Visibility.Collapsed; TbxPassword.Clear(); TbxUsername.Clear(); }
/// <summary> /// Method used to log the user into the application /// </summary> private void UserLogin() { // Create local variables SystemUser validatedUser = null; MainDashboard mainDashboard; bool userValidated = false; string userName = TbxUsername.Text.Trim(); string userPassword = TbxPassword.Password.Trim(); if (loginAttemptCount < 3) { // Loop through all the system users available in the System Database loginAttemptCount++; if (validationUtils.ValidateUserInput(userName, userPassword)) { try { // Get the system user who matches the form data var systemUser = smaDB.SystemUsers.FirstOrDefault(s => s.Username == userName && s.Password == userPassword); if (systemUser.UserId > 0) { // Set the system user to the validated user validatedUser = systemUser; userValidated = true; } } catch (Exception) { // Display an error if the login was unsuccessful if (loginAttemptCount < 3) { Lbl_ErrorLabel.Content = "Username or Password incorrect"; Lbl_ErrorLabel.Visibility = Visibility.Visible; TbxPassword.Clear(); TbxUsername.Clear(); // Update the system logs if the login was unsucessful systemEventUtils.AddSystemEvent(new SystemEvent { UserId = 1002, EventTypeId = 1003, EventDateTime = DateTime.Now, EventData = $"Invalid Login attempt for unknown user at { DateTime.Now} , using {userName} / {userPassword} combination" }); } else { // Display an error after 3 failed logins MessageBox.Show("3 Failed Logins System now shutdown, Please contact the System Administrator", "Login Error", MessageBoxButton.OK, MessageBoxImage.Error); // Update the system logs if the login was unsucessful systemEventUtils.AddSystemEvent(new SystemEvent { UserId = 1002, EventTypeId = 1003, EventDateTime = DateTime.Now, EventData = $"System locked after 3 failed attempts for unknown user at { DateTime.Now} , using {userName} / {userPassword} combination" }); // Exit the application Close(); Environment.Exit(0); } } } else { if (loginAttemptCount < 3) { Lbl_ErrorLabel.Content = "Invalid Credentials"; Lbl_ErrorLabel.Visibility = Visibility.Visible; TbxPassword.Clear(); TbxUsername.Clear(); // Update the system logs if the login was unsucessful systemEventUtils.AddSystemEvent(new SystemEvent { UserId = 1002, EventTypeId = 1003, EventDateTime = DateTime.Now, EventData = $"Invalid Login attempt for unknown user at { DateTime.Now} , using {userName} / {userPassword} combination" }); } else { // Display an error after 3 failed logins MessageBox.Show("3 Failed Logins System now shutdown, Please contact the System Administrator", "Login Error", MessageBoxButton.OK, MessageBoxImage.Error); // Update the system logs if the login was unsucessful systemEventUtils.AddSystemEvent(new SystemEvent { UserId = 1002, EventTypeId = 1003, EventDateTime = DateTime.Now, EventData = $"System locked after 3 failed attempts for unknown user at { DateTime.Now} , using {userName} / {userPassword} combination" }); // Exit the application Close(); Environment.Exit(0); } } } else { // Display an error after 3 failed logins MessageBox.Show("3 Failed Logins System now shutdown, Please contact the System Administrator", "Login Error", MessageBoxButton.OK, MessageBoxImage.Error); // Update the system logs if the login was unsucessful systemEventUtils.AddSystemEvent(new SystemEvent { UserId = 1002, EventTypeId = 1003, EventDateTime = DateTime.Now, EventData = $"System locked after 3 failed attempts for unknown user at { DateTime.Now} , using {userName} / {userPassword} combination" }); // Exit the application Close(); Environment.Exit(0); } // If the user has been validated set up the mainDashboard and record the event in the logs if (userValidated) { mainDashboard = new MainDashboard(); if (validatedUser != null) { mainDashboard.systemUser = validatedUser; } // Update the system logs if the login was sucessful systemEventUtils.AddSystemEvent(new SystemEvent { UserId = validatedUser.UserId, EventTypeId = 1, EventDateTime = DateTime.Now, EventData = $"UserName { validatedUser.Username} successfully logged on to the application at { DateTime.Now}" }); mainDashboard.Owner = this; mainDashboard.ShowDialog(); this.Hide(); } }
private void BtnAddUser_Click(object sender, RoutedEventArgs e) { int selectAccessLevel = 0; try { selectAccessLevel = Convert.ToInt16(CmbBxAdminLevel.SelectedValue.ToString()); } catch (Exception) { // Show an error message if no access level has been selected. MessageBox.Show("Select an Access Level", "Selection Error", MessageBoxButton.OK, MessageBoxImage.Error); } if (validationUtils.ValidateUserAddInput(TbxGiven.Text, TbxSurname.Text, TbxUserId.Text, TbxPassword.Text) && selectAccessLevel > 0) { // Add the new user details to the system if (userUtils.AddSystemUser(new SystemUser { GivenName = TbxGiven.Text.Trim(), Surname = TbxSurname.Text.Trim(), Username = TbxUserId.Text.Trim(), Password = TbxPassword.Text.Trim(), AccessLevelId = selectAccessLevel }) == 1) { // Display a success message and make the return button visible TbxGiven.IsReadOnly = true; TbxSurname.IsReadOnly = true; TbxUserId.IsReadOnly = true; TbxPassword.IsReadOnly = true; BtnAddUser.Visibility = Visibility.Collapsed; BtnAddUserCancel.Visibility = Visibility.Collapsed; BtnAddUserReturn.Visibility = Visibility.Visible; Lbl_UserAddSuccessLabel.Visibility = Visibility.Visible; // Update the system logs if the record was added successfully try { systemEventUtils.AddSystemEvent(new SystemEvent { UserId = systemUser.UserId, EventTypeId = 3, EventDateTime = DateTime.Now, EventData = $"New SystemUser record added at { DateTime.Now} , by {systemUser.Username}" }); } catch (EntityException) { // Show error on failure MessageBox.Show("System Database Error, Please contact the System Administrator", "Database Error", MessageBoxButton.OK, MessageBoxImage.Error); } } else { // Make the ErrorLabel visible Lbl_UserAddErrorLabel.Visibility = Visibility.Visible; // Clear the details from the form TbxGiven.Clear(); TbxSurname.Clear(); TbxUserId.Clear(); TbxPassword.Clear(); // Update the system logs if the record wasn't added successfully try { systemEventUtils.AddSystemEvent(new SystemEvent { UserId = systemUser.UserId, EventTypeId = 1006, EventDateTime = DateTime.Now, EventData = $"Problem adding SystemUser record at { DateTime.Now} , by {systemUser.Username}" }); } catch (EntityException) { // Show error on failure MessageBox.Show("System Database Error, Please contact the System Administrator", "Database Error", MessageBoxButton.OK, MessageBoxImage.Error); } } } else { // Show an error message if the form data is invalid. MessageBox.Show("Invalid form data, please check and resubmit", "Selection Error", MessageBoxButton.OK, MessageBoxImage.Error); } }