2022-05-21 23:33:56 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using Microsoft.Data.Sqlite;
|
2022-05-22 00:01:07 +03:00
|
|
|
|
using System.Collections.Generic;
|
2022-05-21 23:33:56 +03:00
|
|
|
|
|
|
|
|
|
namespace AwesomeEmailExtractor
|
|
|
|
|
{
|
|
|
|
|
public class Authorization
|
|
|
|
|
{
|
|
|
|
|
public static User Login(string login, string password)
|
|
|
|
|
{
|
|
|
|
|
SqliteCommand command = new SqliteCommand();
|
|
|
|
|
command.Connection = Globals.db;
|
2022-05-22 11:17:53 +03:00
|
|
|
|
command.CommandText = "SELECT id, login, role_id FROM users WHERE login = @login AND password = @password";
|
2022-05-21 23:33:56 +03:00
|
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
{
|
2022-05-22 11:17:53 +03:00
|
|
|
|
return new User(reader.GetInt32(0), reader.GetString(1), (UserRoles)reader.GetInt32(2));
|
2022-05-21 23:33:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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}");
|
|
|
|
|
};
|
|
|
|
|
|
2022-05-22 00:01:07 +03:00
|
|
|
|
return Login(login, password);
|
2022-05-21 23:33:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2022-05-22 00:01:07 +03:00
|
|
|
|
public int ID { get; }
|
|
|
|
|
public string Login { get; }
|
2022-05-21 23:33:56 +03:00
|
|
|
|
public UserRoles Role { get; }
|
|
|
|
|
|
2022-05-22 00:01:07 +03:00
|
|
|
|
public User(int id, string login, UserRoles role)
|
2022-05-21 23:33:56 +03:00
|
|
|
|
{
|
2022-05-22 00:01:07 +03:00
|
|
|
|
ID = id;
|
2022-05-21 23:33:56 +03:00
|
|
|
|
Login = login;
|
|
|
|
|
Role = role;
|
|
|
|
|
}
|
2022-05-24 09:09:56 +03:00
|
|
|
|
|
|
|
|
|
public User(User user)
|
|
|
|
|
{
|
|
|
|
|
ID = user.ID;
|
|
|
|
|
Login = user.Login;
|
|
|
|
|
Role = user.Role;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-22 10:56:32 +03:00
|
|
|
|
public void Delete()
|
|
|
|
|
{
|
|
|
|
|
SqliteCommand command = new SqliteCommand();
|
|
|
|
|
command.Connection = Globals.db;
|
2022-05-22 11:08:55 +03:00
|
|
|
|
command.CommandText = "DELETE FROM users WHERE id = @id;";
|
2022-05-22 10:56:32 +03:00
|
|
|
|
|
2022-05-22 11:08:55 +03:00
|
|
|
|
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;";
|
2022-05-22 10:56:32 +03:00
|
|
|
|
|
2022-05-22 11:08:55 +03:00
|
|
|
|
SqliteParameter idParam = new SqliteParameter("@id", ID);
|
|
|
|
|
command.Parameters.Add(idParam);
|
|
|
|
|
|
|
|
|
|
SqliteParameter passwordParam = new SqliteParameter("@password", Authorization.EncryptPassword(password));
|
|
|
|
|
command.Parameters.Add(passwordParam);
|
|
|
|
|
|
2022-05-22 10:56:32 +03:00
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
|
}
|
2022-05-23 21:08:30 +03:00
|
|
|
|
|
|
|
|
|
new public string ToString()
|
|
|
|
|
{
|
|
|
|
|
return this.Login;
|
|
|
|
|
}
|
2022-05-22 00:01:07 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class AdminUtils
|
|
|
|
|
{
|
|
|
|
|
public User User { get; set; }
|
|
|
|
|
|
|
|
|
|
public AdminUtils(User user)
|
|
|
|
|
{
|
|
|
|
|
User = user;
|
|
|
|
|
}
|
|
|
|
|
public void setRole(string login, UserRoles role)
|
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
}
|
2022-05-22 10:00:00 +03:00
|
|
|
|
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();
|
|
|
|
|
}
|
2022-05-24 09:09:56 +03:00
|
|
|
|
public List<User> GetAllUsers()
|
2022-05-22 00:01:07 +03:00
|
|
|
|
{
|
|
|
|
|
if (User.Role != UserRoles.ADMIN)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Недостаточно прав!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SqliteCommand command = new SqliteCommand();
|
|
|
|
|
command.Connection = Globals.db;
|
2022-05-24 09:09:56 +03:00
|
|
|
|
command.CommandText = "SELECT id, login, role_id FROM users";
|
2022-05-22 00:01:07 +03:00
|
|
|
|
|
|
|
|
|
SqliteDataReader reader = command.ExecuteReader();
|
|
|
|
|
|
|
|
|
|
List<User> users = new List<User>();
|
|
|
|
|
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
users.Add(new User(reader.GetInt32(0), reader.GetString(1), (UserRoles)reader.GetInt32(2)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return users;
|
|
|
|
|
}
|
2022-05-24 09:09:56 +03:00
|
|
|
|
|
|
|
|
|
public void editUser(User user)
|
|
|
|
|
{
|
|
|
|
|
if (User.Role != UserRoles.ADMIN)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Недостаточно прав!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SqliteCommand command = new SqliteCommand();
|
|
|
|
|
command.Connection = Globals.db;
|
|
|
|
|
command.CommandText = "UPDATE users SET login = @login, role_id = @role_id WHERE id = @id";
|
|
|
|
|
|
|
|
|
|
SqliteParameter idParam = new SqliteParameter("@id", user.ID);
|
|
|
|
|
command.Parameters.Add(idParam);
|
|
|
|
|
|
|
|
|
|
SqliteParameter loginParam = new SqliteParameter("@login", user.Login);
|
|
|
|
|
command.Parameters.Add(loginParam);
|
|
|
|
|
|
|
|
|
|
SqliteParameter roleParam = new SqliteParameter("@role_id", user.Role);
|
|
|
|
|
command.Parameters.Add(roleParam);
|
|
|
|
|
|
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void editUser(User user, string password)
|
|
|
|
|
{
|
|
|
|
|
editUser(user);
|
|
|
|
|
user.ChangePassword(password);
|
|
|
|
|
}
|
2022-05-21 23:33:56 +03:00
|
|
|
|
}
|
|
|
|
|
}
|