<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Signum.Entities.Authorization;
using Signum.Entities;
using Signum.Utilities;
using Signum.Engine.Maps;
using Signum.Engine.DynamicQuery;
using System.Reflection;

namespace Signum.Engine.Authorization
{
    public static class SessionLogLogic
    {
        public static void Start(SchemaBuilder sb, DynamicQueryManager dqm)
        {
            if (sb.NotDefined(MethodInfo.GetCurrentMethod()))
            {
                AuthLogic.AssertStarted(sb);
                
                sb.Include<SessionLogDN>();

                PermissionAuthLogic.RegisterPermissions(SessionLogPermission.TrackSession);

                dqm[typeof(SessionLogDN)] = (from sl in Database.Query<SessionLogDN>()
                                             select new
                                             {
                                                 Entity = sl.ToLite(),
                                                 sl.Id,
                                                 sl.User,
                                                 sl.SessionStart,
                                                 sl.SessionEnd,
                                                 sl.SessionTimeOut
                                             }).ToDynamic();
            }
        }

        static bool RoleTracked(Lite<RoleDN> role)
        {
            return SessionLogPermission.TrackSession.IsAuthorized(role);
        }

        public static void SessionStart(string userHostAddress, string userAgent)
        {
            var user = UserDN.Current;
            if (SessionLogLogic.RoleTracked(user.Role.ToLite()))
            {
                new SessionLogDN
                {
                    User = user.ToLite(),
                    SessionStart = DateTime.Now.TrimToSeconds(),
                    UserHostAddress = userHostAddress,
                    UserAgent = userAgent
                }.Save();
            }
        }

        public static void SessionEnd(UserDN user, TimeSpan? timeOut)
        {
            if (user == null || !RoleTracked(user.Role.ToLite()))
                return;

            using (AuthLogic.Disable())
            {
                var log = Database.Query<SessionLogDN>()
                    .Where(sl => sl.User.RefersTo(user))
                    .OrderByDescending(sl => sl.SessionStart)
                    .FirstOrDefault();<commit_msg>SessionLog - Do not requiere authorization to log session<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Signum.Entities.Authorization;
using Signum.Entities;
using Signum.Utilities;
using Signum.Engine.Maps;
using Signum.Engine.DynamicQuery;
using System.Reflection;

namespace Signum.Engine.Authorization
{
    public static class SessionLogLogic
    {
        public static void Start(SchemaBuilder sb, DynamicQueryManager dqm)
        {
            if (sb.NotDefined(MethodInfo.GetCurrentMethod()))
            {
                AuthLogic.AssertStarted(sb);
                
                sb.Include<SessionLogDN>();

                PermissionAuthLogic.RegisterPermissions(SessionLogPermission.TrackSession);

                dqm[typeof(SessionLogDN)] = (from sl in Database.Query<SessionLogDN>()
                                             select new
                                             {
                                                 Entity = sl.ToLite(),
                                                 sl.Id,
                                                 sl.User,
                                                 sl.SessionStart,
                                                 sl.SessionEnd,
                                                 sl.SessionTimeOut
                                             }).ToDynamic();
            }
        }

        static bool RoleTracked(Lite<RoleDN> role)
        {
            return SessionLogPermission.TrackSession.IsAuthorized(role);
        }

        public static void SessionStart(string userHostAddress, string userAgent)
        {
            var user = UserDN.Current;
            if (SessionLogLogic.RoleTracked(user.Role.ToLite()))
            {
                using (AuthLogic.Disable())
                {
                    new SessionLogDN
                    {
                        User = user.ToLite(),
                        SessionStart = DateTime.Now.TrimToSeconds(),
                        UserHostAddress = userHostAddress,
                        UserAgent = userAgent
                    }.Save();
                }
            }
        }

        public static void SessionEnd(UserDN user, TimeSpan? timeOut)
        {
            if (user == null || !RoleTracked(user.Role.ToLite()))
                return;

            using (AuthLogic.Disable())
            {
                var log = Database.Query<SessionLogDN>()
                    .Where(sl => sl.User.RefersTo(user))
                    .OrderByDescending(sl => sl.SessionStart)
                    .FirstOrDefault();
{'fraction_non_alphanumeric': 0.07677318784099767, 'fraction_numerical': 0.0, 'mean_word_length': 1.1558168836623268, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2978229', 'n_tokens_mistral': 1127, 'n_tokens_neox': 1092, 'n_words': 216}