authorizationForm.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11. namespace hospital
  12. {
  13. public partial class auth : Form
  14. {
  15. public static string userID = null;
  16. SqlConnection sqlConnection = null;
  17. SqlCommand a = null;
  18. public static string[] data = new string[0];
  19. public auth()
  20. {
  21. InitializeComponent();
  22. }
  23. private void Form1_Load(object sender, EventArgs e)
  24. {
  25. FormCreating.openConnection(ref sqlConnection);
  26. debugAndTrace.onStart();
  27. passwordTB.PasswordChar = '●';
  28. debugAndTrace.writeInFile("Вход в приложение");
  29. }
  30. private void signUpBtn_Click(object sender, EventArgs e)
  31. {
  32. registrationForm reg = new registrationForm();
  33. reg.Show();
  34. this.Hide();
  35. debugAndTrace.writeInFile("Переход на форму регистрации");
  36. reg.FormClosing += (obj, args) =>
  37. {
  38. this.Show();
  39. };
  40. }
  41. public static string signIn(string login, string password)
  42. {
  43. SqlConnection sqlConnection = null;
  44. FormCreating.openConnection(ref sqlConnection);
  45. SqlCommand a = null;
  46. if (login == "admin" && password == "admin")
  47. {
  48. return "Добро пожаловать, админ!";
  49. }
  50. else
  51. {
  52. a = new SqlCommand($"select count(*) from employees where login like {login.GetHashCode()} and password like {password.GetHashCode()} and id_post is null", sqlConnection);
  53. if (a.ExecuteScalar().ToString().Equals("1"))
  54. {
  55. return "Сначала вам необходимо получить роль!";
  56. }
  57. else
  58. {
  59. login = login.Trim();
  60. password = password.Trim();
  61. a = new SqlCommand($"select count(*) from employees where [login] like N'{login.GetHashCode()}' and [password] like N'{password.GetHashCode()}'", sqlConnection);
  62. Array.Resize(ref data, 2);
  63. data[0] = "Логин: " + login + "(" + login.GetHashCode() + ")";
  64. data[1] = "Пароль: " + password + "(" + password.GetHashCode() + ")";
  65. if (a.ExecuteScalar().ToString() == "1")
  66. {
  67. a = new SqlCommand($"select id_employee from employees where login like N'{login.GetHashCode()}' and password like N'{password.GetHashCode()}'", sqlConnection);
  68. userID = a.ExecuteScalar().ToString();
  69. return "Успешная авторизация!";
  70. }
  71. else
  72. {
  73. return "Такого пользователя не существует\nПроверьте правильность введенных данных, либо зарегистрируйте новый аккаунт";
  74. }
  75. }
  76. }
  77. }
  78. private void signInBtn_Click(object sender, EventArgs e)
  79. {
  80. if (loginTB.Text == "admin" && passwordTB.Text == "admin")
  81. {
  82. admin Admin = new admin();
  83. Admin.Show();
  84. this.Hide();
  85. Admin.FormClosing += (obj, args) =>
  86. {
  87. this.Show();
  88. };
  89. }
  90. else
  91. {
  92. a = new SqlCommand($"select count(*) from employees where login like {loginTB.Text.GetHashCode()} and password like {passwordTB.Text.GetHashCode()} and id_post is null", sqlConnection);
  93. if (a.ExecuteScalar().ToString().Equals("1"))
  94. {
  95. MessageBox.Show("Сначала вам необходимо получить роль!", "Ошабка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  96. }
  97. else
  98. {
  99. mainMenu menu = new mainMenu();
  100. loginTB.Text = loginTB.Text.Trim();
  101. passwordTB.Text = passwordTB.Text.Trim();
  102. a = new SqlCommand($"select count(*) from employees where [login] like N'{loginTB.Text.GetHashCode()}' and [password] like N'{passwordTB.Text.GetHashCode()}'", sqlConnection);
  103. Array.Resize(ref data, 2);
  104. data[0] = "Логин: " + loginTB.Text + "(" + loginTB.Text.GetHashCode() + ")";
  105. data[1] = "Пароль: " + passwordTB.Text + "(" + passwordTB.Text.GetHashCode() + ")";
  106. if (a.ExecuteScalar().ToString() == "1")
  107. {
  108. a = new SqlCommand($"select id_employee from employees where login like N'{loginTB.Text.GetHashCode()}' and password like N'{passwordTB.Text.GetHashCode()}'", sqlConnection);
  109. userID = a.ExecuteScalar().ToString();
  110. debugAndTrace.writeInFile("Авторизация", data);
  111. debugAndTrace.writeInFile("Переход на главную форму");
  112. menu.Show();
  113. this.Hide();
  114. }
  115. else
  116. {
  117. MessageBox.Show("Такого пользователя не существует\nПроверьте правильность введенных данных, либо зарегистрируйте новый аккаунт", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  118. debugAndTrace.writeInFile("Ошибка входа", data);
  119. }
  120. }
  121. }
  122. }
  123. private void auth_FormClosed(object sender, FormClosedEventArgs e)
  124. {
  125. Application.Exit();
  126. }
  127. private void button1_Click(object sender, EventArgs e)
  128. {
  129. //mainMenu menu = new mainMenu();
  130. //menu.Show();
  131. //this.Hide();
  132. }
  133. bool passwordChar = false;
  134. private void label1_Click(object sender, EventArgs e)
  135. {
  136. if (passwordChar)
  137. {
  138. passwordTB.PasswordChar = '●';
  139. passwordChar = !passwordChar;
  140. }
  141. else
  142. {
  143. passwordTB.PasswordChar = '\0';
  144. passwordChar = !passwordChar;
  145. }
  146. }
  147. private void label2_Click(object sender, EventArgs e)
  148. {
  149. }
  150. }
  151. }