106 lines
4.2 KiB
C#
106 lines
4.2 KiB
C#
namespace LaaProduction.Services
|
|
{
|
|
using LaaPackages.SqlClient;
|
|
using LaaProduction.Services.Interfaces;
|
|
using LaaProduction.Services.Models;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
public class AccountService : IAccountService
|
|
{
|
|
private readonly SqlConnection sqlConnection;
|
|
|
|
public AccountService(SqlConnection sqlConnection)
|
|
=> this.sqlConnection = sqlConnection;
|
|
|
|
public Employee FindEmployee(Int16 userId)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
SELECT [Vorname] + ' ' + [Name]
|
|
FROM [Mitarbeiter]
|
|
WHERE [MitarbeiterNr] = @{nameof(userId)}")
|
|
.SetParameter(nameof(userId), userId)
|
|
.FirstOrDefault(x => new Employee
|
|
{
|
|
Name = x.GetValue<String>(0).Trim(),
|
|
Permissions = this.sqlConnection
|
|
.CreateCommand($@"
|
|
SELECT DISTINCT [Recht]
|
|
FROM [MitarbeiterRechte]
|
|
WHERE [MitarbeiterNr] = @{nameof(userId)}")
|
|
.SetParameter(nameof(userId), userId)
|
|
.ExecuteReader(p => p.GetValue<String>(0))
|
|
});
|
|
|
|
public IEnumerable<String> FindUserFunctions(Int16 appId, String username, String password)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
SELECT [SF].[Function]
|
|
FROM [Mitarbeiter] AS [U]
|
|
JOIN [UsersFunctions] AS [UF]
|
|
ON [UF].[UserId] = [U].[MitarbeiterNr]
|
|
JOIN [SoftwareFunctions] AS [SF]
|
|
ON [SF].[Id] = [UF].[SoftwareFunctionId]
|
|
WHERE [U].[Benutzername] = @{nameof(username)}
|
|
AND [U].[PasswordHash] = @{nameof(password)}
|
|
AND [SF].[AppId] = @{nameof(appId)}")
|
|
.SetParameter(nameof(username), username)
|
|
.SetParameter(nameof(password), password)
|
|
.SetParameter(nameof(appId), appId)
|
|
.ExecuteReader(x => x.GetValue<String>(0));
|
|
|
|
public Int16 Login(String username, String password)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
SELECT TOP 1 [MitarbeiterNr]
|
|
FROM [Mitarbeiter]
|
|
WHERE [Benutzername] LIKE @{nameof(username)}
|
|
AND [PasswordHash] LIKE @{nameof(password)}")
|
|
.SetParameter(nameof(username), username)
|
|
.SetParameter(nameof(password), password)
|
|
.FirstOrDefault(reader => reader.GetValue<Int16>(0));
|
|
|
|
public Int32 ResetPassword(String username)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
UPDATE [Mitarbeiter]
|
|
SET [PasswordHash] = NULL
|
|
WHERE [Benutzername] = @{nameof(username)}")
|
|
.SetParameter(nameof(username), username)
|
|
.ExecuteNonQuery();
|
|
|
|
public Int32 ResetPassword(String username, String password)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
UPDATE [Mitarbeiter]
|
|
SET [PasswordHash] = @{nameof(password)}
|
|
WHERE [Benutzername] = @{nameof(username)}")
|
|
.SetParameter(nameof(username), username)
|
|
.SetParameter(nameof(password), password)
|
|
.ExecuteNonQuery();
|
|
}
|
|
|
|
public static class AccountExtensions
|
|
{
|
|
public static String ComputeHash(this HashAlgorithm hashAlgorithm, String value)
|
|
{
|
|
if (!String.IsNullOrWhiteSpace(value))
|
|
{
|
|
var bytes = Encoding.UTF8.GetBytes(value);
|
|
var hash = hashAlgorithm.ComputeHash(bytes);
|
|
var base64 = Convert.ToBase64String(hash);
|
|
|
|
return base64;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
public static String ComputeHash(this String value)
|
|
=> SHA256.Create().ComputeHash(value);
|
|
}
|
|
}
|