148 lines
5.4 KiB
C#
148 lines
5.4 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using Saxonite.Net.Core.Web.Online.Models;
|
|
using Saxonite.Net.Core.Web.Base.App;
|
|
using Saxonite.Net.Core.Web.Base.Controllers;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Saxonite.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
|
|
namespace Saxonite.Net.Core.Web.Online.Controllers
|
|
{
|
|
//=======================================================================
|
|
// Online Account Controller - Contact - Ajax actions.
|
|
//=======================================================================
|
|
partial class AccountController
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Account - Get Contact Details.
|
|
//-------------------------------------------------------------------
|
|
public class ContactResponseData
|
|
{
|
|
public ContactResponseData(SaxUserAccount userAccount)
|
|
{
|
|
EmailAddress = userAccount.EmailAddress;
|
|
DisplayName = userAccount.GetDisplayName();
|
|
Title = userAccount.Title;
|
|
FirstName = userAccount.FirstName;
|
|
LastName = userAccount.LastName;
|
|
}
|
|
public String EmailAddress { get; }
|
|
public String DisplayName { get; }
|
|
public String Title { get; }
|
|
public String FirstName { get; }
|
|
public String LastName { get; }
|
|
}
|
|
public class AjaxResponse_AccountContactData : AjaxResponse_Data
|
|
{
|
|
public ContactResponseData Contact { get; set; }
|
|
}
|
|
// Ajax request.
|
|
[HttpPost]
|
|
[Route("Account/Ajax/GetProfileContact")]
|
|
public IActionResult GetProfileContact(Model_Account model)
|
|
{
|
|
String fn = GetType().Name + ".GetProfileContact";
|
|
AjaxResponse_AccountContactData responseData = new AjaxResponse_AccountContactData();
|
|
String messageTitle = Localiser["account.caption.profile.contact"];
|
|
|
|
// Validate session and referrer.
|
|
if (!ValidReferer)
|
|
return Forbid();
|
|
if (!ValidUserSession)
|
|
return GetAjaxResult_InvalidSession(responseData);
|
|
|
|
// Retrieve user details.
|
|
SaxUserAccount userAccount = AccountMgr.GetUserAccountById(SessionData.User.UserAccountId);
|
|
if (userAccount == null)
|
|
{
|
|
SaLogger.Warn(fn + " Failed to retrieve user account for Id: " + SysId.ToString(SessionData.User.UserAccountId));
|
|
return Json(responseData.SetGoHome(messageTitle, @Localiser.GetJavascriptText("msg.something.gone.wrong")));
|
|
}
|
|
|
|
// Populate data to response.
|
|
responseData.Contact = new ContactResponseData(userAccount);
|
|
responseData.Success = true;
|
|
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Account - Store Contact Details.
|
|
//-------------------------------------------------------------------
|
|
public class AjaxResponse_StoreAccountContactDetails : AjaxResponse_Data
|
|
{
|
|
public ContactResponseData Contact { get; set; }
|
|
}
|
|
// Ajax request.
|
|
[HttpPost]
|
|
[Route("Account/Ajax/StoreProfileContact")]
|
|
public IActionResult StoreProfileContact(Model_Account model)
|
|
{
|
|
String fn = GetType().Name + ".StoreProfileContact";
|
|
AjaxResponse_StoreAccountContactDetails responseData = new AjaxResponse_StoreAccountContactDetails();
|
|
String messageTitle = Localiser["account.caption.profile.contact"];
|
|
|
|
// Validate session and referrer.
|
|
if (!ValidReferer)
|
|
return Forbid();
|
|
if (!ValidUserSession)
|
|
return GetAjaxResult_InvalidSession(responseData);
|
|
|
|
// Validate data.
|
|
model.ValidateContactDetails(responseData);
|
|
if (responseData.HasControlErrors())
|
|
return Json(responseData);
|
|
|
|
// Retrieve user details.
|
|
SaxUserAccount userAccount = AccountMgr.GetUserAccountById(SessionData.User.UserAccountId);
|
|
if (userAccount == null)
|
|
{
|
|
SaLogger.Warn(fn + " Failed to retrieve user account for Id: " + SysId.ToString(SessionData.User.UserAccountId));
|
|
return Json(responseData.SetGoHome(messageTitle, @Localiser.GetJavascriptText("msg.something.gone.wrong")));
|
|
}
|
|
if (!userAccount.EmailAddress.EqualsCI(SessionData.User.EmailAddress))
|
|
{
|
|
SaLogger.Warn(fn + " Unable to update user account '" + userAccount.EmailAddress + ". Email doesn't match session: " + SessionData.User.EmailAddress);
|
|
return Json(responseData.SetGoHome(messageTitle, @Localiser.GetJavascriptText("msg.something.gone.wrong")));
|
|
}
|
|
|
|
// Populate user properties.
|
|
userAccount.Changed = false;
|
|
if (model.AccountTitle != null)
|
|
userAccount.Title = model.AccountTitle;
|
|
if (model.AccountFirstName != null)
|
|
userAccount.FirstName = model.AccountFirstName;
|
|
if (model.AccountLastName != null)
|
|
userAccount.LastName = model.AccountLastName;
|
|
|
|
// Update User Account.
|
|
if (userAccount.Changed) SaLogger.Info(fn + " Updating user account '" + userAccount.EmailAddress + "'. Id: " + SysId.ToString(userAccount.Id), true);
|
|
if (AccountMgr.UpdateUserAccount(userAccount, DbUpdateInfo))
|
|
{
|
|
// ToDo Refresh Session with updated organisation details.
|
|
}
|
|
else
|
|
{
|
|
SaLogger.Warn(fn + " Failed to update user account '" + userAccount.EmailAddress + "': " + AccountMgr.ErrorStateMessage);
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.update.failed"]));
|
|
}
|
|
|
|
// Construct response.
|
|
responseData.Contact = new ContactResponseData(userAccount);
|
|
responseData.Success = true;
|
|
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|