private void BtnCreate_Click(object sender, EventArgs e) { try { if (FormLoggedUser.Role.UserRoleId == 2) { MessageBox.Show("You don't have permision to create users", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { usr.Name = txtNameCreate.Text; usr.LastName = txtLastNameCreate.Text; usr.Email = txtEmailCreate.Text; usr.Username = txtUsernameCreate.Text; usr.InsBy = FormLoggedUser.Id; usr.Password = Sec.Hash(usr.Username, txtPasswordCreate.Text); usr._role = getRoleCreate(); usr.RoleID = usr._role.UserRoleId; UsersValidation usrval = new UsersValidation(); ValidationResult vres = usrval.Validate(usr); if (vres.IsValid == false) { foreach (ValidationFailure item in vres.Errors) { errors += $"{item.ErrorMessage} + \n"; } MessageBox.Show(errors); } int error = usrbll.Add(usr); if (error == 0) { this.Close(); } else if (error == 1) { MessageBox.Show("This username alreay exists, please if this user is deactivated you can update it"); } else if (error == -1) { throw new Exception(); } } } catch (FormatException) { MessageBox.Show("User is not valid"); } catch (Exception) { MessageBox.Show("User is not inserted please contact your administrator"); } }
private void BtnUpdatePassword_Click(object sender, EventArgs e) { try { if (FormLoggedUser.Role.UserRoleId == 2) { MessageBox.Show("You don't have permision to change passwords", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { usrbll.ChangePassword(usr.UserID, Sec.Hash(txtUsernameChangePassword.Text, txtPasswordChangePassword.Text), FormLoggedUser.Id); this.Close(); } } catch (Exception) { throw; } }
private void btnLogin_Click(object sender, EventArgs e) { try { UserBLL usrbll = new UserBLL(); User usr = new User(); string pw = Sec.Hash(txtUsernameLogin.Text, txtPasswordLogin.Text); usr = usrbll.LogIn(txtUsernameLogin.Text, pw); if (usr == null) { throw new Exception(); } else if (usr.Password == pw) { FormLoggedUser.Id = usr.UserID; FormLoggedUser.Name = usr.Name; FormLoggedUser.LastName = usr.LastName; FormLoggedUser.Email = usr.Email; FormLoggedUser.Username = txtUsernameLogin.Text; FormLoggedUser.Password = txtPasswordLogin.Text; FormLoggedUser.Role = usr._role; this.Hide(); } else { throw new Exception(); } } catch (Exception) { MessageBox.Show("The Log In Information you typed is incorrect. \nPlease try again!", "Information Incorrect!", MessageBoxButtons.OK, MessageBoxIcon.Information); } }