70 lines
2.1 KiB
C#
70 lines
2.1 KiB
C#
using MySql.Data.MySqlClient;
|
|
using PaymentLogs.Domain;
|
|
using PaymentLogs.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace PaymentLogs.Repositories
|
|
{
|
|
internal class UserRepo : Repo<string, User>
|
|
{
|
|
|
|
public override void Load()
|
|
{
|
|
DataBase db = new DataBase();
|
|
_data.Clear();
|
|
string sql = " SELECT * FROM usrs ";
|
|
db.openConnection();
|
|
MySqlCommand cmd = new MySqlCommand(sql, db.getConnection());
|
|
MySqlDataReader reader = cmd.ExecuteReader();
|
|
|
|
while (reader.Read()) {
|
|
User user = new User() { Login = reader.GetString("login"), PasswordHash = StringUtils.ConvertToSecureString(reader.GetString("password_hash")) };
|
|
_data[user.Login] = user;
|
|
}
|
|
db.closeConnection();
|
|
}
|
|
|
|
|
|
|
|
public void RegisterUser(string _login, string _password)
|
|
{
|
|
User user = new User() { Login = _login.ToLower(), PasswordHash = StringUtils.ConvertToSecureString(StringUtils.SHA256_hash(_password)) };
|
|
Save(user);
|
|
}
|
|
|
|
public Boolean HasUser(string _login)
|
|
{
|
|
return _data.ContainsKey(_login.ToLower());
|
|
}
|
|
|
|
|
|
private void Save(User user)
|
|
{
|
|
DataBase db = new DataBase();
|
|
db.openConnection();
|
|
string hash = StringUtils.SecureStringToString(user.PasswordHash);
|
|
string sql = $" INSERT INTO `usrs` (`login`, `password_hash`) VALUES ('{user.Login}','{hash}') " +
|
|
$"ON DUPLICATE KEY UPDATE `password_hash`='{hash}'";
|
|
MySqlCommand cmd = new MySqlCommand(sql, db.getConnection());
|
|
cmd.ExecuteNonQuery();
|
|
db.closeConnection();
|
|
}
|
|
|
|
|
|
public override void Save()
|
|
{
|
|
foreach(User user in _data.Values)
|
|
{
|
|
Save(user);
|
|
}
|
|
}
|
|
}
|
|
}
|