130 lines
4.5 KiB
C#
130 lines
4.5 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using Sarand.BusinessMgr.Net.Core.Web.App;
|
|
using Sarand.BusinessMgr.Net.Core.Web.Portal.Models;
|
|
using Sarand.BusinessMgr.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Security;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
|
{
|
|
//=======================================================================
|
|
// Online Security Controller.
|
|
//=======================================================================
|
|
public partial class SecurityController : SbmController_Portal
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
public SecurityController(SbmLocaliser localiser, ILogger<HomeController> logger) :
|
|
base(localiser, logger)
|
|
{
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
[HttpGet]
|
|
public IActionResult Index()
|
|
{
|
|
return RedirectToAction("Signin", "Security");
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
[HttpGet]
|
|
public IActionResult Signin()
|
|
{
|
|
// Redirect home if signed-in.
|
|
if (ValidUserSession)
|
|
return RedirectToAction("", "");
|
|
|
|
Model_Security model = new Model_Security(Localiser);
|
|
return View(model);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Security - Change password
|
|
/// <summary>
|
|
/// Security - Change Password action.
|
|
/// <para>- GET from request password email link.</para>
|
|
/// <para>- POST with user signed-in. Browser cache prohibited.</para>
|
|
/// </summary>
|
|
[HttpGet, HttpPost]
|
|
public IActionResult ChangePassword(Model_Security model)
|
|
{
|
|
String fn = GetType().Name + ".ChangePassword";
|
|
bool validSessionRequired = true;
|
|
|
|
if (Request.Method.EqualsCI(HttpMethods.Get))
|
|
{
|
|
// Check for encrypted parameters from 'Request Password' email link.
|
|
// NB Only allowed as a 'GET' request.
|
|
String encryptedParams = Request.Query[ParamName_RequestPasswordEncrypted];
|
|
RequestPasswordParams requestPasswordParams = ExtractRequestPasswordLinkParams(encryptedParams);
|
|
if (requestPasswordParams.IsValid())
|
|
{
|
|
String logSuffix = requestPasswordParams.GetValidParams().Concat("", " ", ".");
|
|
using (SaSecurityItem securityItemUser = SecurityConsole.GetSecurityItemById(SaSecurity.ItemType.User, requestPasswordParams.SecurityItemId))
|
|
using (SbmAffiliate userAccount = AccountMgr.GetAffiliateById(requestPasswordParams.AffiliateId))
|
|
{
|
|
if (securityItemUser != null && userAccount != null)
|
|
{
|
|
if (userAccount.IsStatusActive)
|
|
{
|
|
if (requestPasswordParams.IsValid(securityItemUser, userAccount))
|
|
{
|
|
model.rpe = encryptedParams;
|
|
validSessionRequired = false;
|
|
}
|
|
else SaLogger.Warn(fn + " 'Request Password' parameters supplied but invalid. " + logSuffix);
|
|
}
|
|
else SaLogger.Warn(fn + " 'Request Password' parameters supplied but account is not active. " + logSuffix);
|
|
}
|
|
else SaLogger.Warn(fn + " 'Request Password' parameters supplied but security item or affiliate not found. " + logSuffix);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Must have a valid session to change password unless linked from Request Password above.
|
|
if (validSessionRequired && !ValidUserSession)
|
|
return RedirectToAction("", "");
|
|
|
|
// Configure page.
|
|
model.PageCaption = Localiser["security.caption.change.password"];
|
|
|
|
return View(model);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Security - Request Password
|
|
//-------------------------------------------------------------------
|
|
/// <summary>
|
|
/// Sign-in Request Password action.
|
|
/// <para>- User not signed-in.</para>
|
|
/// <para>- GET only with browser cache allowed.</para>
|
|
/// </summary>
|
|
[HttpGet]
|
|
public IActionResult RequestPassword()
|
|
{
|
|
Model_Security model = new Model_Security(Localiser);
|
|
|
|
// Configure page.
|
|
model.PageCaption = Localiser["security.caption.request.password"];
|
|
model.ProhibitCache = false;
|
|
|
|
return View(model);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|