diff --git a/.vs/AwesomeEmailExtractor/project-colors.json b/.vs/AwesomeEmailExtractor/project-colors.json new file mode 100644 index 0000000..e49ce8c --- /dev/null +++ b/.vs/AwesomeEmailExtractor/project-colors.json @@ -0,0 +1,11 @@ +{ + "Version": 1, + "ProjectMap": { + "fe70a2f9-d831-462e-8705-b14623544f1c": { + "ProjectGuid": "fe70a2f9-d831-462e-8705-b14623544f1c", + "DisplayName": "AwesomeEmailExtractor", + "ColorIndex": 0 + } + }, + "NextColorIndex": 1 +} \ No newline at end of file diff --git a/Authorization.cs b/Authorization.cs new file mode 100644 index 0000000..81f550d --- /dev/null +++ b/Authorization.cs @@ -0,0 +1,185 @@ +using System; +using System.Text; +using System.IO; +using System.Security.Cryptography; +using Microsoft.Data.Sqlite; +using System.Collections.Generic; + +namespace AwesomeEmailExtractor +{ + public class Authorization + { + public static User Login(string login, string password) + { + SqliteCommand command = new SqliteCommand(); + command.Connection = Globals.db; + command.CommandText = "SELECT id, login, role_id FROM users WHERE login = @login AND password = @password"; + + SqliteParameter loginParam = new SqliteParameter("@login", login); + command.Parameters.Add(loginParam); + + SqliteParameter passwordParam = new SqliteParameter("@password", EncryptPassword(password)); + command.Parameters.Add(passwordParam); + + SqliteDataReader reader = command.ExecuteReader(); + + while (reader.Read()) + { + return new User(reader.GetInt32(0), reader.GetString(1), (UserRoles)reader.GetInt32(2)); + } + + throw new Exception("Пользователь не найден!"); + } + + public static User Register(string login, string password) + { + SqliteCommand command = new SqliteCommand(); + command.Connection = Globals.db; + command.CommandText = "INSERT INTO users (login, password, role_id) VALUES (@login, @password, 0);"; + + SqliteParameter loginParam = new SqliteParameter("@login", login); + command.Parameters.Add(loginParam); + + SqliteParameter passwordParam = new SqliteParameter("@password", EncryptPassword(password)); + command.Parameters.Add(passwordParam); + + try + { + command.ExecuteNonQuery(); + } catch (SqliteException e) + { + if (e.SqliteErrorCode == 19) { + throw new Exception("Имя пользователя занятно!"); + } + + throw new Exception($"Ошибка: {e.Message}"); + }; + + return Login(login, password); + } + + public static string EncryptPassword(string password) + { + using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) + { + UTF8Encoding utf8 = new UTF8Encoding(); + byte[] data = md5.ComputeHash(utf8.GetBytes(password)); + return Convert.ToBase64String(data); + } + } + } + + public enum UserRoles + { + DEFAULT, + ADMIN + } + public class User + { + public int ID { get; } + public string Login { get; } + public UserRoles Role { get; } + + public User(int id, string login, UserRoles role) + { + ID = id; + Login = login; + Role = role; + } + + public void Delete() + { + SqliteCommand command = new SqliteCommand(); + command.Connection = Globals.db; + command.CommandText = "DELETE FROM users WHERE id = @id;"; + + SqliteParameter idParam = new SqliteParameter("@id", ID); + command.Parameters.Add(idParam); + + command.ExecuteNonQuery(); + } + + public void ChangePassword(string password) + { + SqliteCommand command = new SqliteCommand(); + command.Connection = Globals.db; + command.CommandText = "UPDATE users SET password = @password WHERE id = @id;"; + + SqliteParameter idParam = new SqliteParameter("@id", ID); + command.Parameters.Add(idParam); + + SqliteParameter passwordParam = new SqliteParameter("@password", Authorization.EncryptPassword(password)); + command.Parameters.Add(passwordParam); + + command.ExecuteNonQuery(); + } + } + + 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 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) + { + 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/AuthorizationForm.Designer.cs b/AuthorizationForm.Designer.cs new file mode 100644 index 0000000..8902b21 --- /dev/null +++ b/AuthorizationForm.Designer.cs @@ -0,0 +1,125 @@ +namespace AwesomeEmailExtractor +{ + partial class AuthorizationForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.loginButton = new System.Windows.Forms.Button(); + this.registerButton = new System.Windows.Forms.Button(); + this.entryLogin = new System.Windows.Forms.TextBox(); + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.entryPassword = new System.Windows.Forms.TextBox(); + this.SuspendLayout(); + // + // loginButton + // + this.loginButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); + this.loginButton.Location = new System.Drawing.Point(12, 127); + this.loginButton.Name = "loginButton"; + this.loginButton.Size = new System.Drawing.Size(293, 23); + this.loginButton.TabIndex = 0; + this.loginButton.Text = "Авторизоваться"; + this.loginButton.UseVisualStyleBackColor = true; + this.loginButton.Click += new System.EventHandler(this.loginButton_Click); + // + // registerButton + // + this.registerButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); + this.registerButton.Location = new System.Drawing.Point(12, 156); + this.registerButton.Name = "registerButton"; + this.registerButton.Size = new System.Drawing.Size(293, 23); + this.registerButton.TabIndex = 1; + this.registerButton.Text = "Зарегистрироваться"; + this.registerButton.UseVisualStyleBackColor = true; + this.registerButton.Click += new System.EventHandler(this.registerButton_Click); + // + // entryLogin + // + this.entryLogin.Location = new System.Drawing.Point(110, 32); + this.entryLogin.Name = "entryLogin"; + this.entryLogin.Size = new System.Drawing.Size(195, 20); + this.entryLogin.TabIndex = 2; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); + this.label1.Location = new System.Drawing.Point(12, 30); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(77, 25); + this.label1.TabIndex = 3; + this.label1.Text = "Логин:"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); + this.label2.Location = new System.Drawing.Point(12, 77); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(92, 25); + this.label2.TabIndex = 5; + this.label2.Text = "Пароль:"; + // + // entryPassword + // + this.entryPassword.Location = new System.Drawing.Point(110, 79); + this.entryPassword.Name = "entryPassword"; + this.entryPassword.Size = new System.Drawing.Size(195, 20); + this.entryPassword.TabIndex = 4; + this.entryPassword.UseSystemPasswordChar = true; + // + // AuthorizationForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(317, 191); + this.Controls.Add(this.label2); + this.Controls.Add(this.entryPassword); + this.Controls.Add(this.label1); + this.Controls.Add(this.entryLogin); + this.Controls.Add(this.registerButton); + this.Controls.Add(this.loginButton); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; + this.MaximizeBox = false; + this.Name = "AuthorizationForm"; + this.Text = "Авторизоваться"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Button loginButton; + private System.Windows.Forms.Button registerButton; + private System.Windows.Forms.TextBox entryLogin; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.TextBox entryPassword; + } +} \ No newline at end of file diff --git a/AuthorizationForm.cs b/AuthorizationForm.cs new file mode 100644 index 0000000..741cb61 --- /dev/null +++ b/AuthorizationForm.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace AwesomeEmailExtractor +{ + public partial class AuthorizationForm : Form + { + public AuthorizationForm() + { + InitializeComponent(); + } + + private void loginButton_Click(object sender, EventArgs e) + { + 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); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + } + } + + private void registerButton_Click(object sender, EventArgs e) + { + var form = FormManager.Current.CreateForm(); + FormManager.Current.Navigate(this, form); + } + } +} diff --git a/AuthorizationForm.resx b/AuthorizationForm.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/AuthorizationForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/AwesomeEmailExtractor.csproj b/AwesomeEmailExtractor.csproj index 3ae428d..d078b5e 100644 --- a/AwesomeEmailExtractor.csproj +++ b/AwesomeEmailExtractor.csproj @@ -12,6 +12,8 @@ 512 true true + + AnyCPU @@ -33,8 +35,36 @@ 4 + + packages\Microsoft.Data.Sqlite.Core.5.0.3\lib\netstandard2.0\Microsoft.Data.Sqlite.dll + + + packages\SQLitePCLRaw.bundle_e_sqlite3.2.0.4\lib\net461\SQLitePCLRaw.batteries_v2.dll + + + packages\SQLitePCLRaw.core.2.0.4\lib\netstandard2.0\SQLitePCLRaw.core.dll + + + packages\SQLitePCLRaw.bundle_e_sqlite3.2.0.4\lib\net461\SQLitePCLRaw.nativelibrary.dll + + + packages\SQLitePCLRaw.provider.dynamic_cdecl.2.0.4\lib\netstandard2.0\SQLitePCLRaw.provider.dynamic_cdecl.dll + + + packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll + + + packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll + + + + packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll + + + packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll + @@ -46,7 +76,17 @@ + + + Form + + + AuthorizationForm.cs + + + + Form @@ -55,6 +95,21 @@ + + Form + + + RegistrationForm.cs + + + Form + + + SettingsForm.cs + + + AuthorizationForm.cs + MainForm.cs @@ -67,6 +122,13 @@ True Resources.resx + + SettingsForm.cs + + + + RegistrationForm.cs + SettingsSingleFileGenerator Settings.Designer.cs @@ -81,4 +143,11 @@ + + + + Данный проект ссылается на пакеты NuGet, отсутствующие на этом компьютере. Используйте восстановление пакетов NuGet, чтобы скачать их. Дополнительную информацию см. по адресу: http://go.microsoft.com/fwlink/?LinkID=322105. Отсутствует следующий файл: {0}. + + + \ No newline at end of file diff --git a/FormManager.cs b/FormManager.cs new file mode 100644 index 0000000..39a3473 --- /dev/null +++ b/FormManager.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace AwesomeEmailExtractor +{ + public class FormManager : ApplicationContext + { + private void onFormClosed(object sender, EventArgs e) + { + if (Application.OpenForms.Count == 0) + { + if (((Form)sender).Name == currentForm) + ExitThread(); + } + } + + public T CreateForm() where T : Form, new() + { + var ret = new T(); + ret.FormClosed += onFormClosed; + return ret; + } + + public void Navigate(Form from, Form to) + { + currentForm = to.Name; + from.Close(); + to.Show(); + } + + public string currentForm = "AuthorizationForm"; + + private static Lazy _current = new Lazy(); + public static FormManager Current => _current.Value; + + public FormManager() + { + var authorization = CreateForm(); + authorization.Show(); + } + } +} diff --git a/Globals.cs b/Globals.cs new file mode 100644 index 0000000..9ba88dd --- /dev/null +++ b/Globals.cs @@ -0,0 +1,124 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; +using Microsoft.Data.Sqlite; + + +namespace AwesomeEmailExtractor +{ + internal class Globals + { + // Getter and setter for SQLite database connection + public static SqliteConnection db { get; set; } + public static SqliteConnection logsDb { get; set; } + + public static User currentUser { get; set; } + + public static string getAppDirectory() + { + return Path.Combine( + Environment.GetFolderPath( + Environment.SpecialFolder.ApplicationData + ), + "AwesomeEmailExtractor" + ); + } + + public static string getAppDatabase() + { + return Path.Combine( + getAppDirectory(), + "database.db" + ); + } + + public static string getDefaultPathAppLogs() + { + return Path.Combine( + getAppDirectory(), + "logs.db" + ); + } + + public static string getPathAppLogs() + { + SqliteCommand command = new SqliteCommand(); + command.Connection = db; + command.CommandText = "SELECT logs_db_path FROM app_settings LIMIT 1"; + + SqliteDataReader reader = command.ExecuteReader(); + while (reader.Read()) + { + return reader["logs_db_path"].ToString(); + } + + return getDefaultPathAppLogs(); + } + + public static void CreateLogsTable() + { + SqliteCommand command = new SqliteCommand(); + command.Connection = logsDb; + + 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, 'Выполнение'), (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));"; + command.ExecuteNonQuery(); + + } + + public static void CreateTables() + { + SqliteCommand command = new SqliteCommand(); + command.Connection = db; + + // Создать таблицу для хранения ролей + command.CommandText = "CREATE TABLE IF NOT EXISTS roles (id INTEGER PRIMARY KEY, name TEXT NOT NULL);"; + command.ExecuteNonQuery(); + + // Добавить роли + command.CommandText = "INSERT OR IGNORE INTO roles (id, name) VALUES (0, 'Обычный'), (1, 'Администратор');"; + command.ExecuteNonQuery(); + + // Создать таблицу для хранения настроек (знаю, так плохо, но сойдет) + command.CommandText = "CREATE TABLE IF NOT EXISTS app_settings (logs_db_path TEXT);"; + command.ExecuteNonQuery(); + + // Создать таблицу для хранения пользователей + command.CommandText = "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, login TEXT NOT NULL UNIQUE, password TEXT NOT NULL, role_id INTEGER NOT NULL, FOREIGN KEY(role_id) REFERENCES roles(id));"; + command.ExecuteNonQuery(); + + // Если таблица пуста - добавить пользователя по умолчанию + command.CommandText = "SELECT COUNT(*) FROM users"; + + if (Convert.ToInt32(command.ExecuteScalar()) == 0) + { + command.CommandText = "INSERT INTO users (login, password, role_id) VALUES ('admin', @password, 1);"; + + SqliteParameter passwordParam = new SqliteParameter("@password", Authorization.EncryptPassword("admin")); + command.Parameters.Add(passwordParam); + command.ExecuteNonQuery(); + command.Parameters.Clear(); + } + + command.CommandText = "SELECT COUNT(*) FROM app_settings"; + + if (Convert.ToInt32(command.ExecuteScalar()) == 0) + { + command.CommandText = "INSERT INTO app_settings (logs_db_path) VALUES (@logs_db_path);"; + + SqliteParameter logsDbPathParam = new SqliteParameter("@logs_db_path", getDefaultPathAppLogs()); + command.Parameters.Add(logsDbPathParam); + command.ExecuteNonQuery(); + command.Parameters.Clear(); + } + } + } +} diff --git a/Logs.cs b/Logs.cs new file mode 100644 index 0000000..9b0a52d --- /dev/null +++ b/Logs.cs @@ -0,0 +1,125 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Data.Sqlite; + +namespace AwesomeEmailExtractor +{ + public class Logs + { + public class LogData { + public User user; + public string date; + public Action action; + public string message; + } + + public enum Action + { + Execute, + Login, + Registration + } + + public static void Log(User user, Action action, Dictionary options) + { + SqliteCommand command = new SqliteCommand(); + command.Connection = Globals.logsDb; + + command.CommandText = "INSERT INTO logs (user_id, date, action, message) VALUES (@user_id, strftime('%Y-%m-%d %H:%M:%S', datetime('now')), @action, @message)"; + + command.Parameters.AddWithValue("@user_id", user.ID); + command.Parameters.AddWithValue("@action", action); + command.Parameters.AddWithValue("@message", GetLogMessage(action, options)); + + command.ExecuteNonQuery(); + } + + public static List GetLogs(User user) + { + SqliteCommand command = new SqliteCommand(); + command.Connection = Globals.logsDb; + + command.CommandText = "SELECT date, action, message FROM logs WHERE user_id = @user_id ORDER BY date DESC"; + command.Parameters.AddWithValue("@user_id", user.ID); + + SqliteDataReader reader = command.ExecuteReader(); + + List logs = new List(); + while (reader.Read()) + { + logs.Add(new LogData() + { + user = user, + date = reader.GetString(0), + action = (Action)reader.GetInt32(1), + message = reader.GetString(2) + }); + } + + return logs; + } + + public static List GetLogs() + { + SqliteCommand command = new SqliteCommand(); + command.Connection = Globals.logsDb; + command.CommandText = "ATTACH DATABASE @dbpath AS appDB"; + command.Parameters.AddWithValue("@dbpath", Globals.getAppDatabase()); + command.ExecuteNonQuery(); + + 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(); + + List logs = new List(); + while (reader.Read()) + { + logs.Add(new LogData() + { + user = new User(reader.GetInt32(0), reader.GetString(1), (UserRoles)reader.GetInt32(2)), + date = reader.GetString(3), + action = (Action)reader.GetInt32(4), + message = reader.GetString(5) + }); + } + + return logs; + } + + public static string GetLogMessage(Action action, Dictionary options) + { + if (action == Action.Execute) + { + string sourceText = (string)options["sourceText"]; + int count = (int)options["count"]; + List uniqueEmails = options["uniqueEmails"] as List; + + + return $"Пользователь выполнил поиск email-ов c таким исходным текстом: [ {sourceText}. ]\n" + + $"Найдено {count} email-ов.\n" + + $"Список уникальных: {String.Join(", ", uniqueEmails)}."; + } + if (action == Action.Login) + { + return "Пользователь вошел в систему."; + } + if (action == Action.Registration) + { + return "Пользователь зарегистрировался в системе."; + } + + return ""; + } + } +} diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs index 8ce0ef9..f949bab 100644 --- a/MainForm.Designer.cs +++ b/MainForm.Designer.cs @@ -36,6 +36,10 @@ this.toolStripStatusLabel = new System.Windows.Forms.ToolStripStatusLabel(); this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.аккаунтToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.settingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.administrationToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.helpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.resultStatusStrip.SuspendLayout(); this.menuStrip1.SuspendLayout(); @@ -112,6 +116,7 @@ // this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.fileToolStripMenuItem, + this.аккаунтToolStripMenuItem, this.helpToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; @@ -125,6 +130,36 @@ this.fileToolStripMenuItem.Size = new System.Drawing.Size(48, 20); this.fileToolStripMenuItem.Text = "Файл"; // + // аккаунтToolStripMenuItem + // + this.аккаунтToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.settingsToolStripMenuItem, + this.administrationToolStripMenuItem, + this.exitToolStripMenuItem}); + this.аккаунтToolStripMenuItem.Name = "аккаунтToolStripMenuItem"; + this.аккаунтToolStripMenuItem.Size = new System.Drawing.Size(63, 20); + this.аккаунтToolStripMenuItem.Text = "Аккаунт"; + // + // settingsToolStripMenuItem + // + this.settingsToolStripMenuItem.Name = "settingsToolStripMenuItem"; + this.settingsToolStripMenuItem.Size = new System.Drawing.Size(189, 22); + this.settingsToolStripMenuItem.Text = "Настройки"; + this.settingsToolStripMenuItem.Click += new System.EventHandler(this.settingsToolStripMenuItem_Click); + // + // administrationToolStripMenuItem + // + this.administrationToolStripMenuItem.Name = "administrationToolStripMenuItem"; + this.administrationToolStripMenuItem.Size = new System.Drawing.Size(189, 22); + this.administrationToolStripMenuItem.Text = "Администрирование"; + // + // exitToolStripMenuItem + // + this.exitToolStripMenuItem.Name = "exitToolStripMenuItem"; + this.exitToolStripMenuItem.Size = new System.Drawing.Size(189, 22); + this.exitToolStripMenuItem.Text = "Выход"; + this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click); + // // helpToolStripMenuItem // this.helpToolStripMenuItem.Name = "helpToolStripMenuItem"; @@ -166,6 +201,10 @@ private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem helpToolStripMenuItem; private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel; + private System.Windows.Forms.ToolStripMenuItem аккаунтToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem settingsToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem administrationToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem; } } diff --git a/MainForm.cs b/MainForm.cs index c66334d..5ebee65 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -15,10 +15,21 @@ namespace AwesomeEmailExtractor public MainForm() { InitializeComponent(); - } + administrationToolStripMenuItem.Enabled = Globals.currentUser.Role == UserRoles.ADMIN; + } + private void executeButton_Click(object sender, EventArgs e) { + // Получаем исходный текст из sourceRichTextBox + string sourceText = sourceRichTextBox.Text; + + if (sourceText.Length == 0) + { + MessageBox.Show("Введите текст в поле исходного текста", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + // Чистим предыдущий результат toolStripStatusLabel.Text = ""; resultCountLabel.Text = ""; @@ -27,9 +38,6 @@ namespace AwesomeEmailExtractor // Объявляем список уникальных e-mail-ов List uniqueEmails = new List(); - // Получаем исходный текст из sourceRichTextBox - string sourceText = sourceRichTextBox.Text; - // Вызываем метод для извлечения e-mail-ов int count = ExtactEmailsAlgorithm.Extract(sourceText, out uniqueEmails); @@ -37,6 +45,28 @@ namespace AwesomeEmailExtractor toolStripStatusLabel.Text = "Успех!"; resultCountLabel.Text = $"Количество e-mail-ов в тексте: {count}"; uniqueListBox.DataSource = uniqueEmails; + + Logs.Log( + Globals.currentUser, + Logs.Action.Execute, + new Dictionary() { + { "sourceText", sourceText }, + { "count", count }, + { "uniqueEmails", uniqueEmails } + }); + } + + private void settingsToolStripMenuItem_Click(object sender, EventArgs e) + { + SettingsForm settingsForm = FormManager.Current.CreateForm(); + settingsForm.ShowDialog(this); + } + + private void exitToolStripMenuItem_Click(object sender, EventArgs e) + { + Globals.currentUser = null; + AuthorizationForm authorization = FormManager.Current.CreateForm(); + FormManager.Current.Navigate(this, authorization); } } } diff --git a/Program.cs b/Program.cs index a06ccf4..e40a3e9 100644 --- a/Program.cs +++ b/Program.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; +using System.IO; +using Microsoft.Data.Sqlite; namespace AwesomeEmailExtractor { @@ -14,9 +16,36 @@ namespace AwesomeEmailExtractor [STAThread] static void Main() { + preMain(); + Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new MainForm()); + Application.Run(FormManager.Current); + + postMain(); + } + + static void preMain() + { + if (!Directory.Exists(Globals.getAppDirectory())) + { + Directory.CreateDirectory(Globals.getAppDirectory()); + } + + Globals.db = new SqliteConnection("Data Source=" + Globals.getAppDatabase()); + Globals.db.Open(); + + Globals.CreateTables(); + + Globals.logsDb = new SqliteConnection("Data Source=" + Globals.getPathAppLogs()); + Globals.logsDb.Open(); + + Globals.CreateLogsTable(); + } + + static void postMain() + { + Globals.db.Close(); } } } diff --git a/RegistrationForm.Designer.cs b/RegistrationForm.Designer.cs new file mode 100644 index 0000000..7fe7ef6 --- /dev/null +++ b/RegistrationForm.Designer.cs @@ -0,0 +1,162 @@ +namespace AwesomeEmailExtractor +{ + partial class RegistrationForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.label2 = new System.Windows.Forms.Label(); + this.entryPassword = new System.Windows.Forms.TextBox(); + this.label1 = new System.Windows.Forms.Label(); + this.entryLogin = new System.Windows.Forms.TextBox(); + this.registerButton = new System.Windows.Forms.Button(); + this.loginButton = new System.Windows.Forms.Button(); + this.label3 = new System.Windows.Forms.Label(); + this.entryRePassword = new System.Windows.Forms.TextBox(); + this.label4 = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); + this.label2.Location = new System.Drawing.Point(9, 52); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(92, 25); + this.label2.TabIndex = 11; + this.label2.Text = "Пароль:"; + // + // entryPassword + // + this.entryPassword.Location = new System.Drawing.Point(183, 52); + this.entryPassword.Name = "entryPassword"; + this.entryPassword.Size = new System.Drawing.Size(195, 20); + this.entryPassword.TabIndex = 10; + this.entryPassword.UseSystemPasswordChar = true; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); + this.label1.Location = new System.Drawing.Point(9, 10); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(77, 25); + this.label1.TabIndex = 9; + this.label1.Text = "Логин:"; + // + // entryLogin + // + this.entryLogin.Location = new System.Drawing.Point(183, 10); + this.entryLogin.Name = "entryLogin"; + this.entryLogin.Size = new System.Drawing.Size(195, 20); + this.entryLogin.TabIndex = 8; + // + // registerButton + // + this.registerButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); + this.registerButton.Location = new System.Drawing.Point(14, 135); + this.registerButton.Name = "registerButton"; + this.registerButton.Size = new System.Drawing.Size(364, 23); + this.registerButton.TabIndex = 6; + this.registerButton.Text = "Зарегистрироваться"; + this.registerButton.UseVisualStyleBackColor = true; + this.registerButton.Click += new System.EventHandler(this.registerButton_Click); + // + // loginButton + // + this.loginButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); + this.loginButton.Location = new System.Drawing.Point(14, 187); + this.loginButton.Name = "loginButton"; + this.loginButton.Size = new System.Drawing.Size(364, 23); + this.loginButton.TabIndex = 7; + this.loginButton.Text = "Авторизоваться"; + this.loginButton.UseVisualStyleBackColor = true; + this.loginButton.Click += new System.EventHandler(this.loginButton_Click); + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); + this.label3.Location = new System.Drawing.Point(9, 94); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(167, 25); + this.label3.TabIndex = 13; + this.label3.Text = "Повтор пароля:"; + // + // entryRePassword + // + this.entryRePassword.Location = new System.Drawing.Point(183, 94); + this.entryRePassword.Name = "entryRePassword"; + this.entryRePassword.Size = new System.Drawing.Size(195, 20); + this.entryRePassword.TabIndex = 12; + this.entryRePassword.UseSystemPasswordChar = true; + // + // label4 + // + this.label4.AutoSize = true; + this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); + this.label4.Location = new System.Drawing.Point(12, 166); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(178, 18); + this.label4.TabIndex = 14; + this.label4.Text = "Уже зарегистрированы?"; + // + // RegistrationForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(391, 222); + this.Controls.Add(this.label4); + this.Controls.Add(this.label3); + this.Controls.Add(this.entryRePassword); + this.Controls.Add(this.label2); + this.Controls.Add(this.entryPassword); + this.Controls.Add(this.label1); + this.Controls.Add(this.entryLogin); + this.Controls.Add(this.registerButton); + this.Controls.Add(this.loginButton); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; + this.Name = "RegistrationForm"; + this.Text = "Зарегистрироваться"; + this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.RegistrationForm_FormClosed); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Label label2; + private System.Windows.Forms.TextBox entryPassword; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.TextBox entryLogin; + private System.Windows.Forms.Button registerButton; + private System.Windows.Forms.Button loginButton; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.TextBox entryRePassword; + private System.Windows.Forms.Label label4; + } +} \ No newline at end of file diff --git a/RegistrationForm.cs b/RegistrationForm.cs new file mode 100644 index 0000000..308f39b --- /dev/null +++ b/RegistrationForm.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace AwesomeEmailExtractor +{ + public partial class RegistrationForm : Form + { + public RegistrationForm() + { + InitializeComponent(); + } + + private void registerButton_Click(object sender, EventArgs e) + { + if (!string.Equals(entryPassword.Text, entryRePassword.Text)) + { + MessageBox.Show("Пароли не совпадают!"); + return; + } + + 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); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + } + } + + private void loginButton_Click(object sender, EventArgs e) + { + AuthorizationForm form = FormManager.Current.CreateForm(); + FormManager.Current.Navigate(this, form); + } + + private void RegistrationForm_FormClosed(object sender, FormClosedEventArgs e) + { + + } + } +} diff --git a/RegistrationForm.resx b/RegistrationForm.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/RegistrationForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/SettingsForm.Designer.cs b/SettingsForm.Designer.cs new file mode 100644 index 0000000..e4c61bf --- /dev/null +++ b/SettingsForm.Designer.cs @@ -0,0 +1,123 @@ +namespace AwesomeEmailExtractor +{ + partial class SettingsForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.changePasswordButton = new System.Windows.Forms.Button(); + this.entryNewPassword = new System.Windows.Forms.TextBox(); + this.label2 = new System.Windows.Forms.Label(); + this.label3 = new System.Windows.Forms.Label(); + this.entryRePassword = new System.Windows.Forms.TextBox(); + this.deleteAccountButton = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // changePasswordButton + // + this.changePasswordButton.Location = new System.Drawing.Point(34, 96); + this.changePasswordButton.Name = "changePasswordButton"; + this.changePasswordButton.Size = new System.Drawing.Size(275, 26); + this.changePasswordButton.TabIndex = 0; + this.changePasswordButton.Text = "Изменить пароль"; + this.changePasswordButton.UseVisualStyleBackColor = true; + this.changePasswordButton.Click += new System.EventHandler(this.changePasswordButton_Click); + // + // entryNewPassword + // + this.entryNewPassword.Location = new System.Drawing.Point(158, 12); + this.entryNewPassword.Name = "entryNewPassword"; + this.entryNewPassword.Size = new System.Drawing.Size(151, 20); + this.entryNewPassword.TabIndex = 2; + this.entryNewPassword.UseSystemPasswordChar = true; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); + this.label2.Location = new System.Drawing.Point(30, 12); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(118, 20); + this.label2.TabIndex = 4; + this.label2.Text = "Новый пароль"; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); + this.label3.Location = new System.Drawing.Point(30, 53); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(125, 20); + this.label3.TabIndex = 5; + this.label3.Text = "Повтор пароля"; + // + // entryRePassword + // + this.entryRePassword.Location = new System.Drawing.Point(158, 53); + this.entryRePassword.Name = "entryRePassword"; + this.entryRePassword.Size = new System.Drawing.Size(151, 20); + this.entryRePassword.TabIndex = 6; + this.entryRePassword.UseSystemPasswordChar = true; + // + // deleteAccountButton + // + this.deleteAccountButton.Location = new System.Drawing.Point(34, 137); + this.deleteAccountButton.Name = "deleteAccountButton"; + this.deleteAccountButton.Size = new System.Drawing.Size(275, 26); + this.deleteAccountButton.TabIndex = 7; + this.deleteAccountButton.Text = "Удалить аккаунт"; + this.deleteAccountButton.UseVisualStyleBackColor = true; + this.deleteAccountButton.Click += new System.EventHandler(this.deleteAccountButton_Click); + // + // SettingsForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(338, 184); + this.Controls.Add(this.deleteAccountButton); + this.Controls.Add(this.entryRePassword); + this.Controls.Add(this.label3); + this.Controls.Add(this.label2); + this.Controls.Add(this.entryNewPassword); + this.Controls.Add(this.changePasswordButton); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; + this.Name = "SettingsForm"; + this.Text = "Настройки"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Button changePasswordButton; + private System.Windows.Forms.TextBox entryNewPassword; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.TextBox entryRePassword; + private System.Windows.Forms.Button deleteAccountButton; + } +} \ No newline at end of file diff --git a/SettingsForm.cs b/SettingsForm.cs new file mode 100644 index 0000000..9d3aaac --- /dev/null +++ b/SettingsForm.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace AwesomeEmailExtractor +{ + public partial class SettingsForm : Form + { + public SettingsForm() + { + InitializeComponent(); + } + + private void deleteAccountButton_Click(object sender, EventArgs e) + { + DialogResult result = MessageBox.Show("Вы уверены что хотите удалить аккаунт?", "Удаление аккаунта", MessageBoxButtons.YesNo); + + if (result == DialogResult.Yes) + { + Globals.currentUser.Delete(); + MessageBox.Show("Аккаунт удален!"); + + this.Close(); + + AuthorizationForm authorization = FormManager.Current.CreateForm(); + FormManager.Current.Navigate(this.Owner, authorization); + } + } + + private void changePasswordButton_Click(object sender, EventArgs e) + { + if (!string.Equals(entryNewPassword.Text, entryRePassword.Text)) + { + MessageBox.Show("Пароли не совпадают!"); + return; + } + + Globals.currentUser.ChangePassword(entryNewPassword.Text); + MessageBox.Show("Пароль изменен!"); + } + } +} diff --git a/SettingsForm.resx b/SettingsForm.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/SettingsForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/packages.config b/packages.config new file mode 100644 index 0000000..277899e --- /dev/null +++ b/packages.config @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file