laatzen/LaaProductionWeb/LaaProduction.Personalization/AccountManager.cs
2024-03-04 14:32:01 +01:00

227 lines
7.1 KiB
C#

namespace LaaProduction.Personalization
{
using LaaProduction.Personalization.Interfaces;
using LaaProduction.Personalization.Models;
using LaaProduction.Personalization.Repositories.Interfaces;
using LaaProduction.SharedModels;
using LaaProductionDI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Security.Principal;
public class AccountManager : IAccountManager
{
private static readonly IIdentity anonymous
= new ClaimsIdentity(
claims: Array.Empty<Claim>(),
authenticationType: string.Empty,
nameType: string.Empty,
roleType: string.Empty);
private readonly IEmployeesRepository employeesRepository;
private readonly ISoftwareRepository softwareRepository;
public AccountManager()
{
this.employeesRepository = LaaServiceProvider.GetService<IEmployeesRepository>();
this.softwareRepository = LaaServiceProvider.GetService<ISoftwareRepository>();
}
public bool ChangePassword(string password, string bearer)
{
var tokenData = this.employeesRepository.GetTokenData(bearer);
if (tokenData is null)
{
return false;
}
return this.employeesRepository.SetPassword(tokenData.EmployeeId, password);
}
public IPrincipal GetClaimsPrincipal(string token)
{
var claimsIdentity = anonymous;
var tokenData = this.employeesRepository.GetTokenData(token);
if (tokenData != null)
{
var employee = this.employeesRepository.Find(tokenData.EmployeeId);
var claims = new List<Claim>
{
new Claim(nameof(EmployeeToken.AppId), tokenData.AppId.ToString()),
new Claim(nameof(Employee.EmployeeId), employee.EmployeeId.ToString()),
new Claim(nameof(Employee.FullName), employee.FullName),
new Claim(nameof(Employee.Username), employee.Username),
new Claim(ClaimTypes.NameIdentifier, token),
};
var userRoles = this.employeesRepository
.GetUserRoles(token)
.Intersect(Enum.GetNames(typeof(EmployeesRoles)));
foreach (var role in userRoles)
{
claims.Add(new Claim(nameof(EmployeesRoles), role));
}
var functions = this.softwareRepository
.GetSoftwareFunctions(token)
.Intersect(Enum.GetNames(typeof(SoftwareFunctions)));
foreach (var func in functions)
{
claims.Add(new Claim(nameof(SoftwareFunctions), func));
}
claimsIdentity = new ClaimsIdentity(
claims: claims,
authenticationType: nameof(ClaimsIdentity),
nameType: nameof(Employee.EmployeeId),
roleType: nameof(EmployeesRoles));
}
return new ClaimsPrincipal(claimsIdentity);
}
public bool? Login(LDAPLoginModel model, out string bearer)
{
bearer = default(string);
var softwareId = this.GetOrCreateSoftwareId(
model.Assembly,
model.MajorV,
model.MinorV,
model.BuildV);
if (softwareId <= 0)
{
return false;
}
var employeeNr = this.employeesRepository.FindEmployeeNr(model.EmployeeId);
if (employeeNr <= 0)
{
return null;
}
bearer = this.GetOrCreateAuthToken(softwareId, employeeNr);
return true;
}
public string Login(UserLoginModel model)
{
var softwareId = this.GetOrCreateSoftwareId(
model.Assembly,
model.MajorV,
model.MinorV,
model.BuildV);
if (softwareId <= 0)
{
return default(string);
}
var employeeNr = this.employeesRepository.FindEmployeeNr(model.Username, model.Password);
if (employeeNr <= 0)
{
return default(string);
}
return this.GetOrCreateAuthToken(softwareId, employeeNr);
}
public void ResetPassword(short employeeId)
{
this.employeesRepository.ResetPassword(employeeId);
this.employeesRepository.ResetTokens(employeeId);
}
public void SetPassword(short employeeId, string password)
=> this.employeesRepository.SetPassword(employeeId, password);
private short GetOrCreateSoftwareId(string assembly, int major, int minor, int build)
{
/// Gets the app id for provided assembly name
var softwareId = this.softwareRepository.SoftwareId(assembly);
/// In case that the software dose not exists wird hinzugefügt.
if (softwareId <= 0)
{
softwareId = this.softwareRepository.Insert(assembly, major, minor, build);
}
/// softwareId = [VersionSoftwareAccess].[AppID]
return softwareId;
}
public string GetOrCreateAuthToken(short appId, short userId)
{
var token = default(string);
if (appId > 0 && userId > 0)
{
this.employeesRepository.CreateToken(appId, userId);
token = this.employeesRepository.GetToken(appId, userId);
}
return token;
}
public bool? Register(LDAPRegisterModel model, out string bearer)
{
bearer = default(string);
var softwareId = this.GetOrCreateSoftwareId(
model.Assembly,
model.MajorV,
model.MinorV,
model.BuildV);
if (softwareId <= 0)
{
return false;
}
var employeeNr = this.employeesRepository.Update(
model.EmployeeId,
model.FirstName,
model.LastName,
model.Phone,
model.Email,
model.Username,
model.DomainName,
model.Password);
if (employeeNr < 0)
{
employeeNr = this.employeesRepository.Insert(
model.EmployeeId,
model.FirstName,
model.LastName,
model.Phone,
model.Email,
model.Username,
model.DomainName,
model.Password);
}
if (employeeNr <= 0)
{
return false;
}
bearer = this.GetOrCreateAuthToken(softwareId, employeeNr);
return true;
}
}
}