104 lines
4.3 KiB
C#
104 lines
4.3 KiB
C#
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
using System.Text;
|
|
using _0_Framework.Application;
|
|
using AccountManagement.Application.Contracts.Account;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using ServiceHost.BaseControllers;
|
|
|
|
namespace ServiceHost.Areas.Admin.Controllers;
|
|
|
|
public class AccountController : AdminBaseController
|
|
{
|
|
private readonly IAccountApplication _accountApplication;
|
|
private readonly IConfiguration _configuration;
|
|
private readonly IAuthHelper _authHelper;
|
|
|
|
|
|
public AccountController(IAccountApplication accountApplication, IConfiguration configuration,
|
|
IAuthHelper authHelper)
|
|
{
|
|
_accountApplication = accountApplication;
|
|
_configuration = configuration;
|
|
_authHelper = authHelper;
|
|
}
|
|
|
|
/// <summary>
|
|
/// سلکت لیست اکانت های ادمین برای جستجو
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("select_list")]
|
|
public async Task<ActionResult<List<AccountSelectListViewModel>>> GetAdminAccountsSelectList()
|
|
{
|
|
var res = await _accountApplication.GetAdminSelectList();
|
|
return res;
|
|
}
|
|
|
|
[HttpGet("admins-by-role")]
|
|
public ActionResult<AdminAccountByRoleResponse> GetAdminAccountsByRole()
|
|
{
|
|
var adminAccounts = _accountApplication.GetAdminAccountsNew().Select(x => new AccountSelectListViewModel()
|
|
{
|
|
Id = x.Id,
|
|
Name = x.Fullname,
|
|
RoleId = x.RoleId
|
|
}).ToList();
|
|
|
|
var seniorContractAccountsList = adminAccounts.Where(x => x.RoleId == 3).ToList();
|
|
var juniorContractAccountsList = adminAccounts.Where(x => x.RoleId == 5).ToList();
|
|
var seniorInsuranceAccountList = adminAccounts.Where(x => x.RoleId == 7).ToList();
|
|
var juniorInsuranceAccountsList = adminAccounts.Where(x => x.RoleId == 8).ToList();
|
|
return new AdminAccountByRoleResponse(seniorContractAccountsList, juniorContractAccountsList,
|
|
seniorInsuranceAccountList, juniorInsuranceAccountsList);
|
|
}
|
|
|
|
[HttpGet("pm-sso-token")]
|
|
public IActionResult ProgramManagerSSOtoken()
|
|
{
|
|
var envName = _configuration["ASPNETCORE_ENVIRONMENT"] ?? _configuration["Environment"] ?? "Production";
|
|
var isDevelopment = string.Equals(envName, "Development", System.StringComparison.OrdinalIgnoreCase) ||
|
|
string.Equals(envName, "Dev", System.StringComparison.OrdinalIgnoreCase);
|
|
if (!isDevelopment)
|
|
{
|
|
return NotFound();
|
|
}
|
|
// دریافت اطلاعات کاربر فعلی
|
|
var currentAccountId = _authHelper.CurrentAccountId();
|
|
var accountInfo = _authHelper.CurrentAccountInfo();
|
|
|
|
// تعریف Secret Key برای JWT (باید در appsettings.json تعریف شود)
|
|
var secretKey = _configuration["JwtSettings:SecretKey"] ??
|
|
">3£>^1UBG@yw)QdhRC3$£:;r8~?qpp^oKK4D3a~8L2>enF;lkgh";
|
|
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));
|
|
var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
|
|
|
// ایجاد Claims
|
|
var claims = new[]
|
|
{
|
|
new Claim(ClaimTypes.NameIdentifier, currentAccountId.ToString()),
|
|
new Claim(ClaimTypes.Name, accountInfo.Fullname ?? ""),
|
|
new Claim(ClaimTypes.Email, accountInfo.Username ?? ""),
|
|
new Claim("AccountId", currentAccountId.ToString()),
|
|
new Claim("RoleId", accountInfo.RoleId.ToString())
|
|
};
|
|
|
|
// ایجاد JWT Token
|
|
var token = new JwtSecurityToken(
|
|
issuer: _configuration["JwtSettings:Issuer"] ?? "GozareshgirApp",
|
|
audience: _configuration["JwtSettings:Audience"] ?? "GozareshgirUsers",
|
|
claims: claims,
|
|
expires: DateTime.UtcNow.AddMinutes(int.Parse(_configuration["JwtSettings:ExpirationMinutes"] ?? "30")),
|
|
signingCredentials: credentials
|
|
);
|
|
|
|
var tokenString = new JwtSecurityTokenHandler().WriteToken(token);
|
|
return Ok(tokenString);
|
|
}
|
|
}
|
|
|
|
public record AdminAccountByRoleResponse(
|
|
List<AccountSelectListViewModel> SeniorContractAccountsList,
|
|
List<AccountSelectListViewModel> JuniorContractAccountsList,
|
|
List<AccountSelectListViewModel> SeniorInsuranceAccountList,
|
|
List<AccountSelectListViewModel> JuniorInsuranceAccountsList); |