317 lines
12 KiB
C#
317 lines
12 KiB
C#
using Sacs.Web.Components;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Helpers;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using Saxonite.Net.Core.Web.Base.App;
|
|
using Saxonite.Net.Core.Web.Base.Controllers;
|
|
using Saxonite.Net.Core.Web.Base.Models;
|
|
using Saxonite.Net.Core.Web.Online.App;
|
|
using Saxonite.Net.Standard.Base.App;
|
|
using Saxonite.Net.Standard.Base.Bus;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Specialized;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Saxonite.Net.Core.Web.Online.Models
|
|
{
|
|
//=======================================================================
|
|
public class SaxModel_Online : SaxModel_Base
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private class UserCanManageOptions
|
|
{
|
|
public bool? OrganisationMembers { get; set; } = null;
|
|
public bool? OrganisationOpportunities { get; set; } = null;
|
|
public bool? OrganisationSchedule { get; set; } = null;
|
|
public bool? OrganisationArticles { get; set; } = null;
|
|
}
|
|
private readonly UserCanManageOptions userCanManage = new UserCanManageOptions();
|
|
|
|
//-------------------------------------------------------------------
|
|
// Protected construction.
|
|
protected SaxModel_Online(SaxLocaliser localiser) :
|
|
base(localiser)
|
|
{
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
//-------------------------------------------------------------------
|
|
// Session.
|
|
public SaxSessionData_Online SessionData_Online { get { return SessionData as SaxSessionData_Online; } }
|
|
public SysId SessionUserAccountId { get { return SessionData_Online?.User?.UserAccountId ?? null; } }
|
|
|
|
//-------------------------------------------------------------------
|
|
// Menu options.
|
|
public bool OrganisationMembersAllowed { get { return UserCanManageOrganisationMembers; } }
|
|
public bool ArticlesAllowed { get { return UserIsJournalist || UserCanManageOrganisationArticles; } }
|
|
public bool OrganisationOpportunitiesAllowed { get { return UserCanManageOrganisationOpportunities; } }
|
|
public bool OrganisationEventsAllowed { get { return UserCanManageOrganisationSchedule; } }
|
|
// Support menu is for admin reviewers only.
|
|
public bool SupportMenuAllowed { get { return SessionData_Online?.User.SupportMenuAllowed ?? false; } }
|
|
|
|
//-------------------------------------------------------------------
|
|
// Security profile.
|
|
public bool UserIsJournalist { get { return SessionData != null && SessionData.User != null && SessionData.User.IsJournalist; } }
|
|
public bool UserCanManageOrganisationMembers
|
|
{
|
|
get
|
|
{
|
|
if (userCanManage.OrganisationMembers == null && ValidUserSession)
|
|
{
|
|
SaxUserOrganisation.Params prmUserOrganisation = new SaxUserOrganisation.Params();
|
|
prmUserOrganisation.UserAccountIdsIncluded.Add(SessionUserAccountId);
|
|
prmUserOrganisation.OrganisationMembersOnly = true;
|
|
prmUserOrganisation.OrganisationRolesAny.Add(Sax.OrganisationRole.Admin);
|
|
prmUserOrganisation.OrganisationRolesAny.Add(Sax.OrganisationRole.MemberAdmin);
|
|
prmUserOrganisation.StatusCat = SaxUserOrganisation.StatusCat.ActiveOnly;
|
|
userCanManage.OrganisationMembers = (AccountMgr.GetUserOrganisationCount(prmUserOrganisation) > 0);
|
|
}
|
|
return userCanManage.OrganisationMembers == true ? true : false;
|
|
}
|
|
}
|
|
public bool UserCanManageOrganisationOpportunities
|
|
{
|
|
get
|
|
{
|
|
if (userCanManage.OrganisationOpportunities == null && ValidUserSession)
|
|
{
|
|
SaxUserOrganisation.Params prmUserOrganisation = new SaxUserOrganisation.Params();
|
|
prmUserOrganisation.UserAccountIdsIncluded.Add(SessionUserAccountId);
|
|
prmUserOrganisation.OrganisationMembersOnly = true;
|
|
prmUserOrganisation.OrganisationRolesAny.Add(Sax.OrganisationRole.Admin);
|
|
prmUserOrganisation.OrganisationRolesAny.Add(Sax.OrganisationRole.OpportunityAdmin);
|
|
prmUserOrganisation.StatusCat = SaxUserOrganisation.StatusCat.ActiveOnly;
|
|
userCanManage.OrganisationOpportunities = (AccountMgr.GetUserOrganisationCount(prmUserOrganisation) > 0);
|
|
}
|
|
return userCanManage.OrganisationOpportunities == true ? true : false;
|
|
}
|
|
}
|
|
public bool UserCanManageOrganisationSchedule
|
|
{
|
|
get
|
|
{
|
|
if (userCanManage.OrganisationSchedule == null && ValidUserSession)
|
|
{
|
|
SaxUserOrganisation.Params prmUserOrganisation = new SaxUserOrganisation.Params();
|
|
prmUserOrganisation.UserAccountIdsIncluded.Add(SessionUserAccountId);
|
|
prmUserOrganisation.OrganisationMembersOnly = true;
|
|
prmUserOrganisation.OrganisationRolesAny.Add(Sax.OrganisationRole.Admin);
|
|
prmUserOrganisation.OrganisationRolesAny.Add(Sax.OrganisationRole.ScheduleAdmin);
|
|
prmUserOrganisation.StatusCat = SaxUserOrganisation.StatusCat.ActiveOnly;
|
|
userCanManage.OrganisationSchedule = (AccountMgr.GetUserOrganisationCount(prmUserOrganisation) > 0);
|
|
}
|
|
return userCanManage.OrganisationSchedule == true ? true : false;
|
|
}
|
|
}
|
|
public bool UserCanManageOrganisationArticles
|
|
{
|
|
get
|
|
{
|
|
if (userCanManage.OrganisationArticles == null && ValidUserSession)
|
|
{
|
|
SaxUserOrganisation.Params prmUserOrganisation = new SaxUserOrganisation.Params();
|
|
prmUserOrganisation.UserAccountIdsIncluded.Add(SessionUserAccountId);
|
|
prmUserOrganisation.OrganisationMembersOnly = true;
|
|
prmUserOrganisation.OrganisationRolesAny.Add(Sax.OrganisationRole.Admin);
|
|
prmUserOrganisation.OrganisationRolesAny.Add(Sax.OrganisationRole.ArticleAdmin);
|
|
prmUserOrganisation.StatusCat = SaxUserOrganisation.StatusCat.ActiveOnly;
|
|
userCanManage.OrganisationArticles = (AccountMgr.GetUserOrganisationCount(prmUserOrganisation) > 0);
|
|
}
|
|
return userCanManage.OrganisationArticles == true ? true : false;
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item Id Obfuscation.
|
|
//-------------------------------------------------------------------
|
|
public SysId ExtractSysId(String idx)
|
|
{
|
|
String fn = GetType().Name + ".ExtractSysId";
|
|
if (StringUtil.IsEmpty(idx)) return null;
|
|
|
|
// Numeric values are assumed to be unencrypted Ids.
|
|
if (StringUtil.IsNumeric(idx))
|
|
return idx.ToSysId();
|
|
|
|
try
|
|
{
|
|
// Attempt to decrypt.
|
|
SysId id = DecryptId(idx);
|
|
if (SysId.IsValid(id))
|
|
return id;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SaLogger.Warn(fn + " " + ExceptionUtil.GetErrorStateMessage(exc));
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Id encryption.
|
|
// To obfuscate Id values that are sent to and from the browser.
|
|
// NB IV value can be reset evert time the application restarts.
|
|
public static String Base64Key_IdObfuscation = SacsEncryptionHelper.EncodePlainKey("|SaxOnIte.Onl1n3-iD+0bfuscAtion|", SacsEncryptionHelper.KeyLength);
|
|
public static String Base64IV_IdObfuscation = SacsEncryptionHelper.InitSymmetricEncryptionIV(Base64Key_IdObfuscation);
|
|
public static String ParamName_IdObfuscation_Id { get; } = "iob";
|
|
public String EncryptId(SysId id)
|
|
{
|
|
if (!SysId.IsValid(id)) return "";
|
|
NameValueCollection idObfuscationParams = new NameValueCollection();
|
|
idObfuscationParams.Add(ParamName_IdObfuscation_Id, SysId.ToString(id));
|
|
return SaxController_Base.EncryptParameters(idObfuscationParams, Base64IV_IdObfuscation, Base64Key_IdObfuscation);
|
|
}
|
|
public SysId DecryptId(String encryptedId)
|
|
{
|
|
if (StringUtil.IsEmpty(encryptedId))
|
|
return new SysId();
|
|
if (StringUtil.IsNumeric(encryptedId))
|
|
return encryptedId.ToSysId();
|
|
NameValueCollection idObfuscationParams = SaController.DecryptParameters(encryptedId, Base64IV_IdObfuscation, Base64Key_IdObfuscation);
|
|
return idObfuscationParams.Get(ParamName_IdObfuscation_Id).ToSysId();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Reference Data.
|
|
//-------------------------------------------------------------------
|
|
// Regions.
|
|
/// <summary>Region Data - to be JSON serialised in Ajax responses.</summary>
|
|
public class RegionData
|
|
{
|
|
/// <summary>Encrypted Id</summary>
|
|
public String Id { get; set; }
|
|
public int RegionType { get; set; }
|
|
public String RegionTypeCode { get; set; }
|
|
public String RegionTypeDesc { get; set; }
|
|
public String Name { get; set; }
|
|
public String ParentId { get; set; }
|
|
public String StatusCode { get; set; }
|
|
public String StatusDesc { get; set; }
|
|
}
|
|
public List<RegionData> GetRegionDataList_Active()
|
|
{
|
|
List<RegionData> regions = new List<RegionData>();
|
|
foreach (SaxRegion.Data region in DataCacheConsole.GetActiveRegions())
|
|
{
|
|
RegionData regionData = new RegionData()
|
|
{
|
|
Id = GetEncryptedRegionId(region.Id),
|
|
RegionType = (int)region.RegionType,
|
|
RegionTypeCode = region.RegionType.ToString(),
|
|
RegionTypeDesc = Localiser.GetRegionTypeCaption(region.RegionType),
|
|
Name = GetRegionCaption(region),
|
|
ParentId = SysId.ToString(region.RegionIdParent),
|
|
StatusCode = region.StatusCode,
|
|
StatusDesc = Localiser.GetStatusCaption(region.Status)
|
|
};
|
|
regions.Add(regionData);
|
|
}
|
|
return regions;
|
|
}
|
|
public String GetEncryptedRegionId(SysId regionId)
|
|
{
|
|
return DataCacheConsole.GetEncryptedRegionId(regionId, EncryptId(regionId));
|
|
}
|
|
public String GetRegionCaption(SysId regionId)
|
|
{
|
|
SaxRegion.Data region = DataCacheConsole.GetAllRegions().Find(item => item.Id.Equals(regionId));
|
|
return Localiser.GetRegionName(region);
|
|
}
|
|
public String GetRegionCaption(SaxRegion.Data region)
|
|
{
|
|
return Localiser.GetRegionName(region);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Sports.
|
|
/// <summary>Sport Data - to be JSON serialised in Ajax responses.</summary>
|
|
public class SportData
|
|
{
|
|
/// <summary>Encrypted Id</summary>
|
|
public String Id { get; set; }
|
|
public String SportIdParent { get; set; }
|
|
public String Name { get; set; }
|
|
public String StatusCode { get; set; }
|
|
public String StatusDesc { get; set; }
|
|
}
|
|
public List<SportData> GetSportDataList_Active()
|
|
{
|
|
List<SportData> sports = new List<SportData>();
|
|
foreach (SaxSport.Data sport in DataCacheConsole.GetActiveSports())
|
|
{
|
|
SportData sportData = new SportData()
|
|
{
|
|
Id = GetEncryptedSportId(sport.Id),
|
|
SportIdParent = SysId.IsValid(sport.SportIdParent) ? GetEncryptedSportId(sport.SportIdParent) : null,
|
|
Name = GetSportCaption(sport),
|
|
StatusCode = sport.StatusCode,
|
|
StatusDesc = Localiser.GetStatusCaption(sport.Status)
|
|
};
|
|
sports.Add(sportData);
|
|
}
|
|
return sports;
|
|
}
|
|
public List<SaxSport.Data> GetChildSports_Active(SysId sportIdParent)
|
|
{
|
|
List<SaxSport.Data> sports = new List<SaxSport.Data>();
|
|
if (!SysId.IsValid(sportIdParent)) return sports;
|
|
List<SaxSport.Data> activeSports = DataCacheConsole.GetActiveSports();
|
|
sports.AddRange(activeSports.FindAll(item => sportIdParent.Equals(item.SportIdParent)));
|
|
return sports;
|
|
}
|
|
public String GetEncryptedSportId(SysId sportId)
|
|
{
|
|
return DataCacheConsole.GetEncryptedSportId(sportId, EncryptId(sportId));
|
|
}
|
|
// Sport caption.
|
|
public String GetSportCaption(SysId sportId)
|
|
{
|
|
SaxSport.Data sport = DataCacheConsole.GetAllSports().Find(item => item.Id.Equals(sportId));
|
|
return GetSportCaption(sport);
|
|
}
|
|
public String GetSportCaption(SaxSport.Data sport)
|
|
{
|
|
if (sport == null) return "";
|
|
return Localiser.GetSportName(sport);
|
|
}
|
|
// Speciality type.
|
|
public bool SportSpecialityNotRelevant(SysId sportId)
|
|
{
|
|
SaxSport.Data sport = DataCacheConsole.GetAllSports().Find(item => item.Id.Equals(sportId));
|
|
return SportSpecialityNotRelevant(sport);
|
|
}
|
|
public bool SportSpecialityNotRelevant(SaxSport.Data sport)
|
|
{
|
|
if (sport == null) return true;
|
|
return sport.SpecialityLabel == Sax.SportSpecialityLabel.NotRelevant;
|
|
}
|
|
// Speciality label.
|
|
public String GetSportSpecialityLabel(SysId sportId)
|
|
{
|
|
SaxSport.Data sport = DataCacheConsole.GetAllSports().Find(item => item.Id.Equals(sportId));
|
|
return GetSportSpecialityLabel(sport);
|
|
}
|
|
public String GetSportSpecialityLabel(SaxSport.Data sport)
|
|
{
|
|
if (sport == null) return "";
|
|
switch (sport.SpecialityLabel)
|
|
{
|
|
case Sax.SportSpecialityLabel.Positon:
|
|
return Localiser["label.position"];
|
|
case Sax.SportSpecialityLabel.Discipline:
|
|
return Localiser["label.discipline"];
|
|
}
|
|
return Localiser["label.speciality"];
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|