private async void button_addStaff_Click(object sender, EventArgs e) { if (controller != null) { try { if (!(textBox_password.Text.Equals(textBox_repeatPassword.Text))) { // cannot add, passwords don't match MessageBox.Show("Passwords do not match. Please try again", "Retail POS", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); logger.Info("Passwords do not match"); textBox_password.Text = null; textBox_repeatPassword.Text = null; return; } else { // can add // hash the password, prepare the data string fullName = textBox_fullName.Text; string password = textBox_password.Text; string privelege = comboBox_privelege.Text; Hasher hasher = new Hasher(fullName, password); string passwordHash = hasher.computeHash(); Model.ObjectModel.Staff newStaff = new Model.ObjectModel.Staff(); newStaff.FullName = fullName; newStaff.PasswordHash = passwordHash; switch (privelege) { case "Admin": newStaff.privelege = Model.ObjectModel.Staff.Privelege.Admin; break; case "Normal": newStaff.privelege = Model.ObjectModel.Staff.Privelege.Normal; break; default: // shouldn't happen return; } // log it logger.Info("Adding staff record: "); logger.Info("Full name: " + fullName); logger.Info("Password: "******"Hashed and salted password: "******"Privelege: " + privelege); await Task.Run(() => { // run this task in a separate thread controller.addStaff(newStaff); }); } } catch (Exception ex) { // failed to add new staff // tell the user and the logger string errorMessage = "Error adding new staff member: " + ex.Message; logger.Error(ex, errorMessage); logger.Error("Stack trace: " + ex.StackTrace); MessageBox.Show(errorMessage, "Retail POS", MessageBoxButtons.OK, MessageBoxIcon.Error); // nothing more we can do return; } // at this point, it succeeded // feedback for user string successMessage = "Successfully added new staff member record"; logger.Info(successMessage); MessageBox.Show(successMessage, "Retail POS", MessageBoxButtons.OK, MessageBoxIcon.Information); // clean up UI textBox_fullName.Text = string.Empty; textBox_password.Text = string.Empty; textBox_repeatPassword.Text = string.Empty; } }
private async void button_addStaff_Click(object sender, EventArgs e) { try { // use wait cursor this.UseWaitCursor = true; // update the status bar labelProgressBar1.setColourAndText(Configuration.ProgressBarColours.TASK_IN_PROGRESS_COLOUR, "Adding new staff record"); labelProgressBar1.Value = 100; if (!(textBox_password.Text.Equals(textBox_repeatPassword.Text))) { // cannot add, passwords don't match MessageBox.Show("Passwords do not match. Please try again", "Retail POS", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); logger.Info("Passwords do not match"); textBox_password.Text = null; textBox_repeatPassword.Text = null; return; } else { // can add // hash the password, prepare the data string fullName = textBox_fullName.Text; string password = textBox_password.Text; string privelege = comboBox_privelege.Text; Hasher hasher = new Hasher(fullName, password); string passwordHash = hasher.computeHash(); Model.ObjectModel.Staff newStaff = new Model.ObjectModel.Staff(); newStaff.fullName = fullName; newStaff.passwordHash = passwordHash; switch (privelege) { case "Admin": newStaff.privelege = Model.ObjectModel.Privelege.Admin; break; case "Normal": newStaff.privelege = Model.ObjectModel.Privelege.Normal; break; default: // shouldn't happen return; } // log it logger.Info("Adding staff record: "); logger.Info("Full name: " + fullName); logger.Info("Password: "******"Hashed and salted password: "******"Privelege: " + privelege); await Task.Run(() => { }); } } catch (Exception ex) { // failed to add new staff // tell the user and the logger string errorMessage = "Error adding new staff member: " + ex.Message; logger.Error(ex, errorMessage); logger.Error("Stack trace: " + ex.StackTrace); labelProgressBar1.setColourAndText(Configuration.ProgressBarColours.TASK_FAILED_COLOUR, "Error adding new staff"); labelProgressBar1.Value = 100; this.UseWaitCursor = false; MessageBox.Show(errorMessage, "Retail POS", MessageBoxButtons.OK, MessageBoxIcon.Error); // nothing more we can do return; } // at this point, it succeeded // feedback for user string successMessage = "Successfully added new staff member record"; logger.Info(successMessage); labelProgressBar1.setColourAndText(Configuration.ProgressBarColours.TASK_SUCCEEDED_COLOUR, successMessage); labelProgressBar1.Value = 100; this.UseWaitCursor = false; MessageBox.Show(successMessage, "Retail POS", MessageBoxButtons.OK, MessageBoxIcon.Information); // clean up UI textBox_fullName.Text = string.Empty; textBox_password.Text = string.Empty; textBox_repeatPassword.Text = string.Empty; }