laatzen/Common/Ui/GenesisToolBox/Infrastructure/Software.cs

145 lines
5.0 KiB
C#

namespace Xylem.Common.Ui.GenesisToolBox.Infrastructure
{
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using CommonCore.Configuration;
using Logic.SoftwareAccessHelper;
/// <summary>
/// Holds information over current software and logged in user.
/// </summary>
internal static class Software
{
private static String bearer;
private static IEnumerable<SoftwareFunctions> softwareFunctions = Array.Empty<SoftwareFunctions>();
public static String UserName;
private static Boolean HasUser => !string.IsNullOrWhiteSpace(bearer);
public static void Initialize()
{
var version = SoftwareVersion.Current;
var user = new SoftwareUser();
Login(new ADLoginModel
{
Assembly = version.AssemblyName.Name,
Email = user.Email,
EmployeeId = user.EmployeeId,
FirstName = user.FirstName,
LastName = user.LastName,
Phone = user.Phone,
Username = user.Username,
MajorV = version.MajorV,
MinorV = version.MinorV,
BuildV = version.BuildV,
});
}
/// <summary>
/// Sends login request and obtains an bearer authorization token.
/// </summary>
/// <param name="model"><see cref="ADLoginModel"/> - containing all software and user info required for authentication.</param>
public static void Login(ADLoginModel model)
{
var authenticationResponse = LocalWebRequest.PostRequestAndGetResponseAsync(
url: $"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Login/AD",
json: model);
bearer = authenticationResponse.Content;
softwareFunctions = GetFunctions();
UserName = "Options";
if (HasUser)
{
UserName = $"{model.FirstName} {model.LastName}";
}
}
/// <summary>
/// Sends login request and obtains an bearer authorization token.
/// </summary>
public static Boolean Login(String username, String password)
{
var authenticationResponse = LocalWebRequest.PostRequestAndGetResponseAsync(
url: $"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Login/Local",
json: new UserLoginModel
{
Username = username,
Password = password,
Assembly = SoftwareVersion.Current.Name,
MajorV = SoftwareVersion.Current.MajorV,
MinorV = SoftwareVersion.Current.MinorV,
BuildV = SoftwareVersion.Current.BuildV,
});
bearer = authenticationResponse.Content;
softwareFunctions = GetFunctions();
UserName = "Options";
if (HasUser)
{
try
{
UserName = LocalWebRequest
.GetRequest(
timeoutMs: 1200,
url: $"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Personalization/Login",
header: new Dictionary<String, String> { { "Authorization", $"Bearer {bearer}" } })
.Trim('"');
}
catch (Exception )
{
// ignored
}
}
return HasUser;
}
public static IEnumerable<SoftwareFunctions> GetFunctions()
{
if (string.IsNullOrWhiteSpace(bearer))
{
return Array.Empty<SoftwareFunctions>();
}
try
{
var functionsResponse = LocalWebRequest.GetRequest(
timeoutMs: 1200,
url: $"{ServiceUrls.LaaProductionAPI}/LaaProductionWeb/API/Personalization/Software/Access",
header: new Dictionary<String, String> { { "Authorization", $"Bearer {bearer}" } });
return JsonConvert
.DeserializeObject<IEnumerable<String>>(functionsResponse)
?.Select(x => Enum.TryParse($"{x}", out SoftwareFunctions f) ? f : default(SoftwareFunctions))
?? Array.Empty<SoftwareFunctions>();
}
catch (Exception e)
{
Console.WriteLine(e);
return Array.Empty<SoftwareFunctions>();
}
}
public static Boolean IsEnabled(SoftwareFunctions softwareFunction)
=> softwareFunction != SoftwareFunctions.NONE && softwareFunctions.Contains(softwareFunction);
public static void Logout()
{
UserName = "Options";
bearer = default(String);
softwareFunctions = Array.Empty<SoftwareFunctions>();
Initialize();
}
}
}