226 lines
7.1 KiB
C#
226 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 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(IEmployeesRepository employeesRepository, ISoftwareRepository softwareRepository)
|
|
{
|
|
this.employeesRepository = employeesRepository;
|
|
this.softwareRepository = softwareRepository;
|
|
}
|
|
|
|
public Boolean 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 Boolean? 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(Int16 employeeId)
|
|
{
|
|
this.employeesRepository.ResetPassword(employeeId);
|
|
this.employeesRepository.ResetTokens(employeeId);
|
|
}
|
|
|
|
public void SetPassword(Int16 employeeId, String password)
|
|
=> this.employeesRepository.SetPassword(employeeId, password);
|
|
|
|
private Int16 GetOrCreateSoftwareId(String assembly, Int32 major, Int32 minor, Int32 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(Int16 appId, Int16 userId)
|
|
{
|
|
var token = default(String);
|
|
|
|
if (appId > 0 && userId > 0)
|
|
{
|
|
this.employeesRepository.CreateToken(appId, userId);
|
|
|
|
token = this.employeesRepository.GetToken(appId, userId);
|
|
}
|
|
|
|
return token;
|
|
}
|
|
|
|
public Boolean? 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;
|
|
}
|
|
}
|
|
}
|