From a38dfe230860240bf4a436ff11d50fdceac79f83 Mon Sep 17 00:00:00 2001 From: Maxim Slipenko Date: Sun, 22 May 2022 00:01:07 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D1=82=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20AdminUtils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Authorization.cs | 66 ++++++++++++++++++++++++++++++++++++++++++++---- Program.cs | 6 +++++ 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/Authorization.cs b/Authorization.cs index 8382da4..233693e 100644 --- a/Authorization.cs +++ b/Authorization.cs @@ -3,6 +3,7 @@ using System.Text; using System.IO; using System.Security.Cryptography; using Microsoft.Data.Sqlite; +using System.Collections.Generic; namespace AwesomeEmailExtractor { @@ -12,7 +13,7 @@ namespace AwesomeEmailExtractor { SqliteCommand command = new SqliteCommand(); command.Connection = Globals.db; - command.CommandText = "SELECT login, role_id FROM users WHERE login = @login AND password = @password"; + command.CommandText = "SELECT * FROM users WHERE login = @login AND password = @password"; SqliteParameter loginParam = new SqliteParameter("@login", login); command.Parameters.Add(loginParam); @@ -24,7 +25,7 @@ namespace AwesomeEmailExtractor while (reader.Read()) { - return new User(reader.GetString(0), (UserRoles)reader.GetInt32(1)); + return new User(reader.GetInt32(0), reader.GetString(1), (UserRoles)reader.GetInt32(1)); } throw new Exception("Пользователь не найден!"); @@ -54,7 +55,7 @@ namespace AwesomeEmailExtractor throw new Exception($"Ошибка: {e.Message}"); }; - return new User(login, UserRoles.DEFAULT); + return Login(login, password); } public static string EncryptPassword(string password) @@ -75,13 +76,68 @@ namespace AwesomeEmailExtractor } public class User { - public string Login { get; set; } + public int ID { get; } + public string Login { get; } public UserRoles Role { get; } - public User(string login, UserRoles role) + public User(int id, string login, UserRoles role) { + ID = id; Login = login; Role = role; } + + } + + public class AdminUtils + { + public User User { get; set; } + + public AdminUtils(User user) + { + User = user; + } + public void setRole(string login, UserRoles role) + { + if (User.Role != UserRoles.ADMIN) + { + throw new Exception("Недостаточно прав!"); + } + + SqliteCommand command = new SqliteCommand(); + command.Connection = Globals.db; + command.CommandText = "UPDATE users SET role_id = @role WHERE login = @login"; + + SqliteParameter roleParam = new SqliteParameter("@role", (int)role); + command.Parameters.Add(roleParam); + + SqliteParameter loginParam = new SqliteParameter("@login", login); + command.Parameters.Add(loginParam); + + command.ExecuteNonQuery(); + } + + public List getAllUsers() + { + if (User.Role != UserRoles.ADMIN) + { + throw new Exception("Недостаточно прав!"); + } + + SqliteCommand command = new SqliteCommand(); + command.Connection = Globals.db; + command.CommandText = "SELECT * FROM users"; + + SqliteDataReader reader = command.ExecuteReader(); + + List users = new List(); + + while (reader.Read()) + { + users.Add(new User(reader.GetInt32(0), reader.GetString(1), (UserRoles)reader.GetInt32(2))); + } + + return users; + } } } diff --git a/Program.cs b/Program.cs index 874679d..eebf76d 100644 --- a/Program.cs +++ b/Program.cs @@ -17,7 +17,13 @@ namespace AwesomeEmailExtractor static void Main() { preMain(); + + User user = new User(1, "admin", UserRoles.ADMIN); + AdminUtils admin = new AdminUtils(user); + + + Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm());