215 lines
8.2 KiB
C#
215 lines
8.2 KiB
C#
using CPRNIMS.Domain.Services;
|
|
using CPRNIMS.Domain.UIContracts.Account;
|
|
using CPRNIMS.Domain.UIContracts.Attachment;
|
|
using CPRNIMS.Domain.UIContracts.CaptCha;
|
|
using CPRNIMS.Infrastructure.Helper;
|
|
using CPRNIMS.Infrastructure.ViewModel.Account;
|
|
using CPRNIMS.WebApps.Controllers.Base;
|
|
using CPRNIMS.WebApps.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Diagnostics;
|
|
using System.Web;
|
|
using System.Drawing.Imaging;
|
|
using System.Drawing;
|
|
|
|
namespace CPRNIMS.WebApps.Controllers
|
|
{
|
|
public class HomeController : BaseMethod
|
|
{
|
|
private readonly ILogger<HomeController> _logger;
|
|
private readonly IAccount _account;
|
|
private readonly IAttachment _attachment;
|
|
private readonly ICaptchaService _captchaService;
|
|
public HomeController(TokenHelper tokenHelper,
|
|
ErrorLogHelper errorMessageService,
|
|
IWebHostEnvironment webHostEnvironment,
|
|
IAttachment attachment, IAccount account,
|
|
ICaptchaService captchaService) :
|
|
base(tokenHelper, errorMessageService, webHostEnvironment)
|
|
{
|
|
_account = account;
|
|
_attachment = attachment;
|
|
_captchaService = captchaService;
|
|
}
|
|
[HttpGet]
|
|
public IActionResult GetCaptcha()
|
|
{
|
|
var (code, image) = _captchaService.GenerateCaptcha();
|
|
HttpContext.Session.SetString("CaptchaCode", code);
|
|
return File(image, "image/png");
|
|
}
|
|
public IActionResult ValidateCaptcha(string captchaCode)
|
|
{
|
|
try
|
|
{
|
|
// Decode the captcha code
|
|
var decodedCaptcha = HttpUtility.UrlDecode(captchaCode);
|
|
|
|
// Get stored captcha from session
|
|
var storedCaptcha = HttpContext.Session.GetString("CaptchaCode");
|
|
|
|
if (string.IsNullOrEmpty(storedCaptcha))
|
|
{
|
|
return Json(new
|
|
{
|
|
success = false,
|
|
message = "CAPTCHA has expired. Please refresh and try again."
|
|
});
|
|
}
|
|
|
|
// Compare captcha (case-insensitive)
|
|
bool isValid = decodedCaptcha.Equals(storedCaptcha,
|
|
StringComparison.OrdinalIgnoreCase);
|
|
|
|
return Json(new
|
|
{
|
|
success = isValid,
|
|
message = isValid ? "Validation successful" : "Invalid CAPTCHA code"
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Log the error
|
|
return StatusCode(500, new
|
|
{
|
|
success = false,
|
|
message = "An error occurred during validation"
|
|
});
|
|
}
|
|
}
|
|
[HttpPost]
|
|
public async Task<IActionResult> UploadFile(IFormFile file)
|
|
{
|
|
// Save the uploaded file to a temporary location
|
|
var filePath = Path.GetTempFileName();
|
|
using (var stream = new FileStream(filePath, FileMode.Create))
|
|
{
|
|
await file.CopyToAsync(stream);
|
|
}
|
|
|
|
// Call the GoogleDriveService to upload the file
|
|
var googleDriveService = new GoogleDriveService("d");
|
|
var fileId = await googleDriveService.UploadFileAsync(filePath, file.FileName);
|
|
|
|
// Optionally, do something with the file ID
|
|
|
|
return RedirectToAction("Index");
|
|
}
|
|
public IActionResult Index()
|
|
{
|
|
Response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
|
|
Response.Headers.Add("Pragma", "no-cache");
|
|
Response.Headers.Add("Expires", "0");
|
|
HttpContext.Session.Clear();
|
|
TempData.Clear();
|
|
ViewBag.UserRoles = "";
|
|
var cred = new Infrastructure.Models.Account.User { ErrMessage = false };
|
|
return View(cred);
|
|
}
|
|
public IActionResult Logout()
|
|
{
|
|
Response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
|
|
Response.Headers.Add("Pragma", "no-cache");
|
|
Response.Headers.Add("Expires", "0");
|
|
HttpContext.Session.Clear();
|
|
TempData.Clear();
|
|
ViewBag.UserRoles = "";
|
|
var cred = new Infrastructure.Models.Account.User { ErrMessage = false };
|
|
return View(cred);
|
|
}
|
|
public async Task<IActionResult> RouteController(Infrastructure.Models.Account.User user)
|
|
{
|
|
try
|
|
{
|
|
var storedCaptchaCode = HttpContext.Session.GetString("CaptchaCode");
|
|
|
|
if (string.IsNullOrEmpty(storedCaptchaCode))
|
|
{
|
|
return Json(new { success = false, ResponseMessage = "CAPTCHA validation is required." });
|
|
}
|
|
var cred = new LoginVM
|
|
{
|
|
UserName = user.UserName,
|
|
Password = user.Password,
|
|
};
|
|
var login = await _tokenHelper.LoginAsync(cred);
|
|
|
|
if (login.Status == "Failed")
|
|
{
|
|
return Json(new { success = false, Response = login.Status, ResponseMessage = login.Message });
|
|
}
|
|
else if (login.Status == "Invalid")
|
|
{
|
|
await PostErrorMessage(login.Message, "WebApps");
|
|
return Json(new { success = false, responseStatus = login.Status, ResponseMessage = login.Message });
|
|
}
|
|
else
|
|
{
|
|
var (newCred, isValid) = await GetStoreCredAsync(user, await _tokenHelper.GetJwtTokenAsync(user));
|
|
if (isValid == true)
|
|
{
|
|
var userAccess = await _account.GetLandingPageByUserId(newCred);
|
|
|
|
var landingAction = userAccess.Where(u => u.AccessTypeId == 1).ToList();
|
|
if (landingAction.Count != 0)
|
|
{
|
|
return Json(new
|
|
{
|
|
success = true,
|
|
Response = true,
|
|
responseAction = landingAction.Select(u => u.Action).FirstOrDefault(),
|
|
responseController = landingAction.Select(u => u.Controller).FirstOrDefault()
|
|
});
|
|
}
|
|
return Json(new { success = false, ResponseMessage = "NoAcces" });
|
|
}
|
|
return Json(new { success = false, ResponseMessage = "NoAcces" });
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var message = ex.InnerException?.ToString() ?? ex.Message.ToString();
|
|
await PostErrorMessage(message,"WebApps");
|
|
return Json(new { success = false, ResponseMessage = "NoAcces" });
|
|
}
|
|
}
|
|
public async Task<bool> GetUserAttribute(Infrastructure.Models.Account.User user, string token)
|
|
{
|
|
if (user.Password != null && user.UserName != null)
|
|
{
|
|
|
|
var (cred, isValid) = await GetStoreCredAsync(user, token);
|
|
|
|
IsValid = isValid;
|
|
if (isValid)
|
|
{
|
|
//Getting the URL
|
|
var URLAttachment = await _attachment.GetAttachmentById(cred);
|
|
if (URLAttachment != null)
|
|
{
|
|
GetStoreAttachment(URLAttachment, true);
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
GetStoreAttachment(URLAttachment, false);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
public IActionResult Privacy()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult Error()
|
|
{
|
|
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
|
}
|
|
}
|
|
}
|