diff --git a/Authorization.cs b/Authorization.cs
new file mode 100644
index 0000000..8382da4
--- /dev/null
+++ b/Authorization.cs
@@ -0,0 +1,87 @@
+using System;
+using System.Text;
+using System.IO;
+using System.Security.Cryptography;
+using Microsoft.Data.Sqlite;
+
+namespace AwesomeEmailExtractor
+{
+ public class Authorization
+ {
+ public static User Login(string login, string password)
+ {
+ SqliteCommand command = new SqliteCommand();
+ command.Connection = Globals.db;
+ command.CommandText = "SELECT 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.GetString(0), (UserRoles)reader.GetInt32(1));
+ }
+
+ 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 new User(login, UserRoles.DEFAULT);
+ }
+
+ 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 string Login { get; set; }
+ public UserRoles Role { get; }
+
+ public User(string login, UserRoles role)
+ {
+ Login = login;
+ Role = role;
+ }
+ }
+}
diff --git a/AwesomeEmailExtractor.csproj b/AwesomeEmailExtractor.csproj
index 3ae428d..681d84e 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,9 @@
+
+
Form
@@ -67,6 +99,7 @@
True
Resources.resx
+
SettingsSingleFileGenerator
Settings.Designer.cs
@@ -81,4 +114,11 @@
+
+
+
+ Данный проект ссылается на пакеты NuGet, отсутствующие на этом компьютере. Используйте восстановление пакетов NuGet, чтобы скачать их. Дополнительную информацию см. по адресу: http://go.microsoft.com/fwlink/?LinkID=322105. Отсутствует следующий файл: {0}.
+
+
+
\ No newline at end of file
diff --git a/Globals.cs b/Globals.cs
new file mode 100644
index 0000000..df03053
--- /dev/null
+++ b/Globals.cs
@@ -0,0 +1,68 @@
+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 string getAppDirectory()
+ {
+ return Path.Combine(
+ Environment.GetFolderPath(
+ Environment.SpecialFolder.ApplicationData
+ ),
+ "AwesomeEmailExtractor"
+ );
+ }
+
+ public static string getAppDatabase()
+ {
+ return Path.Combine(
+ getAppDirectory(),
+ "database.db"
+ );
+ }
+
+
+
+ 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, 'DEFAULT'), (1, 'ADMIN');";
+ 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();
+ }
+ }
+ }
+}
diff --git a/Program.cs b/Program.cs
index a06ccf4..874679d 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,31 @@ namespace AwesomeEmailExtractor
[STAThread]
static void Main()
{
+ preMain();
+
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
+
+ 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();
+ }
+
+ static void postMain()
+ {
+ Globals.db.Close();
}
}
}
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