70 lines
2.6 KiB
C#
70 lines
2.6 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using MySqlConnector;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using uWine.Core.Domain.Entities;
|
|
using uWine.Core.Domain.Repositories;
|
|
|
|
namespace uWine.Core.Infrastructure.Repository
|
|
{
|
|
public class MariaDbAuthenticationRepository : MariaDbServerRepository, IAuthenticationRepository
|
|
{
|
|
public MariaDbAuthenticationRepository(IConfiguration configuration) : base(configuration)
|
|
{
|
|
}
|
|
|
|
public bool Valid(User user)
|
|
{
|
|
if (user == null || string.IsNullOrEmpty(user.Username) || string.IsNullOrEmpty(user.Password)) return false;
|
|
using (var conn = Connection())
|
|
{
|
|
conn.Open();
|
|
var cmd = new MySqlCommand("sp_login_user", conn);
|
|
cmd.CommandType = CommandType.StoredProcedure;
|
|
cmd.Parameters.AddWithValue("@IN_username", user.Username);
|
|
cmd.Parameters.AddWithValue("@IN_password", user.Password);
|
|
using (var reader = cmd.ExecuteReader())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
return reader.GetBoolean("valid");
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public User LoggedUser(User user)
|
|
{
|
|
using (var conn = Connection())
|
|
{
|
|
conn.Open();
|
|
var cmd = new MySqlCommand($@"SELECT u.id,
|
|
username,
|
|
u.role as role_id,
|
|
r.role as name,
|
|
description
|
|
FROM t_users u
|
|
JOIN t_role r on r.id = u.role
|
|
WHERE username = @username AND active = 1 AND
|
|
password = SHA2(CONCAT(@password, salt), 512)", conn);
|
|
cmd.Parameters.AddWithValue("username", user.Username);
|
|
cmd.Parameters.AddWithValue ("password", user.Password);
|
|
using( var reader = cmd.ExecuteReader())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
return User.LoginUser(reader);
|
|
}
|
|
|
|
}
|
|
}
|
|
return new User();
|
|
}
|
|
}
|
|
}
|