добавляет класс AdminUtils

This commit is contained in:
Maxim Slipenko 2022-05-22 00:01:07 +03:00
parent dfff6faacc
commit a38dfe2308
Signed by: Maks1mS
GPG Key ID: 7461AF39A8705FB8
2 changed files with 67 additions and 5 deletions

View File

@ -3,6 +3,7 @@ using System.Text;
using System.IO; using System.IO;
using System.Security.Cryptography; using System.Security.Cryptography;
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
using System.Collections.Generic;
namespace AwesomeEmailExtractor namespace AwesomeEmailExtractor
{ {
@ -12,7 +13,7 @@ namespace AwesomeEmailExtractor
{ {
SqliteCommand command = new SqliteCommand(); SqliteCommand command = new SqliteCommand();
command.Connection = Globals.db; 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); SqliteParameter loginParam = new SqliteParameter("@login", login);
command.Parameters.Add(loginParam); command.Parameters.Add(loginParam);
@ -24,7 +25,7 @@ namespace AwesomeEmailExtractor
while (reader.Read()) 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("Пользователь не найден!"); throw new Exception("Пользователь не найден!");
@ -54,7 +55,7 @@ namespace AwesomeEmailExtractor
throw new Exception($"Ошибка: {e.Message}"); throw new Exception($"Ошибка: {e.Message}");
}; };
return new User(login, UserRoles.DEFAULT); return Login(login, password);
} }
public static string EncryptPassword(string password) public static string EncryptPassword(string password)
@ -75,13 +76,68 @@ namespace AwesomeEmailExtractor
} }
public class User public class User
{ {
public string Login { get; set; } public int ID { get; }
public string Login { get; }
public UserRoles Role { get; } public UserRoles Role { get; }
public User(string login, UserRoles role) public User(int id, string login, UserRoles role)
{ {
ID = id;
Login = login; Login = login;
Role = role; 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<User> 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<User> users = new List<User>();
while (reader.Read())
{
users.Add(new User(reader.GetInt32(0), reader.GetString(1), (UserRoles)reader.GetInt32(2)));
}
return users;
}
} }
} }

View File

@ -18,6 +18,12 @@ namespace AwesomeEmailExtractor
{ {
preMain(); preMain();
User user = new User(1, "admin", UserRoles.ADMIN);
AdminUtils admin = new AdminUtils(user);
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm()); Application.Run(new MainForm());