public static void RegisterAdmin() { using (HBSModel _entity = new HBSModel()) { if (_entity.Users.Any(x => x.Username == GeneralUtils.ADMIN_ROLE)) { return; } Role role = new Role() { RoleName = GeneralUtils.ADMIN_ROLE }; Department department = new Department() { DepartmentName = "Management" }; User admin = new User(); admin.Username = GeneralUtils.ADMIN_ROLE; byte[] passwordHash, passwordSalt; GeneralUtils.CreatePasswordHash("Test123!", out passwordHash, out passwordSalt); admin.Pwd = passwordHash; admin.PwdSalt = passwordSalt; admin.RoleID = role.ID; admin.DepartmentID = department.ID; _entity.Users.Add(admin); role.Users.Add(admin); department.Users.Add(admin); _entity.Roles.Add(role); _entity.Departments.Add(department); _entity.SaveChanges(); } }
private void btn_password_Click(object sender, EventArgs e) { confirmPasswordErrorLabel.Visible = false; passwordErrorLabel.Visible = false; bool noErrors = true; try { if (String.IsNullOrEmpty(_selectedUser.Username)) { throw new Exception("No User selected"); } if (tb_password.Text != tb_repeat_password.Text) { confirmPasswordErrorLabel.Visible = true; noErrors = false; } if (!GeneralUtils.checkPasswordComplexity(tb_password.Text)) { passwordErrorLabel.Visible = true; noErrors = false; } if (noErrors) { using (HBSModel _entity = new HBSModel()) { var _user = _entity.Users.FirstOrDefault(user => user.Username == _selectedUser.Username); byte[] passwordHash, passwordSalt; GeneralUtils.CreatePasswordHash(tb_password.Text, out passwordHash, out passwordSalt); _user.Pwd = passwordHash; _user.PwdSalt = passwordSalt; _entity.SaveChanges(); MessageBox.Show("Password Updated", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information); initalizeUserInputs(); } } } catch (Exception ex) { DesktopAppUtils.popDefaultErrorMessageBox("Error:\n" + ex.Message); } }
public static void RegisterUsers() { using (HBSModel _entity = new HBSModel()) { Role role = new Role() { RoleName = "junior" }; Department department = new Department() { DepartmentName = "Construction" }; List <string> users = new List <String>(); users.Add("madalin"); users.Add("cristian"); users.Add("gabriel"); foreach (string username in users) { User newUser = new User(); newUser.Username = username; byte[] passwordHash, passwordSalt; GeneralUtils.CreatePasswordHash("password", out passwordHash, out passwordSalt); newUser.Pwd = passwordHash; newUser.PwdSalt = passwordSalt; newUser.StartDate = DateTime.Now.Date; newUser.RoleID = role.ID; newUser.DepartmentID = department.ID; _entity.Users.Add(newUser); role.Users.Add(newUser); department.Users.Add(newUser); } _entity.Roles.Add(role); _entity.Departments.Add(department); _entity.SaveChanges(); } }
private void btn_register_employee_Click(object sender, EventArgs e) { try { hideErrors(); bool noErrors = true; if (String.IsNullOrEmpty(tb_username.Text) || (tb_username.Text.Length < 6)) { usernameErrorLabel.Text = "Username must be above 6 characters"; usernameErrorLabel.Visible = true; noErrors = false; } if (String.IsNullOrEmpty(tb_password.Text)) { passwordErrorLabel.Text = "Password field must be filled"; passwordErrorLabel.Visible = true; noErrors = false; } if (tb_password.Text != tb_repeat_password.Text) { confirmPasswordErrorLabel.Text = "Passwords do not match"; confirmPasswordErrorLabel.Visible = true; noErrors = false; } if (!GeneralUtils.checkPasswordComplexity(tb_password.Text)) { passwordErrorLabel.Text = "Password does not match the required complexity"; passwordErrorLabel.Visible = true; noErrors = false; } if (cb_departments.SelectedIndex == -1) { departmentErrorLabel.Text = "Please select department"; departmentErrorLabel.Visible = true; noErrors = false; } if (cb_roles.SelectedIndex == -1) { roleErrorLabel.Text = "Please select role"; roleErrorLabel.Visible = true; noErrors = false; } if (!String.IsNullOrEmpty(tb_phoneNumber.Text)) { if (!tb_phoneNumber.ValidInput()) { phoneNumberErrorLabel.Text = "The phone number entered is not in a valid format"; phoneNumberErrorLabel.Visible = true; noErrors = false; } } if (noErrors) { using (HBSModel _entity = new HBSModel()) { User newUser = new User { Username = tb_username.Text }; var isAlreadyRegistered = _entity.Users.FirstOrDefault(x => x.Username == newUser.Username); if (isAlreadyRegistered != null) { usernameErrorLabel.Text = "Username already registered"; usernameErrorLabel.Visible = true; } // hash the password GeneralUtils.CreatePasswordHash(tb_password.Text, out byte[] passwordHash, out byte[] passwordSalt); newUser.Pwd = passwordHash; newUser.PwdSalt = passwordSalt; newUser.PhoneNumber = tb_phoneNumber.Text; // Find ID of selected role and department try { var _selectedDepartment = _entity.Departments.First(dpt => dpt.DepartmentName == cb_departments.SelectedItem.ToString()); var _selectedRole = _entity.Roles.First(role => role.RoleName == cb_roles.SelectedItem.ToString()); newUser.DepartmentID = _selectedDepartment.ID; newUser.RoleID = _selectedRole.ID; } catch (Exception ex) { DesktopAppUtils.popDefaultErrorMessageBox("Please select valid role and department:\n" + ex.Message); return; } newUser.PhoneNumber = tb_phoneNumber.Text; // get date and make it to datetime2 newUser.StartDate = dp_add_employee.Value.Date; // calculate remaining days newUser.RemainingDays = GeneralUtils.CalculateHolidayAllowanceOnRegistration(dp_add_employee.Value.Date); try { _entity.Users.Add(newUser); _entity.SaveChanges(); if (MessageBox.Show("Employee successfully registered", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK) { tb_username.Text = ""; tb_password.Text = ""; tb_repeat_password.Text = ""; cb_departments.SelectedIndex = -1; cb_roles.SelectedIndex = -1; dp_add_employee.Value = DateTime.Now; dp_add_employee.Format = DateTimePickerFormat.Custom; } } catch { DesktopAppUtils.popDefaultErrorMessageBox("Something went wrong, please try again later"); } } } } catch (Exception ex) { DesktopAppUtils.popDefaultErrorMessageBox("Registration Error: \n" + ex.Message); } }
public static void CreateUsers() { using (HBSModel _entity = new HBSModel()) { if (_entity.Users.Count() < 2) { byte[] passwordHash, passwordSalt; GeneralUtils.CreatePasswordHash("password", out passwordHash, out passwordSalt); _entity.Users.Add(new User() { Username = "******", Pwd = passwordHash, PwdSalt = passwordSalt, StartDate = new DateTime(2020, 1, 1), RemainingDays = GeneralUtils.CalculateHolidayAllowanceOnRegistration(new DateTime(2020, 1, 1)), RoleID = 3, DepartmentID = 3 }); _entity.Users.Add(new User() { Username = "******", Pwd = passwordHash, PwdSalt = passwordSalt, StartDate = new DateTime(2010, 1, 1), RemainingDays = GeneralUtils.CalculateHolidayAllowanceOnRegistration(new DateTime(2010, 1, 1)), RoleID = 4, DepartmentID = 3 }); _entity.Users.Add(new User() { Username = "******", Pwd = passwordHash, PwdSalt = passwordSalt, StartDate = new DateTime(2019, 1, 1), RemainingDays = GeneralUtils.CalculateHolidayAllowanceOnRegistration(new DateTime(2019, 1, 1)), RoleID = 6, DepartmentID = 3 }); _entity.Users.Add(new User() { Username = "******", Pwd = passwordHash, PwdSalt = passwordSalt, StartDate = new DateTime(2018, 5, 5), RemainingDays = GeneralUtils.CalculateHolidayAllowanceOnRegistration(new DateTime(2018, 5, 5)), RoleID = 7, DepartmentID = 3 }); _entity.Users.Add(new User() { Username = "******", Pwd = passwordHash, PwdSalt = passwordSalt, StartDate = new DateTime(2019, 6, 6), RemainingDays = GeneralUtils.CalculateHolidayAllowanceOnRegistration(new DateTime(2019, 6, 6)), RoleID = 8, DepartmentID = 3 }); _entity.Users.Add(new User() { Username = "******", Pwd = passwordHash, PwdSalt = passwordSalt, StartDate = new DateTime(2019, 2, 2), RemainingDays = GeneralUtils.CalculateHolidayAllowanceOnRegistration(new DateTime(2019, 2, 2)), RoleID = 5, DepartmentID = 3 }); _entity.Users.Add(new User() { Username = "******", Pwd = passwordHash, PwdSalt = passwordSalt, StartDate = new DateTime(2017, 3, 3), RemainingDays = GeneralUtils.CalculateHolidayAllowanceOnRegistration(new DateTime(2017, 3, 3)), RoleID = 7, DepartmentID = 3 }); _entity.Users.Add(new User() { Username = "******", Pwd = passwordHash, PwdSalt = passwordSalt, StartDate = new DateTime(2019, 9, 9), RemainingDays = GeneralUtils.CalculateHolidayAllowanceOnRegistration(new DateTime(2019, 9, 9)), RoleID = 6, DepartmentID = 3 }); _entity.Users.Add(new User() { Username = "******", Pwd = passwordHash, PwdSalt = passwordSalt, StartDate = new DateTime(2012, 6, 6), RemainingDays = GeneralUtils.CalculateHolidayAllowanceOnRegistration(new DateTime(2012, 6, 6)), RoleID = 5, DepartmentID = 3 }); _entity.SaveChanges(); } } }