51 lines
1.6 KiB
C#
51 lines
1.6 KiB
C#
using Microsoft.AspNetCore.Components.Authorization;
|
|
using System.Reflection;
|
|
using ReserveManagementSystemUI.Model.Configuration;
|
|
using Serilog;
|
|
|
|
namespace ReserveManagementSystemUI.Helpers
|
|
{
|
|
public class ServiceHelpers
|
|
{
|
|
private AuthenticationStateProvider _authenticationStateProvider;
|
|
private readonly RmsUIConfiguration _rmsUIConfig;
|
|
|
|
public ServiceHelpers(RmsUIConfiguration rmsUIConfig, AuthenticationStateProvider authenticationStateProvider)
|
|
{
|
|
_rmsUIConfig = rmsUIConfig;
|
|
_authenticationStateProvider = authenticationStateProvider;
|
|
}
|
|
|
|
public string GetControllerApiUrl(string apiUrl, string controller)
|
|
{
|
|
PropertyInfo propInfo = typeof(RmsUIConfiguration).GetProperty(controller);
|
|
var propValue = propInfo.GetValue(_rmsUIConfig, null);
|
|
|
|
return _rmsUIConfig.RmsApiUrl + propValue;
|
|
}
|
|
|
|
public string GetReportServerBaseUrl()
|
|
{
|
|
return _rmsUIConfig.ReportServerUrl;
|
|
}
|
|
|
|
public string GetUserName()
|
|
{
|
|
var authState = _authenticationStateProvider.GetAuthenticationStateAsync().Result;
|
|
|
|
if (authState?.User?.Identity?.Name == null)
|
|
{
|
|
Log.Error("Could not get user identity by AuthenticationState");
|
|
return "";
|
|
}
|
|
|
|
var userName = authState.User.Identity.Name;
|
|
var slashIndex = userName.IndexOf('\\');
|
|
|
|
return slashIndex >= 0 && slashIndex < userName.Length - 1
|
|
? userName[(slashIndex + 1)..]
|
|
: userName;
|
|
}
|
|
}
|
|
}
|