<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Signum.Engine.Maps;
using Signum.Entities.Authorization;
using Signum.Entities.Basics;
using Signum.Engine.DynamicQuery;
using Signum.Engine.Basics;
using Signum.Utilities;
using Signum.Utilities.DataStructures;
using System.Threading;
using Signum.Entities;
using Signum.Entities.Operations;
using Signum.Engine.Operations;
using System.Reflection;
using Signum.Engine.Extensions.Properties;

namespace Signum.Engine.Authorization
{
    public static class OperationAuthLogic
    {
        static AuthCache<RuleOperationDN, OperationAllowedRule, OperationDN, Enum, bool> cache; 

        public static void Start(SchemaBuilder sb)
        {
            if (sb.NotDefined(MethodInfo.GetCurrentMethod()))
            {
                AuthLogic.AssertIsStarted(sb);
                OperationLogic.AssertIsStarted(sb);

                OperationLogic.AllowOperation += new AllowOperationHandler(OperationLogic_AllowOperation);

                cache = new AuthCache<RuleOperationDN, OperationAllowedRule, OperationDN, Enum, bool>(sb,
                     EnumLogic<OperationDN>.ToEnum,
                     EnumLogic<OperationDN>.ToEntity,
                     AuthUtils.MaxAllowed, true);
            }
        }

        static bool OperationLogic_AllowOperation(Enum operationKey)
        {
            return cache.GetAllowed(RoleDN.Current.ToLite(), operationKey);
        }

        public static List<OperationInfo> Filter(List<OperationInfo> operationInfos)
        {
            RoleDN role = RoleDN.Current;

            return operationInfos.Where(ai => cache.GetAllowed(role.ToLite(), ai.Key)).ToList(); 
        }

        public static OperationRulePack GetOperationRules(Lite<RoleDN> roleLite, TypeDN typeDN)
        {

            var resources = OperationLogic.GetAllOperationInfos(TypeLogic.DnToType[typeDN]).Select(a => EnumLogic<OperationDN>.ToEntity(a.Key));
            return new OperationRulePack
            {
                Role = roleLite,
                Type = typeDN,
                Rules = cache.GetRules(roleLite, resources).ToMList()
            };
        }

        public static void SetOperationRules(OperationRulePack rules)
        {
            var keys = OperationLogic.GetAllOperationInfos(TypeLogic.DnToType[rules.Type])
                .Select(a => OperationDN.UniqueKey(a.Key)).ToArray();

            cache.SetRules(rules, r => keys.Contains(r.Key));
        }

        public static void SetOperationAllowed(Lite<RoleDN> role, Enum operationKey, bool allowed)
        {
            cache.SetAllowed(role, operationKey, allowed);
        }
    }
}
<commit_msg>(TFS CS 5348)<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Signum.Engine.Maps;
using Signum.Entities.Authorization;
using Signum.Entities.Basics;
using Signum.Engine.DynamicQuery;
using Signum.Engine.Basics;
using Signum.Utilities;
using Signum.Utilities.DataStructures;
using System.Threading;
using Signum.Entities;
using Signum.Entities.Operations;
using Signum.Engine.Operations;
using System.Reflection;
using Signum.Engine.Extensions.Properties;

namespace Signum.Engine.Authorization
{
    public static class OperationAuthLogic
    {
        static AuthCache<RuleOperationDN, OperationAllowedRule, OperationDN, Enum, bool> cache; 

        public static void Start(SchemaBuilder sb)
        {
            if (sb.NotDefined(MethodInfo.GetCurrentMethod()))
            {
                AuthLogic.AssertIsStarted(sb);
                OperationLogic.AssertIsStarted(sb);

                OperationLogic.AllowOperation += new AllowOperationHandler(OperationLogic_AllowOperation);

                cache = new AuthCache<RuleOperationDN, OperationAllowedRule, OperationDN, Enum, bool>(sb,
                     EnumLogic<OperationDN>.ToEnum,
                     EnumLogic<OperationDN>.ToEntity,
                     AuthUtils.MaxAllowed, true);
            }
        }

        static bool OperationLogic_AllowOperation(Enum operationKey)
        {
            return cache.GetAllowed(RoleDN.Current.ToLite(), operationKey);
        }

        public static List<OperationInfo> Filter(List<OperationInfo> operationInfos)
        {
            RoleDN role = RoleDN.Current;

            return operationInfos.Where(ai => cache.GetAllowed(role.ToLite(), ai.Key)).ToList(); 
        }

        public static OperationRulePack GetOperationRules(Lite<RoleDN> roleLite, TypeDN typeDN)
        {

            var resources = OperationLogic.GetAllOperationInfos(TypeLogic.DnToType[typeDN]).Select(a => EnumLogic<OperationDN>.ToEntity(a.Key));
            return new OperationRulePack
            {
                Role = roleLite,
                Type = typeDN,
                Rules = cache.GetRules(roleLite, resources).ToMList()
            };
        }

        public static void SetOperationRules(OperationRulePack rules)
        {
            var keys = OperationLogic.GetAllOperationInfos(TypeLogic.DnToType[rules.Type])
                .Select(a => OperationDN.UniqueKey(a.Key)).ToArray();

            cache.SetRules(rules, r => keys.Contains(r.Key));
        }

        public static void SetOperationAllowed(Lite<RoleDN> role, Enum operationKey, bool allowed)
        {
            cache.SetAllowed(role, operationKey, allowed);
        }


        public static bool GetOperationAllowed(Enum operationKey)
        {
            return cache.GetAllowed(RoleDN.Current.ToLite(), operationKey);
        }
    }
}

{'fraction_non_alphanumeric': 0.08672228015293709, 'fraction_numerical': 0.0006951685783802572, 'mean_word_length': 2.330439814814815, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4760234', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1539, 'n_words': 280}