155 lines
6.4 KiB
C#
155 lines
6.4 KiB
C#
namespace LaaProduction.Personalization.Repositories
|
|
{
|
|
using LaaProduction.Personalization.Models;
|
|
using LaaProduction.Personalization.Repositories.Interfaces;
|
|
|
|
using LaaProductionSQL.Interfaces;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
internal class EmployeesRepository : IEmployeesRepository
|
|
{
|
|
private readonly ISQLConnection sqlConnection;
|
|
|
|
public EmployeesRepository(ISQLConnection sqlConnection)
|
|
=> this.sqlConnection = sqlConnection;
|
|
|
|
public IEnumerable<Employee> All()
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
SELECT [M].[MitarbeiterNr] AS [EmployeeId]
|
|
, [M].[Vorname] + ' ' + [M].[Name] AS [FullName]
|
|
, [M].[Benutzername] AS [Username]
|
|
FROM [Mitarbeiter] AS [M]
|
|
ORDER BY [FullName], [Username]")
|
|
.ExecuteReader(x => new Employee
|
|
{
|
|
EmployeeId = x.GetSmallint(),
|
|
FullName = x.GetString(),
|
|
Username = x.GetString(),
|
|
});
|
|
|
|
public void CreateToken(short appId, short userId)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
INSERT INTO [MitarbeiterTokens] (
|
|
[AppId]
|
|
, [UserId])
|
|
VALUES (@{nameof(appId)}
|
|
, @{nameof(userId)})")
|
|
.SetParameter(nameof(appId), appId)
|
|
.SetParameter(nameof(userId), userId)
|
|
.ExecuteNonQuery();
|
|
|
|
public short EmployeeId(string username, string password)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
SELECT [MitarbeiterNr]
|
|
FROM [Mitarbeiter]
|
|
WHERE [Benutzername] = @{nameof(username)}
|
|
AND [PasswordHash] = @{nameof(password)}")
|
|
.SetParameter(nameof(username), username)
|
|
.SetParameter(nameof(password), password)
|
|
.FirstOrDefault(x => x.GetSmallint());
|
|
|
|
public Employee Find(short employeeId)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
SELECT [M].[MitarbeiterNr] AS [EmployeeId]
|
|
, [M].[Vorname] + ' ' + [M].[Name] AS [FullName]
|
|
, [M].[Benutzername] AS [Username]
|
|
, [M].[PasswordHash] AS [Password]
|
|
FROM [Mitarbeiter] AS [M]
|
|
WHERE [M].[MitarbeiterNr] = @{nameof(employeeId)}")
|
|
.SetParameter(nameof(employeeId), employeeId)
|
|
.FirstOrDefault(x => new Employee
|
|
{
|
|
EmployeeId = x.GetSmallint(),
|
|
FullName = x.GetString(),
|
|
Username = x.GetString(),
|
|
HasPassword = x.GetString() != null
|
|
});
|
|
|
|
public Employee GetEmployee(string token)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
SELECT [M].[MitarbeiterNr]
|
|
, [M].[Vorname] + ' ' + [M].[Name]
|
|
, [M].[Benutzername]
|
|
FROM [MitarbeiterTokens] AS [MT]
|
|
JOIN [Mitarbeiter] AS [M]
|
|
ON [M].[MitarbeiterNr] = [MT].[UserId]
|
|
WHERE [MT].[Token] = @{nameof(token)}")
|
|
.SetParameter(nameof(token), token)
|
|
.FirstOrDefault(x => new Employee
|
|
{
|
|
EmployeeId = x.GetSmallint(),
|
|
FullName = x.GetString(),
|
|
Username = x.GetString()
|
|
});
|
|
|
|
public string GetToken(short appId, short userId)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
SELECT [Token]
|
|
FROM [MitarbeiterTokens]
|
|
WHERE [AppId] = @{nameof(appId)}
|
|
AND [UserId] = @{nameof(userId)}")
|
|
.SetParameter(nameof(appId), appId)
|
|
.SetParameter(nameof(userId), userId)
|
|
.FirstOrDefault(x => x.GetString());
|
|
|
|
public EmployeeToken GetTokenData(string token)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
SELECT [AppId]
|
|
, [UserId]
|
|
FROM [MitarbeiterTokens]
|
|
WHERE [Token] = @{nameof(token)}")
|
|
.SetParameter(nameof(token), token)
|
|
.FirstOrDefault(x => new EmployeeToken
|
|
{
|
|
AppId = x.GetSmallint(),
|
|
EmployeeId = x.GetSmallint()
|
|
});
|
|
|
|
public IEnumerable<string> GetUserRoles(string token)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
SELECT [MR].[Recht]
|
|
FROM [MitarbeiterTokens] AS [MT]
|
|
JOIN [MitarbeiterRechte] AS [MR]
|
|
ON [MR].[MitarbeiterNr] = [MT].[UserId]
|
|
WHERE [MT].[Token] = @{nameof(token)}")
|
|
.SetParameter(nameof(token), token)
|
|
.ExecuteReader(x => x.GetString());
|
|
|
|
public void ResetPassword(short employeeId)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
UPDATE [Mitarbeiter]
|
|
SET [PasswordHash] = NULL
|
|
WHERE [MitarbeiterNr] = @{nameof(employeeId)}")
|
|
.SetParameter(nameof(employeeId), employeeId)
|
|
.ExecuteNonQuery();
|
|
|
|
public void ResetTokens(short employeeId)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
DELETE FROM [MitarbeiterTokens]
|
|
WHERE [UserId] = @{nameof(employeeId)}")
|
|
.SetParameter(nameof(employeeId), employeeId)
|
|
.ExecuteNonQuery();
|
|
|
|
public void SetPassword(short employeeId, string password)
|
|
=> this.sqlConnection
|
|
.CreateCommand($@"
|
|
UPDATE [Mitarbeiter]
|
|
SET [PasswordHash] = @{nameof(password)}
|
|
WHERE [MitarbeiterNr] = @{nameof(employeeId)}")
|
|
.SetParameter(nameof(employeeId), employeeId)
|
|
.SetParameter(nameof(password), password)
|
|
.ExecuteNonQuery();
|
|
}
|
|
}
|