Advertisement
Guest User

Untitled

a guest
Oct 12th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.69 KB | None | 0 0
  1. internal class Program : Utils
  2. {
  3. static bool _UserExist(string username)
  4. {
  5. Utils tools = new Utils();
  6. SqlConnection connection = new SqlConnection(tools.ConnectionString);
  7. SqlCommand command = new SqlCommand
  8. {
  9. Connection = connection,
  10. CommandType = CommandType.Text,
  11. CommandText = "USE TAO; SELECT * FROM [User] WHERE Username = " + "'" + username + "';"
  12. };
  13.  
  14. try
  15. {
  16. connection.Open();
  17. var reader = command.ExecuteReader();
  18. if (reader.HasRows)
  19. return true;
  20. }
  21. catch(Exception ex)
  22. {
  23. Console.ForegroundColor = ConsoleColor.Red;
  24. Console.WriteLine("Error during the creation of the user : " + ex.Message);
  25. }
  26. finally
  27. {
  28. Console.ForegroundColor = ConsoleColor.Gray;
  29. }
  30. return false;
  31. }
  32.  
  33. static void _AddUser()
  34. {
  35. MD5CryptoServiceProvider md5Hash = new MD5CryptoServiceProvider();
  36. List<char> passwordMask = new List<char>();
  37. Utils tools = new Utils();
  38. string username = null, password = "";
  39. SqlConnection connection = new SqlConnection(tools.ConnectionString);
  40. SqlCommand command = new SqlCommand();
  41.  
  42. Console.WriteLine("CreationUserTool\n");
  43. while (string.IsNullOrEmpty(username))
  44. {
  45. Console.Write("Username : ");
  46. username = Console.ReadLine();
  47. }
  48. while (passwordMask.Count == 0 || passwordMask.ToString() == "")
  49. {
  50. Console.Write("Password : ");
  51. ConsoleKeyInfo consoleKeyInfo;
  52. while ((consoleKeyInfo = Console.ReadKey(true)).Key != ConsoleKey.Enter)
  53. {
  54. if (consoleKeyInfo.Key != ConsoleKey.Backspace)
  55. {
  56. passwordMask.Add(consoleKeyInfo.KeyChar);
  57. Console.Write("*");
  58. }
  59. else if (passwordMask.Count > 0)
  60. {
  61. Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
  62. Console.Write(" ");
  63. Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
  64. if (passwordMask.Count > 0)
  65. passwordMask.RemoveAt(passwordMask.Count - 1);
  66. }
  67. }
  68. Console.WriteLine("");
  69. //password = Console.ReadLine();
  70. }
  71. if (_UserExist(username))
  72. {
  73. Console.ForegroundColor = ConsoleColor.Red;
  74. Console.WriteLine("User already exist");
  75. Console.ForegroundColor = ConsoleColor.White;
  76. ConsoleKeyInfo key = Console.ReadKey();
  77. if (key.Key == ConsoleKey.Enter || key.Key != ConsoleKey.Enter)
  78. return;
  79. }
  80. foreach (var item in passwordMask)
  81. {
  82. password += item;
  83. }
  84. var data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(password));
  85. md5Hash.Dispose();
  86. command.Connection = connection;
  87. command.CommandType = CommandType.Text;
  88. command.CommandText = "USE TAO; INSERT into [User] (Id, Username, Password) VALUES (@Id, @Username, @Password);";
  89. command.Parameters.AddWithValue("@Id", Guid.NewGuid());
  90. command.Parameters.AddWithValue("@Username", username);
  91. command.Parameters.AddWithValue("@Password", Convert.ToBase64String(data));
  92. try
  93. {
  94. connection.Open();
  95. command.ExecuteNonQuery();
  96. Console.ForegroundColor = ConsoleColor.Green;
  97. Console.WriteLine("User added successfully");
  98. ConsoleKeyInfo key = Console.ReadKey();
  99. if (key.Key == ConsoleKey.Enter || key.Key != ConsoleKey.Enter)
  100. return;
  101. }
  102. catch (SqlException ex)
  103. {
  104. Console.ForegroundColor = ConsoleColor.Red;
  105. Console.WriteLine("Error during the creation of the user : " + ex.Message);
  106. ConsoleKeyInfo key = Console.ReadKey();
  107. if (key.Key == ConsoleKey.Enter || key.Key != ConsoleKey.Enter)
  108. return;
  109. }
  110. finally
  111. {
  112. Console.ForegroundColor = ConsoleColor.Gray;
  113. }
  114. }
  115.  
  116. static void _ModifyUser()
  117. {
  118. int i, choice;
  119. string username = null, password = "";
  120. List<string> users = new List<string>();
  121. Utils tools = new Utils();
  122. SqlConnection connection = new SqlConnection(tools.ConnectionString);
  123. SqlCommand command = new SqlCommand
  124. {
  125. Connection = connection,
  126. CommandType = CommandType.Text,
  127. CommandText = "SELECT Username FROM [User];"
  128. };
  129.  
  130. try
  131. {
  132. connection.Open();
  133. var reader = command.ExecuteReader();
  134. while (reader.Read())
  135. {
  136. IDataRecord record = (IDataRecord)reader;
  137. users.Add($"{record[0]}");
  138. }
  139. reader.Close();
  140. if (users.Count == 0)
  141. {
  142. Console.WriteLine("ModificationUserTool\n");
  143. Console.ForegroundColor = ConsoleColor.Red;
  144. Console.WriteLine("No user to update");
  145. Console.ForegroundColor = ConsoleColor.Gray;
  146. ConsoleKeyInfo key = Console.ReadKey();
  147. if (key.Key == ConsoleKey.Enter || key.Key != ConsoleKey.Enter)
  148. return;
  149. }
  150. while (users.Count > 0)
  151. {
  152. Console.WriteLine("ModificationUserTool\n");
  153. Console.WriteLine("Choose user for modification : \n");
  154. i = 1;
  155. foreach (var user in users)
  156. {
  157. Console.WriteLine($"\t{i}) {user}");
  158. i++;
  159. }
  160. Console.WriteLine($"\t{i}) Quit\n");
  161. Console.Write("Which user : ");
  162. if (Int32.TryParse(Console.ReadLine(), out choice))
  163. {
  164. if (choice - 1 >= 0 && choice - 1 < users.Count)
  165. {
  166. command.CommandText = "USE TAO; DELETE FROM [User] WHERE Username = '" + users[choice - 1] + "';";
  167. command.ExecuteNonQuery();
  168. users.RemoveAt(choice - 1);
  169. Console.ForegroundColor = ConsoleColor.Green;
  170. Console.WriteLine("User deleted successfully");
  171. Console.ForegroundColor = ConsoleColor.Gray;
  172. }
  173. else if (choice - 1 == users.Count)
  174. {
  175. return;
  176. }
  177. }
  178. Console.Clear();
  179. }
  180. }
  181. catch (Exception ex)
  182. {
  183. Console.ForegroundColor = ConsoleColor.Red;
  184. Console.WriteLine("Error during the creation of the user : " + ex.Message);
  185. ConsoleKeyInfo key = Console.ReadKey();
  186. if (key.Key == ConsoleKey.Enter || key.Key != ConsoleKey.Enter)
  187. return;
  188. }
  189. finally
  190. {
  191. Console.ForegroundColor = ConsoleColor.Gray;
  192. }
  193. }
  194.  
  195. static void _DeleteUser()
  196. {
  197. List<string> users = new List<string>();
  198. Utils tools = new Utils();
  199. SqlConnection connection = new SqlConnection(tools.ConnectionString);
  200. SqlCommand command = new SqlCommand
  201. {
  202. Connection = connection,
  203. CommandType = CommandType.Text,
  204. CommandText = "SELECT Username FROM [User];"
  205. };
  206.  
  207. try
  208. {
  209. connection.Open();
  210. var reader = command.ExecuteReader();
  211. while (reader.Read())
  212. {
  213. IDataRecord record = (IDataRecord)reader;
  214. users.Add($"{record[0]}");
  215. }
  216. reader.Close();
  217. if (users.Count == 0)
  218. {
  219. Console.WriteLine("ModificationUserTool\n");
  220. Console.ForegroundColor = ConsoleColor.Red;
  221. Console.WriteLine("No users to delete");
  222. Console.ForegroundColor = ConsoleColor.Gray;
  223. ConsoleKeyInfo key = Console.ReadKey();
  224. if (key.Key == ConsoleKey.Enter || key.Key != ConsoleKey.Enter)
  225. return;
  226. }
  227. while (users.Count > 0)
  228. {
  229. Console.WriteLine("DeletionUserTool\n");
  230. Console.WriteLine("Choose agent for deletion : \n");
  231. var i = 1;
  232. foreach (var user in users)
  233. {
  234. Console.WriteLine($"\t{0}) {1}", i, user);
  235. i++;
  236. }
  237. Console.WriteLine($"\t{i}) Quit\n");
  238. Console.Write("Which agent : ");
  239. int choice;
  240. if (Int32.TryParse(Console.ReadLine(), out choice))
  241. {
  242. if (choice - 1 >= 0 && choice - 1 < users.Count)
  243. {
  244. command.CommandText = "USE TAO; DELETE FROM [User] WHERE Username = '" + users[choice - 1] + "';";
  245. command.ExecuteNonQuery();
  246. users.RemoveAt(choice - 1);
  247. Console.ForegroundColor = ConsoleColor.Green;
  248. Console.WriteLine("User deleted successfully");
  249. Console.ForegroundColor = ConsoleColor.Gray;
  250. }
  251. else if (choice - 1 == users.Count)
  252. {
  253. return;
  254. }
  255. }
  256. Console.Clear();
  257. }
  258. }
  259. catch (Exception ex)
  260. {
  261. Console.ForegroundColor = ConsoleColor.Red;
  262. Console.WriteLine("Error during the creation of the user : " + ex.Message);
  263. ConsoleKeyInfo key = Console.ReadKey();
  264. if (key.Key == ConsoleKey.Enter || key.Key != ConsoleKey.Enter)
  265. return;
  266. }
  267. finally
  268. {
  269. Console.ForegroundColor = ConsoleColor.Gray;
  270. }
  271. }
  272.  
  273. static void CheckChoice(string choice)
  274. {
  275. Console.Clear();
  276. switch (choice)
  277. {
  278. case "1":
  279. _AddUser();
  280. return;
  281. case "2":
  282. _ModifyUser();
  283. return;
  284. case "3":
  285. _DeleteUser();
  286. return;
  287. }
  288. }
  289.  
  290. static void Main(string[] args)
  291. {
  292. string choice;
  293. byte quit = 0;
  294.  
  295. while (quit != 1)
  296. {
  297. do
  298. {
  299. Console.Clear();
  300. Console.WriteLine("Welcome to the UsersManagementTool\n");
  301. Console.WriteLine("This tool allows you to :\n");
  302. Console.WriteLine("\t1) Create an user.");
  303. Console.WriteLine("\t2) Modify an user.");
  304. Console.WriteLine("\t3) Delete an user.");
  305. Console.WriteLine("\t4) Quit.\n");
  306. Console.Write("What do you want ? ");
  307. choice = Console.ReadLine();
  308. } while (choice != "1" && choice != "2" && choice != "3" && choice != "4");
  309. if (choice == "4")
  310. quit = 1;
  311. else
  312. CheckChoice(choice);
  313. }
  314. }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement