169 lines
5.8 KiB
C#
169 lines
5.8 KiB
C#
using BMA.EHR.Application.Common.Interfaces;
|
|
using BMA.EHR.Application.Responses;
|
|
using BMA.EHR.Domain.Models.MetaData;
|
|
using BMA.EHR.Domain.Models.Organizations;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Newtonsoft.Json.Linq;
|
|
using Newtonsoft.Json;
|
|
using System.Net.Http.Headers;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System.Security.Claims;
|
|
using System.Net.Http.Json;
|
|
using BMA.EHR.Application.Responses.Leaves;
|
|
|
|
namespace BMA.EHR.Application.Repositories
|
|
{
|
|
public class PermissionRepository
|
|
{
|
|
#region " Fields "
|
|
|
|
private readonly IApplicationDBContext _dbContext;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
private readonly IConfiguration _configuration;
|
|
|
|
#endregion
|
|
|
|
#region " Constructor and Destuctor "
|
|
|
|
public PermissionRepository(IApplicationDBContext dbContext,
|
|
IHttpContextAccessor httpContextAccessor,
|
|
IConfiguration configuration)
|
|
{
|
|
_dbContext = dbContext;
|
|
_httpContextAccessor = httpContextAccessor;
|
|
_configuration = configuration;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region " Properties "
|
|
|
|
//protected string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
|
|
|
//protected string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
|
|
|
|
//protected bool? IsPlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1");
|
|
|
|
protected string? AccessToken => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"];
|
|
|
|
#endregion
|
|
|
|
#region " Methods "
|
|
|
|
public async Task<dynamic> GetPermissionAPIAsync(string action, string system)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/permission/dotnet/{action}/{system}";
|
|
|
|
using (var client = new HttpClient())
|
|
{
|
|
client.DefaultRequestHeaders.Authorization =
|
|
new AuthenticationHeaderValue("Bearer", AccessToken.Replace("Bearer ", ""));
|
|
client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]);
|
|
var req = await client.GetAsync(apiPath);
|
|
if (!req.IsSuccessStatusCode)
|
|
{
|
|
throw new Exception("Error calling permission API");
|
|
}
|
|
var res = await req.Content.ReadAsStringAsync();
|
|
return res;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<GetPermissionWithActingResultDto?> GetPermissionWithActingAPIAsync(string action, string system)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/permission/dotnet-acting/{action}/{system}";
|
|
|
|
using (var client = new HttpClient())
|
|
{
|
|
client.DefaultRequestHeaders.Authorization =
|
|
new AuthenticationHeaderValue("Bearer", AccessToken.Replace("Bearer ", ""));
|
|
client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]);
|
|
var req = await client.GetAsync(apiPath);
|
|
if (!req.IsSuccessStatusCode)
|
|
{
|
|
throw new Exception("Error calling permission API");
|
|
}
|
|
var apiResult = await req.Content.ReadAsStringAsync();
|
|
//return res;
|
|
|
|
if (apiResult != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetPermissionWithActingResultDto>(apiResult);
|
|
return raw;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<dynamic> GetPermissionOrgAPIAsync(string action, string system, string profileId)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/permission/dotnet-org/{action}/{system}/{profileId}";
|
|
|
|
using (var client = new HttpClient())
|
|
{
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken.Replace("Bearer ", ""));
|
|
client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]);
|
|
var req = await client.GetAsync(apiPath);
|
|
var res = await req.Content.ReadAsStringAsync();
|
|
return res;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<dynamic> GetPermissionAPIWorkflowAsync(string refId, string sysName)
|
|
{
|
|
try
|
|
{
|
|
var apiPath = $"{_configuration["API"]}/org/workflow/keycloak/isofficer";
|
|
|
|
using (var client = new HttpClient())
|
|
{
|
|
var res = await client.PostAsJsonAsync(apiPath, new
|
|
{
|
|
data = new
|
|
{
|
|
refId = refId,
|
|
sysName = sysName,
|
|
},
|
|
});
|
|
var result = await res.Content.ReadAsStringAsync();
|
|
if (res.IsSuccessStatusCode)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|