<commit_before>﻿using System;
using System.Collections.Generic;
using AdvancedContentSecurity.Core.Items;
using AdvancedContentSecurity.Core.ItemSecurity;
using AdvancedContentSecurity.Core.Rules;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Rules;
using Sitecore.Security.Accounts;

namespace AdvancedContentSecurity.Core.ContentSecurity
{
    public class ContentSecurityManager : IContentSecurityManager
    {
        public ContentSecurityManager(IItemSecurityManager itemSecurityManager, IRulesManager rulesManager, IItemManager itemManager)
        {
            ItemSecurityManager = itemSecurityManager;
            RulesManager = rulesManager;
            ItemManager = itemManager;
        }

        protected IItemSecurityManager ItemSecurityManager { get; private set; }

        public IRulesManager RulesManager { get; private set; }

        public IItemManager ItemManager { get; set; }

        public virtual bool IsRuleReadAccessAllowed(Item item, User user)
        {
            const bool defaultValue = true;

            ValidateItemAndUser(item, user);


            if (!ItemSecurityManager.HasPermission(ContentSecurityConstants.AccessRights.Rules, item, user) || 
                String.IsNullOrEmpty(ItemManager.GetFieldValue(item, ContentSecurityConstants.FieldNames.ReadRules)))
            {
                return defaultValue;
            }

            IEnumerable<Item> rulesItems = ItemManager.GetItemsFromMultilist(item, ContentSecurityConstants.FieldNames.ReadRules);

            return EvaluateRules(item, rulesItems, defaultValue);
        }

        public virtual bool IsRestricted(Item item, User user)
        {
            const bool defaultValue = false;
            ValidateItemAndUser(item, user);

            if (ItemSecurityManager.HasPermission(ContentSecurityConstants.AccessRights.Restricted, item, user))
            {
                return true;
            }

            if (!ItemSecurityManager.HasPermission(ContentSecurityConstants.AccessRights.Rules, item, user) ||
                String.IsNullOrEmpty(ItemManager.GetFieldValue(item, ContentSecurityConstants.FieldNames.RestrictedRules)))
            {
                return defaultValue;
            }

            IEnumerable<Item> rulesItems = ItemManager.GetItemsFromMultilist(item, ContentSecurityConstants.FieldNames.RestrictedRules);

            return EvaluateRules(item, rulesItems, defaultValue);
        }

        private bool EvaluateRules(Item item, IEnumerable<Item> rulesItems, bool defaultValue)
        {
            if (rulesItems == null)
            {
                return defaultValue;
            }

            foreach (Item rulesItem in rulesItems)
            {
                if (!RulesManager.EvaluateRulesFromField<RuleContext>(ContentSecurityConstants.FieldNames.Rule, rulesItem, item))
                {
                    continue;
                }

                return !defaultValue;
            }

            return defaultValue;
        }

        private static void ValidateItemAndUser(Item item, User user)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
        }
    }
}
<commit_msg>Fixed broken content security manager<commit_after>﻿using System;
using System.Collections.Generic;
using AdvancedContentSecurity.Core.Items;
using AdvancedContentSecurity.Core.ItemSecurity;
using AdvancedContentSecurity.Core.Rules;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Rules;
using Sitecore.Security.Accounts;

namespace AdvancedContentSecurity.Core.ContentSecurity
{
    public class ContentSecurityManager : IContentSecurityManager
    {
        public ContentSecurityManager(IItemSecurityManager itemSecurityManager, IRulesManager rulesManager, IItemManager itemManager)
        {
            ItemSecurityManager = itemSecurityManager;
            RulesManager = rulesManager;
            ItemManager = itemManager;
        }

        protected IItemSecurityManager ItemSecurityManager { get; private set; }

        public IRulesManager RulesManager { get; private set; }

        public IItemManager ItemManager { get; set; }

        public virtual bool IsRuleReadAccessAllowed(Item item, User user)
        {
            const bool defaultValue = true;

            ValidateItemAndUser(item, user);


            if (!ItemSecurityManager.HasPermission(ContentSecurityConstants.AccessRights.Rules, item, user) || 
                String.IsNullOrEmpty(ItemManager.GetFieldValue(item, ContentSecurityConstants.FieldNames.ReadRules)))
            {
                return defaultValue;
            }

            IEnumerable<Item> rulesItems = ItemManager.GetItemsFromMultilist(item, ContentSecurityConstants.FieldNames.ReadRules);

            return EvaluateRules(item, rulesItems);
        }

        public virtual bool IsRestricted(Item item, User user)
        {
            const bool defaultValue = false;
            ValidateItemAndUser(item, user);

            if (ItemSecurityManager.HasPermission(ContentSecurityConstants.AccessRights.Restricted, item, user))
            {
                return true;
            }

            if (!ItemSecurityManager.HasPermission(ContentSecurityConstants.AccessRights.Rules, item, user) ||
                String.IsNullOrEmpty(ItemManager.GetFieldValue(item, ContentSecurityConstants.FieldNames.RestrictedRules)))
            {
                return defaultValue;
            }

            IEnumerable<Item> rulesItems = ItemManager.GetItemsFromMultilist(item, ContentSecurityConstants.FieldNames.RestrictedRules);

            return EvaluateRules(item, rulesItems);
        }

        private bool EvaluateRules(Item item, IEnumerable<Item> rulesItems)
        {
            if (rulesItems == null)
            {
                return true;
            }

            foreach (Item rulesItem in rulesItems)
            {
                if (RulesManager.EvaluateRulesFromField<RuleContext>(ContentSecurityConstants.FieldNames.Rule, rulesItem, item))
                {
                    continue;
                }

                return false;
            }

            return true;
        }

        private static void ValidateItemAndUser(Item item, User user)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.06925826630920465, 'fraction_numerical': 0.0, 'mean_word_length': 2.2206235011990407, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '7322112', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1536, 'n_words': 328}