using System; using Kreta.Framework.Entities; namespace Kreta.Framework.Session.Instrumentation { public class SessionLogger { private const string insertCommandText = @" INSERT INTO T_FELHASZNALOBELEPESTORTENET (C_SESSIONID, C_SERVERNAME, C_USERIP, C_LOGINDATE, C_LASTACTIVITY, C_FELHASZNALOID, C_GONDVISELOID, SERIAL, CREATED, CREATOR) VALUES (:pSESSIONID, :pSERVERNAME, :pUSERIP, :pLOGINDATE, :pLASTACTIVITY, :pUSERID, :pTUTELARYID, :pSERIAL, :pCREATED, :pCREATOR)"; private const string insertCommandTextWithIntezmeny = @" DECLARE @intezmenyId INT = (SELECT ID FROM T_INTEZMENY WHERE C_AZONOSITO = :pIntezmenyAzonosito AND TOROLT = 'F') DECLARE @tanevId INT = (SELECT ID FROM T_TANEV WHERE TOROLT = 'F' AND C_AKTIV = 'T' AND C_INTEZMENYID = @intezmenyId) INSERT INTO T_FELHASZNALOBELEPESTORTENET (C_SESSIONID, C_SERVERNAME, C_USERIP, C_LOGINDATE, C_LASTACTIVITY, C_FELHASZNALOID, C_GONDVISELOID, C_INTEZMENYID, C_TANEVID, SERIAL, CREATED, CREATOR) VALUES (:pSESSIONID, :pSERVERNAME, :pUSERIP, :pLOGINDATE, :pLASTACTIVITY, :pUSERID, :pTUTELARYID, @intezmenyId, @tanevId, :pSERIAL, :pCREATED, :pCREATOR)"; private const string updateCommandText = @" UPDATE T_FELHASZNALOBELEPESTORTENET SET C_LASTACTIVITY = :pLASTACTIVITY, SERIAL = SERIAL + 1, LASTCHANGED = :pLASTCHANGED, MODIFIER = :pMODIFIER WHERE (C_FELHASZNALOID = :pUSERID and C_SESSIONID = :pSESSIONID)"; private string ConnectionString(string intezmenyAzonosito) { return SDAServer.Instance.ConnectionManager.GetIntezmenyConnectionString(intezmenyAzonosito); } public void SessionCreated(object sender, SessionEventArgs e) { try { using (var connection = new SDA.DataProvider.SDAConnection(ConnectionString(e.IntezmenyAzonosito))) { connection.Open(); using (var transaction = connection.BeginTransaction()) { using (var command = connection.CreateCommand()) { command.Connection = connection; command.Transaction = transaction; command.CommandText = insertCommandText; if (!string.IsNullOrWhiteSpace(e.IntezmenyAzonosito)) { command.CommandText = insertCommandTextWithIntezmeny; command.Parameters.Add("pIntezmenyAzonosito", SDA.DataProvider.SDADBType.String).Value = e.IntezmenyAzonosito; } DAUtil.BindParameter(command, "pSESSIONID", SDA.DataProvider.SDADBType.String, 36, e.SessionId, false); DAUtil.BindParameter(command, "pSERVERNAME", SDA.DataProvider.SDADBType.String, 50, e.ServerName, false); DAUtil.BindParameter(command, "pUSERIP", SDA.DataProvider.SDADBType.String, 15, e.ClientIP?.Split(':')[0], false); DAUtil.BindParameter(command, "pLOGINDATE", SDA.DataProvider.SDADBType.DateTime, DateTime.Now, false); DAUtil.BindParameter(command, "pLASTACTIVITY", SDA.DataProvider.SDADBType.DateTime, DateTime.Now, false); DAUtil.BindIdParameter(command, "pUSERID", e.FelhasznaloId); DAUtil.BindParameter(command, "pTUTELARYID", SDA.DataProvider.SDADBType.Int, e.GondviseloId, !e.GondviseloId.HasValue); command.Parameters.Add("pSERIAL", SDA.DataProvider.SDADBType.Int).Value = 0; command.Parameters.Add("pCREATED", SDA.DataProvider.SDADBType.DateTime).Value = DateTime.Now; command.Parameters.Add("pCREATOR", SDA.DataProvider.SDADBType.Int).Value = e.UserUniqueIdentifier; command.ExecuteNonQuery(); } transaction.Commit(); } } } catch (Exception exception) { try { if (SDAServer.Instance != null) { SDAServer.Instance.Logger.ExceptionThrown(exception); } } catch { } } } public void SessionDeleted(object sender, SessionEventArgs e) { try { using (var connection = new SDA.DataProvider.SDAConnection(ConnectionString(e.IntezmenyAzonosito))) { connection.Open(); using (var transaction = connection.BeginTransaction()) { using (var command = connection.CreateCommand()) { command.Connection = connection; command.Transaction = transaction; command.CommandText = updateCommandText; DAUtil.BindParameter(command, "pLASTACTIVITY", SDA.DataProvider.SDADBType.DateTime, DateTime.Now, false); DAUtil.BindParameter(command, "pSESSIONID", SDA.DataProvider.SDADBType.String, 36, e.SessionId, false); DAUtil.BindIdParameter(command, "pUSERID", e.FelhasznaloId); command.Parameters.Add("pLASTCHANGED", SDA.DataProvider.SDADBType.DateTime).Value = DateTime.Now; command.Parameters.Add("pMODIFIER", SDA.DataProvider.SDADBType.String).Value = e.UserUniqueIdentifier; command.ExecuteNonQuery(); } transaction.Commit(); } } } catch (Exception exception) { try { if (SDAServer.Instance != null) { SDAServer.Instance.Logger.ExceptionThrown(exception); } } catch { } } } } }