162 lines
6.0 KiB
C#
162 lines
6.0 KiB
C#
using CPRNIMS.Core.Facades;
|
|
using CPRNIMS.Infrastructure.Constant;
|
|
using CPRNIMS.Infrastructure.Helper;
|
|
using CPRNIMS.Infrastructure.ViewModel;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Security.Claims;
|
|
|
|
namespace CPRNIMS.WebApps.Controllers.Base
|
|
{
|
|
public abstract class BaseMethod : BaseProperties
|
|
{
|
|
protected readonly ErrorLogHelper ErrorMessageService;
|
|
protected readonly IWebHostEnvironment WebHostEnvironment;
|
|
protected readonly Infrastructure.Helper.TokenHelper TokenHelper;
|
|
|
|
protected BaseMethod(
|
|
ErrorLogHelper errorMessageService,
|
|
IWebHostEnvironment webHostEnvironment,
|
|
Infrastructure.Helper.TokenHelper tokenHelper)
|
|
{
|
|
ErrorMessageService = errorMessageService;
|
|
WebHostEnvironment = webHostEnvironment;
|
|
TokenHelper = tokenHelper;
|
|
}
|
|
|
|
protected Infrastructure.Models.Account.User GetUser()
|
|
{
|
|
if (!User.Identity?.IsAuthenticated ?? true)
|
|
return null;
|
|
|
|
var roles = User.FindAll(ClaimTypes.Role).Select(r => r.Value).ToList();
|
|
|
|
UserRoles = roles.Any() ? string.Join(",", roles) : null;
|
|
|
|
return new Infrastructure.Models.Account.User
|
|
{
|
|
UserId = User.FindFirstValue(ClaimTypes.NameIdentifier),
|
|
UserName = User.Identity?.Name,
|
|
FullName = User.FindFirst("FullName")?.Value,
|
|
Company = User.FindFirst("Company")?.Value,
|
|
MyAccess = UserRoles,
|
|
URLAttachment = User.FindFirst("URLAttachment")?.Value
|
|
};
|
|
}
|
|
protected async Task<string> GetValidTokenAsync()
|
|
{
|
|
var token = await TokenHelper.GetValidTokenAsync();
|
|
|
|
if (string.IsNullOrEmpty(token))
|
|
{
|
|
// Token refresh failed, user needs to re-login
|
|
await HttpContext.SignOutAsync(
|
|
CookieAuthenticationDefaults.AuthenticationScheme);
|
|
return null;
|
|
}
|
|
|
|
return token;
|
|
}
|
|
protected Dictionary<string, string> GetTokenClaims()
|
|
{
|
|
return TokenHelper.GetStoredClaims();
|
|
}
|
|
|
|
protected async Task<IActionResult> IsAuthenTicated()
|
|
{
|
|
if (!User.Identity.IsAuthenticated)
|
|
return RedirectToAction("Index", "Home");
|
|
|
|
// Ensure token is still valid
|
|
var token = await GetValidTokenAsync();
|
|
if (string.IsNullOrEmpty(token))
|
|
return RedirectToAction("Index", "Home");
|
|
|
|
PopulateViewBagFromClaims();
|
|
return View();
|
|
}
|
|
|
|
protected void PopulateViewBagFromClaims()
|
|
{
|
|
if (!User.Identity?.IsAuthenticated ?? true)
|
|
return;
|
|
|
|
ViewBag.UserId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
ViewBag.UserName = User.Identity?.Name;
|
|
ViewBag.FullName = User.FindFirst("FullName")?.Value;
|
|
ViewBag.UserCompany = User.FindFirst("Company")?.Value;
|
|
ViewBag.UserRoles = string.Join(",",
|
|
User.FindAll(ClaimTypes.Role).Select(c => c.Value));
|
|
ViewBag.URLAttachment = User.FindFirst("URLAttachment")?.Value ?? "Content/Images/UserProfile/404userImage.jpg";
|
|
}
|
|
|
|
protected IActionResult GetResponse<T>(T response)
|
|
{
|
|
return Json(new
|
|
{
|
|
success = response != null,
|
|
data = response ?? Activator.CreateInstance<T>()
|
|
});
|
|
}
|
|
|
|
protected string ResolveProfileImage(string urlAttachment)
|
|
{
|
|
return string.IsNullOrWhiteSpace(urlAttachment)
|
|
? "Content/Images/UserProfile/404userImage.jpg"
|
|
: urlAttachment;
|
|
}
|
|
public void GetStoreAttachment(string urlContent, bool isNull)
|
|
{
|
|
if (!String.IsNullOrEmpty(urlContent) && isNull == true)
|
|
{
|
|
HttpContext.Session.SetString("URLAttachment", urlContent);
|
|
ViewBag.URLAttachment = urlContent;
|
|
TempData["URLAttachment"] = urlContent;
|
|
}
|
|
else
|
|
{
|
|
HttpContext.Session.SetString("URLAttachment", "Content\\Images\\UserProfile\\404userImage.jpg");
|
|
URLAttachment = HttpContext.Session.GetString("URLAttachment");
|
|
ViewBag.URLAttachment = HttpContext.Session.GetString("URLAttachment");
|
|
}
|
|
}
|
|
protected AttachmentVM CreateUpdateAttachment(string contentValueBytes)
|
|
{
|
|
var base64Image = contentValueBytes.Split(',')[1];
|
|
byte[] contentBytes = Convert.FromBase64String(base64Image);
|
|
|
|
var facadeAttachment = new FacadeAttachment();
|
|
|
|
var (imageFormat, imageEncoder, imageResult) =
|
|
facadeAttachment.GetImageFormatAndEncoder(contentValueBytes);
|
|
|
|
if (imageResult != "Format is valid")
|
|
return new AttachmentVM { Result = imageResult };
|
|
|
|
var (isValid, isValidResult) =
|
|
facadeAttachment.CheckFileSize(contentBytes, 2 * 1024 * 1024);
|
|
|
|
if (!isValid)
|
|
return new AttachmentVM { Result = isValidResult };
|
|
|
|
var fileName = $"{Guid.NewGuid()}.{imageFormat.Name.ToLower()}";
|
|
var filePath = Path.Combine(
|
|
WebHostEnvironment.WebRootPath,
|
|
FileExtensionPath.GetExtensionPath(imageFormat.Name.ToLower()),
|
|
fileName);
|
|
|
|
var relativePath =
|
|
Path.GetRelativePath(WebHostEnvironment.WebRootPath, filePath);
|
|
|
|
return facadeAttachment.SaveAttachment(
|
|
contentBytes,
|
|
relativePath,
|
|
imageEncoder,
|
|
fileName,
|
|
imageFormat.Name.ToLower() == "png"
|
|
? FileExtension.Png
|
|
: FileExtension.Jpeg);
|
|
}
|
|
}
|
|
} |