FormEditUser.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using PasswordCheckDll;
  12. namespace СУБД_Швейная_фабрика
  13. {
  14. public partial class FormEditUser : Form
  15. {
  16. public FormEditUser()
  17. {
  18. InitializeComponent();
  19. }
  20. private void cbxPassword_CheckedChanged(object sender, EventArgs e)
  21. {
  22. tbxPas.UseSystemPasswordChar = !tbxPas.UseSystemPasswordChar;
  23. tbxPasRepeat.UseSystemPasswordChar = !tbxPasRepeat.UseSystemPasswordChar;
  24. tbxOldPas.UseSystemPasswordChar = !tbxOldPas.UseSystemPasswordChar;
  25. }
  26. private void FormEditUser_Load(object sender, EventArgs e)
  27. {
  28. SqlConnection con = new SqlConnection(FormAuthorization.StrCon);
  29. con.Open();
  30. SqlCommand cmd = new SqlCommand($@"select Fam,Name,Otch,Phone from Users where IdUser = {FormAuthorization.IdUser}",con);
  31. SqlDataReader res = cmd.ExecuteReader();
  32. res.Read();
  33. tbxFam.Text = res["Fam"].ToString();
  34. tbxName.Text = res["Name"].ToString();
  35. tbxOtch.Text = res["Otch"].ToString();
  36. tbxPhone.Text = res["Phone"].ToString();
  37. res.Close();
  38. con.Close();
  39. }
  40. private void btnSaveEdit_Click(object sender, EventArgs e)
  41. {
  42. if (tbxFam.Text == "" || tbxName.Text == "" || tbxOtch.Text == "" || tbxPhone.Text == "" || tbxPas.Text.Trim() == "")
  43. {
  44. MessageBox.Show("Поля не могут быть пустыми", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  45. return;
  46. }
  47. SqlConnection con = new SqlConnection(FormAuthorization.StrCon);
  48. con.Open();
  49. SqlCommand cmd = new SqlCommand($@"select Fam,Name,Otch,Phone,Password from Users where IdUser = {FormAuthorization.IdUser}", con);
  50. SqlDataReader res = cmd.ExecuteReader();
  51. res.Read();
  52. if (res["Password"].ToString() != tbxOldPas.Text)
  53. {
  54. MessageBox.Show("Неправильно введён старый пароль", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  55. return;
  56. }
  57. if (!PasswordCheckClass.PasswordCheck(tbxPas.Text))
  58. {
  59. MessageBox.Show(@"Пароли не соответствует требования: • длина пароля – минимум 6 символов;
  60. • обязательно и строчные и прописные символы;
  61. • цифр должно быть не более половины от всех символов пароля;
  62. • должен содержать минимум 1 символ из набора: ! @ # $ % ^.", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  63. return;
  64. }
  65. if (tbxPas.Text != tbxPasRepeat.Text)
  66. {
  67. MessageBox.Show("Новый пароль не совпадает", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  68. return;
  69. }
  70. res.Close();
  71. SqlCommand cmdUpdate = new SqlCommand($@"update Users
  72. set Fam = '{tbxFam.Text}', Name = '{tbxName.Text}',Otch = '{tbxOtch.Text}',Password = '{tbxPas.Text}',Phone = '{tbxPhone.Text}'
  73. where IdUser = {FormAuthorization.IdUser}", con);
  74. cmdUpdate.ExecuteNonQuery();
  75. con.Close();
  76. MessageBox.Show("Сохранение прошло успешно", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  77. this.Close();
  78. }
  79. private void btnBack_Click(object sender, EventArgs e)
  80. {
  81. this.Close();
  82. }
  83. }
  84. }