173 lines
7.3 KiB
C#
173 lines
7.3 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Email;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using Saxonite.Net.Core.Web.Admin.Models;
|
|
using Saxonite.Net.Standard.Base.App;
|
|
using Saxonite.Net.Standard.Base.Bus;
|
|
using Saxonite.Net.Standard.Base.Helpers;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Saxonite.Net.Core.Web.Admin.Controllers
|
|
{
|
|
//=======================================================================
|
|
// Controller: Organisation - Activation - Ajax
|
|
//=======================================================================
|
|
partial class OrganisationController
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Get Organisation Activation Info ajax request.
|
|
//-------------------------------------------------------------------
|
|
private class AjaxResponse_OrganisationActivationInfo : AjaxResponse_Data
|
|
{
|
|
// Construction.
|
|
public AjaxResponse_OrganisationActivationInfo(SaxModel_Admin model) : base(model) { }
|
|
// Organisation details.
|
|
public OrganisationData_Full organisation { get; } = new OrganisationData_Full();
|
|
// Member awaiting activation.
|
|
public List<MemberData_List> Members { get; } = new List<MemberData_List>();
|
|
}
|
|
//-------------------------------------------------------------------
|
|
[HttpPost]
|
|
[Route("Organisation/Ajax/GetActivationInfo")]
|
|
public IActionResult Ajax_GetActivationInfo(Model_Organisation model)
|
|
{
|
|
String fn = GetType().Name + ".Ajax_GetActivationInfo";
|
|
String messageTitle = Localiser["organisation.caption.organisation"];
|
|
AjaxResponse_OrganisationActivationInfo responseData = new AjaxResponse_OrganisationActivationInfo(model);
|
|
|
|
// Validate session.
|
|
if (!ValidUserSession)
|
|
return GetAjaxResult_InvalidSession(responseData);
|
|
|
|
if (StringUtil.IsEmpty(SessionConfig.URL.Online.Root))
|
|
return Json(responseData.SetMessage(messageTitle, "Root online URL not specified."));
|
|
|
|
// Identify item.
|
|
SysId organisationId = model.OrganisationId.ToSysId();
|
|
if (!SysId.IsValid(organisationId))
|
|
{
|
|
SaLogger.Warn(fn + " Request to get activation info without valid organisation Id.");
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.specified"]));
|
|
}
|
|
|
|
// Retrieve item.
|
|
SaxOrganisation organisation = AccountMgr.GetOrganisationById(organisationId);
|
|
if (organisation == null)
|
|
{
|
|
SaLogger.Warn(fn + " Failed to retrieve organisation for Id: '" + model.OrganisationId + "'");
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.found"]));
|
|
}
|
|
|
|
// Construct response.
|
|
PopulateOrganisationResponseData(organisation, responseData.organisation);
|
|
GetOrganisationMembers(model, organisationId, responseData.Members);
|
|
|
|
// Complete response.
|
|
responseData.Success = true;
|
|
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Activate Organisation ajax request.
|
|
//-------------------------------------------------------------------
|
|
[HttpPost]
|
|
[Route("Organisation/Ajax/Activate")]
|
|
[Route("Organisation/Ajax/ActivateItem")]
|
|
public IActionResult Ajax_Activate(Model_Organisation model)
|
|
{
|
|
String fn = GetType().Name + ".Ajax_Activate";
|
|
String messageTitle = Localiser["organisation.caption.organisation"];
|
|
AjaxResponse_Organisation responseData = new AjaxResponse_Organisation(model);
|
|
|
|
// Validate session.
|
|
if (!ValidUserSession)
|
|
return GetAjaxResult_InvalidSession(responseData);
|
|
|
|
// Validate item properties.
|
|
model.ValidateOrganisationData(responseData);
|
|
if (responseData.HasControlErrors())
|
|
return Json(responseData);
|
|
|
|
// Retrieve existing item.
|
|
SysId organisationId = model.OrganisationId.ToSysId();
|
|
SaxOrganisation organisation = AccountMgr.GetOrganisationById(organisationId);
|
|
if (organisation == null && SysId.IsValid(organisationId))
|
|
{
|
|
SaLogger.Warn(fn + " Failed to retrieve organisation for Id: '" + model.OrganisationId + "'");
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.found"]));
|
|
}
|
|
|
|
// Activate organisation.
|
|
organisation.Status = Sarand.Net.Standard.Base.Core.SaStatus.Active;
|
|
if (!AccountMgr.UpdateOrganisation(organisation, DbUpdateInfo))
|
|
{
|
|
SaLogger.Warn(fn + " Failed to update organisation [Id:" + SysId.ToString(organisation.Id) + "]: " + AccountMgr.ErrorStateMessage);
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.update.failed"] + "\n\n" + AccountMgr.ErrorStateMessage));
|
|
}
|
|
|
|
// Activate awaiting members and send welcome email to each.
|
|
SaxUserOrganisation.Params prmUserOrganisation = new SaxUserOrganisation.Params();
|
|
prmUserOrganisation.OrganisationIdsIncluded.Add(organisationId);
|
|
prmUserOrganisation.UserAccountStatusCat = SaxUserAccount.StatusCat.AwaitingOnly;
|
|
prmUserOrganisation.StatusCat = SaxUserOrganisation.StatusCat.ActiveOnly;
|
|
List<SaxUserOrganisation> userOrganisations = new List<SaxUserOrganisation>();
|
|
foreach (SaxUserOrganisation userOrganisation in userOrganisations)
|
|
{
|
|
using (SaxUserAccount userAccount = AccountMgr.GetUserAccountById(userOrganisation.UserAccountId))
|
|
{
|
|
if (userAccount == null) continue;
|
|
userAccount.Status = SaStatus.Active;
|
|
if (AccountMgr.UpdateUserAccount(userAccount, DbUpdateInfo))
|
|
{
|
|
// Send welcome email.
|
|
SaEmailData emailData = new SaEmailData();
|
|
emailData.Config = EmailConfig;
|
|
emailData.AddressesTo.Add(userAccount.EmailAddress);
|
|
emailData.Subject = Localiser["security.caption.request.password"];
|
|
PopulateAccountActivatedEmailBody(emailData, userAccount);
|
|
if (EmailConsole.SendEmail(emailData))
|
|
SaLogger.Info(fn + " Password request email sent to " + userAccount.EmailAddress, true);
|
|
else SaLogger.Warn(fn + " Failed to send password request email to " + userAccount.EmailAddress);
|
|
}
|
|
else
|
|
{
|
|
SaLogger.Warn(fn + " Failed to update organisation [Id:" + SysId.ToString(organisation.Id) + "]: " + AccountMgr.ErrorStateMessage);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Construct response.
|
|
PopulateOrganisationResponseData(organisation, responseData.organisation);
|
|
|
|
// Complete response.
|
|
responseData.Success = true;
|
|
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
private void PopulateAccountActivatedEmailBody(SaEmailData emailData, SaxUserAccount userAccount)
|
|
{
|
|
// Will need to handle culture (language/country) as and when introduced.
|
|
emailData.MessageBody = SaxResourceHelper.GetTextResource(SaxResourceHelper.TextResource.Email_Security_AccountActivated_Html);
|
|
emailData.HtmlBody = true;
|
|
|
|
// Replace dynamic tags.
|
|
EmailConsole.ReplaceDynamicEmailBodyTags(emailData, null, userAccount);
|
|
|
|
// Replace dynamic tag for link back to change password page.
|
|
SaEmailConsole.ReplaceDynamicEmailTag(emailData, "[#Link.RequestPassword]", SecurityController.GenerateRequestPasswordLink(this, userAccount.Id, userAccount.EmailAddress, "", SessionConfig.URL.Online.Root));
|
|
SaEmailConsole.ReplaceDynamicEmailTag(emailData, "[#Link.ExpiresMinutes]", SecurityController.GetRequestPasswordEmailLinkExpiresMinutes(this).ToString());
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
}
|