From c8bb88a734715745dcee97b804d5868d5ce43a3d Mon Sep 17 00:00:00 2001 From: Maxim Slipenko Date: Sun, 22 May 2022 10:00:00 +0300 Subject: [PATCH] =?UTF-8?q?=D1=83=D0=BB=D1=83=D1=87=D1=88=D0=B0=D0=B5?= =?UTF-8?q?=D1=82=20=D0=BB=D0=BE=D0=B3=D0=B8=20=D0=B8=20=D0=B4=D0=BE=D0=B1?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D1=8F=D0=B5=D1=82=20=D0=BC=D0=B5=D1=82=D0=BE?= =?UTF-8?q?=D0=B4=20=D0=B4=D0=BB=D1=8F=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D0=B5=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Authorization.cs | 16 ++++++++++++++++ AuthorizationForm.cs | 1 + Globals.cs | 2 +- Logs.cs | 22 +++++++++++++++++++--- MainForm.cs | 2 +- RegistrationForm.cs | 3 ++- 6 files changed, 40 insertions(+), 6 deletions(-) diff --git a/Authorization.cs b/Authorization.cs index 233693e..561041b 100644 --- a/Authorization.cs +++ b/Authorization.cs @@ -117,6 +117,22 @@ namespace AwesomeEmailExtractor command.ExecuteNonQuery(); } + public void deleteUser(string login) + { + if (User.Role != UserRoles.ADMIN) + { + throw new Exception("Недостаточно прав!"); + } + + SqliteCommand command = new SqliteCommand(); + command.Connection = Globals.db; + command.CommandText = "DELETE FROM users WHERE login = @login"; + + SqliteParameter loginParam = new SqliteParameter("@login", login); + command.Parameters.Add(loginParam); + + command.ExecuteNonQuery(); + } public List getAllUsers() { if (User.Role != UserRoles.ADMIN) diff --git a/AuthorizationForm.cs b/AuthorizationForm.cs index d53f6da..741cb61 100644 --- a/AuthorizationForm.cs +++ b/AuthorizationForm.cs @@ -22,6 +22,7 @@ namespace AwesomeEmailExtractor try { Globals.currentUser = Authorization.Login(entryLogin.Text, entryPassword.Text); + Logs.Log(Globals.currentUser, Logs.Action.Login, new Dictionary()); var mainForm = FormManager.Current.CreateForm(); FormManager.Current.Navigate(this, mainForm); diff --git a/Globals.cs b/Globals.cs index afb38d5..9ba88dd 100644 --- a/Globals.cs +++ b/Globals.cs @@ -66,7 +66,7 @@ namespace AwesomeEmailExtractor command.CommandText = "CREATE TABLE IF NOT EXISTS logs_actions (id INTEGER PRIMARY KEY, name TEXT NOT NULL)"; command.ExecuteNonQuery(); - command.CommandText = "INSERT OR IGNORE INTO logs_actions (id, name) VALUES (0, 'Выполнение');"; + command.CommandText = "INSERT OR IGNORE INTO logs_actions (id, name) VALUES (0, 'Выполнение'), (1, 'Вход'), (2, 'Регистрация');"; command.ExecuteNonQuery(); command.CommandText = "CREATE TABLE IF NOT EXISTS logs (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL, date TEXT NOT NULL, action INTEGER NOT NULL, message TEXT NOT NULL, FOREIGN KEY(action) REFERENCES logs_actions(id));"; diff --git a/Logs.cs b/Logs.cs index fa1a1bf..9b0a52d 100644 --- a/Logs.cs +++ b/Logs.cs @@ -19,6 +19,8 @@ namespace AwesomeEmailExtractor public enum Action { Execute, + Login, + Registration } public static void Log(User user, Action action, Dictionary options) @@ -68,7 +70,15 @@ namespace AwesomeEmailExtractor command.Parameters.AddWithValue("@dbpath", Globals.getAppDatabase()); command.ExecuteNonQuery(); - command.CommandText = "SELECT user_id, appDB.users.login, appDB.users.id as role_id, date, action, message FROM logs LEFT JOIN appDB.users ON logs.user_id = appDB.users.id ORDER BY date DESC "; + command.CommandText = @" + SELECT + user_id, + CASE WHEN appDB.users.login is NULL THEN 'Deleted_' || user_id ELSE appDB.users.login END AS login + appDB.users.role, + date, + action, + message + from logs LEFT JOIN appDB.users on logs.user_id = appDB.users.id ORDER BY date DESC"; SqliteDataReader reader = command.ExecuteReader(); @@ -100,10 +110,16 @@ namespace AwesomeEmailExtractor $"Найдено {count} email-ов.\n" + $"Список уникальных: {String.Join(", ", uniqueEmails)}."; } - else + if (action == Action.Login) { - return ""; + return "Пользователь вошел в систему."; } + if (action == Action.Registration) + { + return "Пользователь зарегистрировался в системе."; + } + + return ""; } } } diff --git a/MainForm.cs b/MainForm.cs index 5317cb7..ca6ca5a 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -45,7 +45,7 @@ namespace AwesomeEmailExtractor uniqueListBox.DataSource = uniqueEmails; Logs.Log( - new User(1, "", UserRoles.ADMIN), + Globals.currentUser, Logs.Action.Execute, new Dictionary() { { "sourceText", sourceText }, diff --git a/RegistrationForm.cs b/RegistrationForm.cs index 18d62f5..308f39b 100644 --- a/RegistrationForm.cs +++ b/RegistrationForm.cs @@ -28,7 +28,8 @@ namespace AwesomeEmailExtractor try { Globals.currentUser = Authorization.Register(entryLogin.Text, entryPassword.Text); - + Logs.Log(Globals.currentUser, Logs.Action.Registration, new Dictionary()); + var form = FormManager.Current.CreateForm(); FormManager.Current.Navigate(this, form); }