234 lines
6.6 KiB
C#
234 lines
6.6 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.Web;
|
|
using Kreta.Framework.Caching;
|
|
using Kreta.Framework.Security;
|
|
using SDA.DataProvider;
|
|
|
|
namespace Kreta.Framework
|
|
{
|
|
/// <summary>
|
|
/// Az osztály biztosítja a felhasználói muveletek elvégzéséhez szükséges környezetet.
|
|
/// Ide van kivezetve minden olyan metódus és változó, amire a futás során szükség lehet.
|
|
/// </summary>
|
|
public class UserContext
|
|
{
|
|
[ThreadStatic]
|
|
private static UserContext instance;
|
|
|
|
private readonly LoginInfoCache loginInfoCache;
|
|
|
|
public UserContext(LoginInfo loginInfo) : this(loginInfo, new TransactionContext(loginInfo.IntezmenyAzonosito))
|
|
{
|
|
}
|
|
|
|
public UserContext(LoginInfo loginInfo, TransactionContext transactionContext)
|
|
{
|
|
LoginInfo = loginInfo;
|
|
|
|
LastAccess = DateTime.Now;
|
|
LanguageContext = new LanguageContext(LanguageContext.DefaultLanguageContext.LCID);
|
|
TransactionContext = transactionContext;
|
|
|
|
loginInfoCache = SDAServer.Instance.CacheManager.AquireCache<LoginInfoCache>();
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.Append("SessionId: ").Append(SessionID).Append('\n');
|
|
sb.Append("ClientIp: ").Append(ClientIP).Append('\n');
|
|
sb.Append("UserId: ").Append(FelhasznaloId).Append('\n');
|
|
sb.Append("LastAccess: ").Append(LastAccess).Append('\n');
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
public TransactionContext TransactionContext { get; }
|
|
|
|
public LanguageContext LanguageContext { get; }
|
|
|
|
public LoginInfo LoginInfo { get; }
|
|
|
|
public int IntezmenyId => LoginInfo.IntezmenyId;
|
|
|
|
public int AktivTanevId => LoginInfo.AktivTanevId;
|
|
|
|
public int SelectedTanevId => LoginInfo.SelectedTanevId;
|
|
|
|
public string IntezmenyAzonosito => LoginInfo.IntezmenyAzonosito;
|
|
|
|
public int UniqueIdentifier => LoginInfo.UniqueIdentifier;
|
|
|
|
public string SessionID => LoginInfo.SessionID;
|
|
|
|
public int FelhasznaloId => LoginInfo.FelhasznaloId;
|
|
|
|
public string ClientIP => LoginInfo.ClientIP;
|
|
|
|
public void SetIntezmenyEsTanev(int intezmenyId, int aktivTanevId, int selectedTanevId)
|
|
{
|
|
LoginInfo.IntezmenyId = intezmenyId;
|
|
SetTanev(aktivTanevId, selectedTanevId);
|
|
}
|
|
|
|
public void SetTanev(int aktivTanevId, int selectedTanevId)
|
|
{
|
|
LoginInfo.AktivTanevId = aktivTanevId;
|
|
LoginInfo.SelectedTanevId = selectedTanevId;
|
|
UpdateLoginInfoCache();
|
|
}
|
|
|
|
public void SetFelhasznaloId(int felhasznaloId)
|
|
{
|
|
LoginInfo.FelhasznaloId = felhasznaloId;
|
|
LoginInfo.UniqueIdentifier = felhasznaloId;
|
|
UpdateLoginInfoCache();
|
|
}
|
|
|
|
public void SetSelectedTanevId(int selectedTanevId)
|
|
{
|
|
LoginInfo.SelectedTanevId = selectedTanevId;
|
|
UpdateLoginInfoCache();
|
|
}
|
|
|
|
private void UpdateLoginInfoCache()
|
|
{
|
|
if (loginInfoCache.IsExistsLoginInfo(SessionID))
|
|
{
|
|
loginInfoCache.UpdateLoginInfo(LoginInfo);
|
|
}
|
|
}
|
|
|
|
public bool Activated { get; private set; }
|
|
|
|
public DateTime LastAccess { get; set; }
|
|
|
|
/// <summary>
|
|
/// Visszadja az aktuális szálhoz tartozó UserContext példányt.
|
|
/// Ha nincs ilyen, akkor null-t ad vissza.
|
|
/// </summary>
|
|
public static UserContext Instance
|
|
{
|
|
get
|
|
{
|
|
if (HttpContext.Current != null)
|
|
{
|
|
return HttpContext.Current.Items[nameof(UserContext)] as UserContext;
|
|
}
|
|
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
#region Módszerek
|
|
|
|
/// <summary>
|
|
/// Aktíválja a munkamenetet.
|
|
/// </summary>
|
|
public virtual void Activate()
|
|
{
|
|
LastAccess = DateTime.Now;
|
|
|
|
var userContext = this;
|
|
|
|
if (HttpContext.Current != null && !HttpContext.Current.Items.Contains(nameof(UserContext)))
|
|
{
|
|
HttpContext.Current.Items.Add(nameof(UserContext), userContext);
|
|
}
|
|
else
|
|
{
|
|
instance = userContext;
|
|
}
|
|
|
|
if (!userContext.Activated)
|
|
{
|
|
userContext.Activated = true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Passzíválja a munkamenetet.
|
|
/// </summary>
|
|
public virtual void DeActivate()
|
|
{
|
|
LastAccess = DateTime.Now;
|
|
|
|
var userContext = this;
|
|
|
|
if (HttpContext.Current != null && HttpContext.Current.Items.Contains(nameof(UserContext)))
|
|
{
|
|
userContext = HttpContext.Current.Items[nameof(UserContext)] as UserContext;
|
|
HttpContext.Current.Items.Remove(nameof(UserContext));
|
|
}
|
|
else
|
|
{
|
|
instance = null;
|
|
}
|
|
|
|
userContext.TransactionContext.Close();
|
|
userContext.Activated = false;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Ügylet kezelés
|
|
|
|
/// <summary>
|
|
/// Feloldja a munkamenet ügyletének zárolását.
|
|
/// </summary>
|
|
/// <param name="commit">A ügyletet elkövesse, vagy visszagörgesse.</param>
|
|
internal void UnlockTransaction(bool commit)
|
|
{
|
|
TransactionContext.Unlock();
|
|
if (commit)
|
|
{
|
|
TransactionContext.Commit();
|
|
}
|
|
else
|
|
{
|
|
TransactionContext.Rollback();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Jóváhagyja a munkamenethez tartozó ügyletet.
|
|
/// </summary>
|
|
public void CommitTransaction()
|
|
{
|
|
TransactionContext.Commit();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Visszagörgeti a munkamenethez tartozó ügyletet.
|
|
/// </summary>
|
|
public void RollbackTransaction()
|
|
{
|
|
TransactionContext.Rollback();
|
|
}
|
|
|
|
/// <summary>
|
|
/// A munkamenethez tartozó adatbáziskapcsolat.
|
|
/// </summary>
|
|
public SDAConnection SDAConnection
|
|
{
|
|
get
|
|
{
|
|
return TransactionContext.DBConnection;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// A munkamenethez tartozó ügylet.
|
|
/// </summary>
|
|
public SDATransaction SDATransaction
|
|
{
|
|
get
|
|
{
|
|
return TransactionContext.DBTransaction;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|