Files
Backend-Api/ServiceHost/Areas/AdminNew/Pages/Index.cshtml.cs
2025-12-08 16:36:35 +03:30

176 lines
6.9 KiB
C#

using _0_Framework.Application;
using AccountManagement.Application;
using AccountManagement.Application.Contracts.Task;
using AccountManagement.Application.Contracts.Ticket;
using backService;
using Company.Domain.WorkshopAccountAgg;
using CompanyManagment.App.Contracts.Workshop;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using WorkFlow.Application.Contracts.AdminWorkFlow;
namespace ServiceHost.Areas.AdminNew.Pages
{
public class IndexModel : PageModel
{
private readonly IConfiguration _configuration;
private readonly IAuthHelper _authHelper;
private readonly IWorkshopAccountRepository _workshopAccountRepository;
private readonly IAdminWorkFlowApplication _adminWorkFlowApplication;
private readonly ITicketApplication _ticketApplication;
private readonly ITaskApplication _taskApplication;
private long _roleId;
public List<BackupViewModel> DbBackupList { get; set; }
public List<BackupViewModel> InsuranceBackupList { get; set; }
public IndexModel(IWebHostEnvironment webHostEnvironment, IConfiguration configuration, IAuthHelper authHelper, IWorkshopAccountRepository workshopAccountRepository, IAdminWorkFlowApplication adminWorkFlowApplication, ITicketApplication ticketApplication, ITaskApplication taskApplication)
{
_configuration = configuration;
_authHelper = authHelper;
_workshopAccountRepository = workshopAccountRepository;
_ticketApplication = ticketApplication;
_taskApplication = taskApplication;
_adminWorkFlowApplication = adminWorkFlowApplication;
_roleId = authHelper.CurrentAccountInfo().RoleId;
}
public void OnGet()
{
#region DbBackupLoad
string? dbZipPath = _configuration["BackupOptions:DbBackupZipPath"];
string[] dbBackups = Directory.GetFiles(dbZipPath);
DbBackupList = dbBackups.Select(x => new BackupViewModel()
{
FileName = Path.GetFileName(x),
FullPath = x,
CreationDate = Path.GetFileName(x).ExtractTimeFromDbbackup(),
}).OrderByDescending(x => x.CreationDate).ToList();
#endregion
#region InsurancBackupLoad
string? insuranceZipPath = _configuration["BackupOptions:InsuranceListZipPath"];
string[] insuranceBackups = Directory.GetFiles(insuranceZipPath);
InsuranceBackupList = insuranceBackups.Select(x => new BackupViewModel()
{
FileName = Path.GetFileName(x),
FullPath = x,
CreationDate = Path.GetFileName(x).ExtractTimeFromInsurancebackup(),
}).OrderByDescending(x => x.CreationDate).ToList();
#endregion
}
public IActionResult OnGetDownloadFile(string path, string fileName)
{
byte[] fileContent = System.IO.File.ReadAllBytes(path);
return File(fileContent, "application/zip", fileName);
}
public async Task<IActionResult> OnGetLayoutCountTask()
{
var currentAccountId = _authHelper.CurrentAccountId();
int taskCount = await _taskApplication.RequestedAndOverdueTasksCount(currentAccountId);
return new JsonResult(new
{
success = true,
data = taskCount
});
}
public IActionResult OnGetLayoutCountTicket()
{
int ticketCount = _ticketApplication.GetAdminTicketsCount();
return new JsonResult(new
{
success = true,
data = ticketCount
});
}
public async Task<IActionResult> OnGetLayoutCountWorkFlow()
{
var currentAccountId = _authHelper.CurrentAccountId();
var accountWorkshops = _workshopAccountRepository.GetList(currentAccountId).Select(x => x.WorkshopId).ToList();
var permissions = _authHelper.GetPermissions();
int workFlowCount = await _adminWorkFlowApplication.GetWorkFlowCountsForAdmin(accountWorkshops,currentAccountId, _roleId,permissions);
return new JsonResult(new
{
success = true,
data = workFlowCount
});
}
public async Task<IActionResult> OnGetLayoutCountChecker()
{
int checkerCount = await _adminWorkFlowApplication.GetWorkFlowCountForChecker();
return new JsonResult(new
{
success = true,
data = checkerCount,
});
}
public IActionResult OnGetProgramManager()
{
try
{
// دریافت اطلاعات کاربر فعلی
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);
var domain = _configuration["Domain"] ?? "http://localhost:5000";
// Redirect به SSO-Login با Token
var ssoUrl = $"https://pm{domain}/ssoLogin?token={Uri.EscapeDataString(tokenString)}";
// var ssoUrl = $"https://localhost:7032/api/Auth/sso-login?token={Uri.EscapeDataString(tokenString)}";
return Redirect(ssoUrl);
}
catch (Exception ex)
{
// در صورت خطا، برگشت به صفحه اصلی
return RedirectToPage("/Index");
}
}
}
}