2022-05-22 01:19:10 +03:00
|
|
|
|
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 {
|
2022-05-24 12:41:18 +03:00
|
|
|
|
public int ID { get; set; }
|
2022-05-23 21:08:30 +03:00
|
|
|
|
public User User { get; set; }
|
|
|
|
|
public string Date { get; set; }
|
|
|
|
|
public Action Action { get; set; }
|
|
|
|
|
public string Message { get; set; }
|
2022-05-24 12:41:18 +03:00
|
|
|
|
|
|
|
|
|
public void Delete()
|
|
|
|
|
{
|
|
|
|
|
SqliteCommand command = new SqliteCommand();
|
|
|
|
|
command.Connection = Globals.logsDb;
|
|
|
|
|
command.CommandText = "DELETE FROM logs WHERE id = @id;";
|
|
|
|
|
|
|
|
|
|
SqliteParameter idParam = new SqliteParameter("@id", ID);
|
|
|
|
|
command.Parameters.Add(idParam);
|
|
|
|
|
|
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
|
}
|
2022-05-22 01:19:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum Action
|
|
|
|
|
{
|
|
|
|
|
Execute,
|
2022-05-22 10:00:00 +03:00
|
|
|
|
Login,
|
2022-05-24 13:01:28 +03:00
|
|
|
|
Registration,
|
|
|
|
|
DeleteAccount,
|
|
|
|
|
ChangePassword
|
2022-05-22 01:19:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Log(User user, Action action, Dictionary<string, object> 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();
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-23 21:08:30 +03:00
|
|
|
|
public static List<LogData> GetLogsList(User user)
|
2022-05-22 01:19:10 +03:00
|
|
|
|
{
|
2022-05-23 21:08:30 +03:00
|
|
|
|
SqliteDataReader reader = GetLogsDataReader(user);
|
2022-05-22 01:19:10 +03:00
|
|
|
|
|
|
|
|
|
List<LogData> logs = new List<LogData>();
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
logs.Add(new LogData()
|
|
|
|
|
{
|
2022-05-24 12:41:18 +03:00
|
|
|
|
ID = Convert.ToInt32(reader["id"]),
|
2022-05-23 21:08:30 +03:00
|
|
|
|
User = user,
|
2022-05-24 12:41:18 +03:00
|
|
|
|
Date = Convert.ToString(reader["date"]),
|
|
|
|
|
Action = (Action)Convert.ToInt32(reader["action"]),
|
|
|
|
|
Message = Convert.ToString(reader["message"]),
|
2022-05-22 01:19:10 +03:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-23 21:08:30 +03:00
|
|
|
|
reader.Close();
|
|
|
|
|
|
2022-05-22 01:19:10 +03:00
|
|
|
|
return logs;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-23 21:08:30 +03:00
|
|
|
|
private static SqliteDataReader GetLogsDataReader(User user)
|
2022-05-22 01:19:10 +03:00
|
|
|
|
{
|
2022-05-23 21:08:30 +03:00
|
|
|
|
if (Globals.currentUser.ID != user.ID && Globals.currentUser.Role != UserRoles.ADMIN)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("У вас нет прав на просмотр логов");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SqliteCommand command = new SqliteCommand();
|
|
|
|
|
command.Connection = Globals.logsDb;
|
2022-05-24 12:41:18 +03:00
|
|
|
|
command.CommandText = "SELECT id, date, action, message FROM logs WHERE user_id = @user_id ORDER BY date DESC";
|
2022-05-23 21:08:30 +03:00
|
|
|
|
command.Parameters.AddWithValue("@user_id", user.ID);
|
|
|
|
|
|
|
|
|
|
return command.ExecuteReader();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<LogData> GetLogsList()
|
|
|
|
|
{
|
|
|
|
|
if (Globals.currentUser.Role != UserRoles.ADMIN)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("У вас нет прав на просмотр логов");
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-22 01:19:10 +03:00
|
|
|
|
SqliteCommand command = new SqliteCommand();
|
|
|
|
|
command.Connection = Globals.logsDb;
|
|
|
|
|
|
2022-05-22 10:00:00 +03:00
|
|
|
|
command.CommandText = @"
|
2022-05-24 12:41:18 +03:00
|
|
|
|
SELECT
|
|
|
|
|
logs.id,
|
2022-05-22 10:00:00 +03:00
|
|
|
|
user_id,
|
2022-05-24 12:41:18 +03:00
|
|
|
|
CASE WHEN appDB.users.login is NULL THEN 'Deleted_' || user_id ELSE appDB.users.login END AS login,
|
|
|
|
|
CASE WHEN appDB.users.role_id is NULL THEN 0 ELSE appDB.users.role_id END AS role_id,
|
2022-05-22 10:00:00 +03:00
|
|
|
|
date,
|
|
|
|
|
action,
|
|
|
|
|
message
|
2022-05-24 12:41:18 +03:00
|
|
|
|
from logs LEFT JOIN appDB.users on logs.user_id = appDB.users.id ORDER BY date DESC;";
|
2022-05-22 01:19:10 +03:00
|
|
|
|
|
|
|
|
|
SqliteDataReader reader = command.ExecuteReader();
|
|
|
|
|
|
|
|
|
|
List<LogData> logs = new List<LogData>();
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
logs.Add(new LogData()
|
|
|
|
|
{
|
2022-05-24 12:41:18 +03:00
|
|
|
|
ID = Convert.ToInt32(reader["id"]),
|
|
|
|
|
User = new User(
|
|
|
|
|
Convert.ToInt32(reader["user_id"]),
|
|
|
|
|
Convert.ToString(reader["login"]),
|
|
|
|
|
(UserRoles)Convert.ToInt32(reader["role_id"])
|
|
|
|
|
),
|
|
|
|
|
Date = Convert.ToString(reader["date"]),
|
|
|
|
|
Action = (Action)Convert.ToInt32(reader["action"]),
|
|
|
|
|
Message = Convert.ToString(reader["message"])
|
2022-05-22 01:19:10 +03:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return logs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetLogMessage(Action action, Dictionary<string, object> options)
|
|
|
|
|
{
|
|
|
|
|
if (action == Action.Execute)
|
|
|
|
|
{
|
|
|
|
|
string sourceText = (string)options["sourceText"];
|
|
|
|
|
int count = (int)options["count"];
|
|
|
|
|
List<string> uniqueEmails = options["uniqueEmails"] as List<string>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $"Пользователь выполнил поиск email-ов c таким исходным текстом: [ {sourceText}. ]\n" +
|
|
|
|
|
$"Найдено {count} email-ов.\n" +
|
|
|
|
|
$"Список уникальных: {String.Join(", ", uniqueEmails)}.";
|
|
|
|
|
}
|
2022-05-22 10:00:00 +03:00
|
|
|
|
if (action == Action.Login)
|
2022-05-22 01:19:10 +03:00
|
|
|
|
{
|
2022-05-22 10:00:00 +03:00
|
|
|
|
return "Пользователь вошел в систему.";
|
2022-05-22 01:19:10 +03:00
|
|
|
|
}
|
2022-05-22 10:00:00 +03:00
|
|
|
|
if (action == Action.Registration)
|
|
|
|
|
{
|
|
|
|
|
return "Пользователь зарегистрировался в системе.";
|
|
|
|
|
}
|
2022-05-24 13:01:28 +03:00
|
|
|
|
if (action == Action.DeleteAccount)
|
|
|
|
|
{
|
|
|
|
|
return "Пользователь удалил аккаунт из системы.";
|
|
|
|
|
}
|
|
|
|
|
if (action == Action.ChangePassword)
|
|
|
|
|
{
|
|
|
|
|
return "Пользователь сменил пароль.";
|
|
|
|
|
}
|
2022-05-22 10:00:00 +03:00
|
|
|
|
|
|
|
|
|
return "";
|
2022-05-22 01:19:10 +03:00
|
|
|
|
}
|
2022-05-24 12:41:18 +03:00
|
|
|
|
|
|
|
|
|
public static void DeleteLog(LogData logData)
|
|
|
|
|
{
|
|
|
|
|
SqliteCommand command = new SqliteCommand();
|
|
|
|
|
command.Connection = Globals.logsDb;
|
|
|
|
|
|
|
|
|
|
command.CommandText = "DELETE FROM logsDB WHERE id = @id";
|
|
|
|
|
|
|
|
|
|
command.Parameters.AddWithValue("@id", logData.ID);
|
|
|
|
|
|
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
|
}
|
2022-05-22 01:19:10 +03:00
|
|
|
|
}
|
|
|
|
|
}
|