<commit_before><?php
/**
 * ACL Security handlers
 */
namespace Minds\Core\Security;

use Minds\Core;
use Minds\Core\Di\Di;
use Minds\Core\Log\Logger;
use Minds\Entities;
use Minds\Core\Security\RateLimits\Manager as RateLimitsManager;
use Minds\Core\EntitiesBuilder;
use Minds\Helpers\Flags;

class ACL
{
    private static $_;
    public static $ignore = false;

    /** @var RateLimitsManager $rateLimits */
    private $rateLimits;

    /** @var EntitiesBuilder */
    private $entitiesBuilder;

    /** @var Logger */
    private $logger;

    public function __construct($rateLimits = null, $entitiesBuilder = null, $logger = null)
    {
        $this->rateLimits = $rateLimits ?: new RateLimitsManager;
        $this->entitiesBuilder = $entitiesBuilder ?? Di::_()->get('EntitiesBuilder');
        $this->logger = $logger ?? Di::_()->get('Logger');
    }

    /**
     * Initialise default ACL constraints
     */
    private function init()
    {
        ACL\Block::_()->listen();
    }

    /**
     * Override the ACL and return the previous status
     * @param boolean $ignore
     * @return boolean
     */
    public function setIgnore($ignore = false)
    {
        $previous = self::$ignore;
        self::$ignore = $ignore;
        return $previous;
    }

    /**
     * Checks access read rights to entity
     * @param Entities\Entity $entity
     * @param $user optional
     * @param $strict optional. skips public access checks
     * @return boolean
     */
    public function read($entity, $user = null)
    {
        if (!$user) {
            $user = Core\Session::getLoggedinUser();
        }

        if (self::$ignore == true) {
            return true;
        }

        if (Flags::shouldFail($entity)) {
            return false;
        }

        /**
         * Below is a quick and hacky solution to ensuring that content has
         * a valid owner.
         *
         * TODO: We want to move this to also be able to rehydrate the ownerObj
         */
        if ($entity->owner_guid && !$entity instanceof Entities\User) {
            $ownerEntity = $this->entitiesBuilder->single($entity->owner_guid, [
                'cache' => true,
                'cacheTtl' => 604800, // Cache for 7 days
            ]);

            // Owner not found
            if (!$ownerEntity) {
                $this->logger->error("$entity->guid was requested but owner $entity->owner_guid was not found");
                return false;
            }

            // Owner is banned or disabled
            if ($ownerEntity->isBanned() || !$ownerEntity->isEnabled()) {
                $this->logger->error("$entity->guid was requested but owner $entity->owner_guid {$ownerEntity->username}) is banned or disabled");
                return false;
            }

            // Owner passes other ACL rules (fallback)
            if ($this->read($ownerEntity, $user) !== true) {
                $this->logger->error("$entity->guid was requested but owner $entity->owner_guid failed to pass its own ACL READ event");
                return false;
            }
        }

        // If logged out and public and not container
        if (!Core\Session::isLoggedIn()) {
            if (
                (int) $entity->access_id == ACCESS_PUBLIC
                && (
                    $entity->owner_guid == $entity->container_guid
                    || $entity->container_guid == 0
                )
            ) {
                return true;
            } else {
                if (Core\Events\Dispatcher::trigger('acl:read', $entity->getType(), [
                    'entity' => $entity,
                    'user' => $user<commit_msg>(chore): reduce to info level|<commit_after><?php
/**
 * ACL Security handlers
 */
namespace Minds\Core\Security;

use Minds\Core;
use Minds\Core\Di\Di;
use Minds\Core\Log\Logger;
use Minds\Entities;
use Minds\Core\Security\RateLimits\Manager as RateLimitsManager;
use Minds\Core\EntitiesBuilder;
use Minds\Helpers\Flags;

class ACL
{
    private static $_;
    public static $ignore = false;

    /** @var RateLimitsManager $rateLimits */
    private $rateLimits;

    /** @var EntitiesBuilder */
    private $entitiesBuilder;

    /** @var Logger */
    private $logger;

    public function __construct($rateLimits = null, $entitiesBuilder = null, $logger = null)
    {
        $this->rateLimits = $rateLimits ?: new RateLimitsManager;
        $this->entitiesBuilder = $entitiesBuilder ?? Di::_()->get('EntitiesBuilder');
        $this->logger = $logger ?? Di::_()->get('Logger');
    }

    /**
     * Initialise default ACL constraints
     */
    private function init()
    {
        ACL\Block::_()->listen();
    }

    /**
     * Override the ACL and return the previous status
     * @param boolean $ignore
     * @return boolean
     */
    public function setIgnore($ignore = false)
    {
        $previous = self::$ignore;
        self::$ignore = $ignore;
        return $previous;
    }

    /**
     * Checks access read rights to entity
     * @param Entities\Entity $entity
     * @param $user optional
     * @param $strict optional. skips public access checks
     * @return boolean
     */
    public function read($entity, $user = null)
    {
        if (!$user) {
            $user = Core\Session::getLoggedinUser();
        }

        if (self::$ignore == true) {
            return true;
        }

        if (Flags::shouldFail($entity)) {
            return false;
        }

        /**
         * Below is a quick and hacky solution to ensuring that content has
         * a valid owner.
         *
         * TODO: We want to move this to also be able to rehydrate the ownerObj
         */
        if ($entity->owner_guid && !$entity instanceof Entities\User) {
            $ownerEntity = $this->entitiesBuilder->single($entity->owner_guid, [
                'cache' => true,
                'cacheTtl' => 604800, // Cache for 7 days
            ]);

            // Owner not found
            if (!$ownerEntity) {
                $this->logger->info("$entity->guid was requested but owner $entity->owner_guid was not found");
                return false;
            }

            // Owner is banned or disabled
            if ($ownerEntity->isBanned() || !$ownerEntity->isEnabled()) {
                $this->logger->info("$entity->guid was requested but owner $entity->owner_guid {$ownerEntity->username}) is banned or disabled");
                return false;
            }

            // Owner passes other ACL rules (fallback)
            if ($this->read($ownerEntity, $user) !== true) {
                $this->logger->info("$entity->guid was requested but owner $entity->owner_guid failed to pass its own ACL READ event");
                return false;
            }
        }

        // If logged out and public and not container
        if (!Core\Session::isLoggedIn()) {
            if (
                (int) $entity->access_id == ACCESS_PUBLIC
                && (
                    $entity->owner_guid == $entity->container_guid
                    || $entity->container_guid == 0
                )
            ) {
                return true;
            } else {
                if (Core\Events\Dispatcher::trigger('acl:read', $entity->getType(), [
                    'entity' => $entity,
                    'user' => $user
{'fraction_non_alphanumeric': 0.12598854649577312, 'fraction_numerical': 0.00218161985274066, 'mean_word_length': 1.8287697647512533, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 73, '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': '7208851', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1950, 'n_words': 535}