using System; using System.Threading.Tasks; using System.Web; using Kreta.BusinessLogic.Classes; using Kreta.Framework; using Kreta.Web.Security; namespace Kreta.Web.Modules { public class UsageModule : IHttpModule { public void Init(HttpApplication context) { if (!ApplicationData.IsUsageTraced) return; var beginTaskHandler = new EventHandlerTaskAsyncHelper(HandleRequestCall); context.AddOnPostRequestHandlerExecuteAsync(beginTaskHandler.BeginEventHandler, beginTaskHandler.EndEventHandler); } private async Task HandleRequestCall(object sender, EventArgs e) { var app = sender as HttpApplication; if (app == null) { return; } if (string.IsNullOrWhiteSpace(ClaimData.SessionId)) { return; } int felhasznaloId = ClaimData.FelhasznaloId; int? gondviseloId = ClaimData.GondviseloId; var menuResourceKey = GetFormsSiteMapItem(app); if (!menuResourceKey.HasValue) { return; } await Task.Run(() => LogUsage(app, menuResourceKey.Value, felhasznaloId, gondviseloId)); } private void LogUsage(HttpApplication app, int menuResource, int felhasznaloId, int? gondviseloId) { try { var menuName = StringResourcesUtil.GetString(menuResource); HttpContext.Current = app.Context; InsertToDb(menuName, app.Context.Request.Url.AbsoluteUri, felhasznaloId, gondviseloId); } catch { } } private int? GetFormsSiteMapItem(HttpApplication app) { var siteMap = MvcSiteMapProvider.SiteMaps.GetSiteMap(Constants.General.FullKretaSiteMap); var node = siteMap.FindSiteMapNode(app.Context.Request.Url.AbsoluteUri); if (node == null) { return null; } if (node.Attributes.TryGetValue("resourceId", out var value)) { return Convert.ToInt32(value); } return null; } private void InsertToDb(string menupont, string url, int felhasznaloId, int? gondviseloId) { SDAServer.Instance.SessionManager.ActivateSession(ClaimData.SessionId); try { using (var command = UserContext.Instance.SDAConnection.CreateCommand(@"INSERT INTO T_OLDALLATOGATOTTSAG (C_LATOGATASIDEJE, C_MENUPONT, C_URL, C_GONDVISELOID, C_FELHASZNALOID, C_INTEZMENYID, C_TANEVID, CREATOR, TOROLT, SERIAL) VALUES(:platogatasIdo,:pmenupont,:purl,:pgondviseloId,:pfelhasznaloId ,:pintezmenyid, :ptanevid, :pfelhasznaloId, 'F',0)")) { command.Parameters.Add("platogatasIdo", DateTime.Now); command.Parameters.Add("pmenupont", menupont); command.Parameters.Add("purl", url); if (gondviseloId.HasValue) { command.Parameters.Add("pgondviseloId", gondviseloId.Value); } else { command.Parameters.Add("pgondviseloId", DBNull.Value); } command.Parameters.Add("pfelhasznaloId", felhasznaloId); command.Parameters.Add("pintezmenyid", ClaimData.IntezmenyId); command.Parameters.Add("ptanevid", ClaimData.SelectedTanevID.Value); command.Transaction = UserContext.Instance.SDATransaction; command.ExecuteNonQuery(); UserContext.Instance.CommitTransaction(); } } catch { UserContext.Instance.RollbackTransaction(); throw; } finally { SDAServer.Instance.SessionManager.DeActivateSession(ClaimData.SessionId); } } public void Dispose() { } } }