182 lines
5.8 KiB
C#
182 lines
5.8 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 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 string Login(ADLoginModel model)
|
|
{
|
|
var softwareId = this.GetOrCreateSoftwareId(
|
|
model.Assembly,
|
|
model.MajorV,
|
|
model.MinorV,
|
|
model.BuildV);
|
|
|
|
if (softwareId <= 0)
|
|
{
|
|
return default(string);
|
|
}
|
|
|
|
var employeeNr = this.employeesRepository.EmployeeId(model.EmployeeId);
|
|
|
|
if (employeeNr <= 0)
|
|
{
|
|
employeeNr = this.employeesRepository.Update(
|
|
model.EmployeeId,
|
|
model.FirstName,
|
|
model.LastName,
|
|
model.Phone,
|
|
model.Username,
|
|
model.Email);
|
|
|
|
if (employeeNr <= 0)
|
|
{
|
|
employeeNr = this.employeesRepository.Insert(
|
|
model.EmployeeId,
|
|
model.FirstName,
|
|
model.LastName,
|
|
model.Phone,
|
|
model.Username,
|
|
model.Email);
|
|
}
|
|
}
|
|
|
|
if (employeeNr <= 0)
|
|
{
|
|
return default(string);
|
|
}
|
|
|
|
return this.GetOrCreateAuthToken(softwareId, employeeNr);
|
|
}
|
|
|
|
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.EmployeeId(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)
|
|
{
|
|
var softwareId = this.softwareRepository.SoftwareId(assembly);
|
|
|
|
if (softwareId <= 0)
|
|
{
|
|
softwareId = this.softwareRepository.Insert(assembly, major, minor, build);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|