<commit_before><?php
class Vps_Component_Acl
{
    private $_isAllowedComponentClassCache = array();
    protected $_roleRegistry;
    protected $_rules = array(
        'allTags' => array(
            'allRoles' => array(
                'type' => Vps_Acl::TYPE_DENY
            )
        ),
        'byTagId' => array(),
        'allComponents' => array(
            'allRoles' => array(
                'type' => Vps_Acl::TYPE_DENY
            )
        ),
        'byComponentId' => array()
    );

    public function __construct(Zend_Acl_Role_Registry $roleRegistry)
    {
        $this->_roleRegistry = $roleRegistry;
        $this->_init();
    }


    protected function _init()
    {
        $this->allowTag('admin', null);
        $this->allowComponent('admin', null);
        if ($this->_roleRegistry->has('superuser')) {
            $this->allowTag('superuser', null);
            $this->allowComponent('superuser', null);
        }
    }

    /**
     * @param User-Row / null für guest
     * @param Vps_Component_Data/string
     * @param string
     * @return bool
     */
    public function isAllowed($userRow, $component, $privilege = null)
    {
        $role = $this->_getRole($userRow);

        if ($component instanceof Vps_Component_Data) {
            $componentClass = $component->componentClass;
            $allowed = $this->_isAllowedComponentData($userRow, $component);
            if (!$allowed) return false;
        } else {
            $componentClass = $component;
        }

        $allowed = $this->_isAllowedComponentClass($userRow, $componentClass);
        if (!$allowed) return false;

        return true;
    }

    protected function _getRole($userRow)
    {
        if (is_null($userRow)) {
            $role = 'guest';
        } else if (is_string($userRow)) {
            $role = $userRow;
        } else {
            $role = $userRow->role;
        }
        return $this->_roleRegistry->get($role);
    }

    //beim überschreiben aufpassen wegen dem _isAllowedComponentClassCache
    //darum erstmal private gemacht
    private function _isAllowedComponentClass($userRow, $componentClass)
    {
        $role = $this->_getRole($userRow);

        $rules = $this->_getRules('Component', $componentClass, $role);
        if ($rules && $rules['type'] == Vps_Acl::TYPE_ALLOW) return true;

        $rules = $this->_getRules('Component', null, $role);
        if ($rules && $rules['type'] == Vps_Acl::TYPE_ALLOW) return true;

        //überklassen überprüfen
        //cache ist nötig wegen endlos-rekursion + performance
        $roleId = $role->getRoleId();
        if (isset($this->_isAllowedComponentClassCache[$roleId][$componentClass])) {
            return $this->_isAllowedComponentClassCache[$roleId][$componentClass];
        }
        $this->_isAllowedComponentClassCache[$roleId][$componentClass] = false;
        foreach (Vpc_Abstract::getComponentClasses() as $c) {
            foreach (Vpc_Abstract::getChildComponentClasses($c) as $cc) {
                if ($cc == $componentClass) {
                    if ($this->_isAllowedComponentClass($userRow, $c)) {
                        $this->_isAllowedComponentClassCache[$roleId][$componentClass] = true;
                        return true;
                    }
                }
            }
        }

        return false;
    }

    protected function _isAllowedComponentData($userRow, Vps_Component_Data $component)
    {
        $role = $this->_getRole($userRow);

        $allowed = false;
        while ($component) {
            if (isset($component->tags) && $component->tags) {
                foreach ($component->tags as $t) {
                    $rules = $this->_getRules('Tag', $t, $role);
                    if ($rules && $rules['type'] == Vps_Acl::TYPE_ALLOW) {
                        $allowed = true;
                        break;
                    }
                }
            }
            $component = $component->parent;
            if ($component && $component->isPage) break;
        }
        if (!$allowed) {
            $rules = $this->_getRules('Tag', null, $role);
            if ($rules && $rules['type'] == Vps_Acl::TYPE_ALLOW) {
                $allowed = true;
            }
        }
        return $allowed;
    }

    public function allowTag($role, $tag, $privilege = null)
    {<commit_msg>Component_Acl: Tagprüfung ist nicht ganz bis zur Page hochgegangen<commit_after><?php
class Vps_Component_Acl
{
    private $_isAllowedComponentClassCache = array();
    protected $_roleRegistry;
    protected $_rules = array(
        'allTags' => array(
            'allRoles' => array(
                'type' => Vps_Acl::TYPE_DENY
            )
        ),
        'byTagId' => array(),
        'allComponents' => array(
            'allRoles' => array(
                'type' => Vps_Acl::TYPE_DENY
            )
        ),
        'byComponentId' => array()
    );

    public function __construct(Zend_Acl_Role_Registry $roleRegistry)
    {
        $this->_roleRegistry = $roleRegistry;
        $this->_init();
    }


    protected function _init()
    {
        $this->allowTag('admin', null);
        $this->allowComponent('admin', null);
        if ($this->_roleRegistry->has('superuser')) {
            $this->allowTag('superuser', null);
            $this->allowComponent('superuser', null);
        }
    }

    /**
     * @param User-Row / null für guest
     * @param Vps_Component_Data/string
     * @param string
     * @return bool
     */
    public function isAllowed($userRow, $component, $privilege = null)
    {
        $role = $this->_getRole($userRow);

        if ($component instanceof Vps_Component_Data) {
            $componentClass = $component->componentClass;
            $allowed = $this->_isAllowedComponentData($userRow, $component);
            if (!$allowed) return false;
        } else {
            $componentClass = $component;
        }

        $allowed = $this->_isAllowedComponentClass($userRow, $componentClass);
        if (!$allowed) return false;

        return true;
    }

    protected function _getRole($userRow)
    {
        if (is_null($userRow)) {
            $role = 'guest';
        } else if (is_string($userRow)) {
            $role = $userRow;
        } else {
            $role = $userRow->role;
        }
        return $this->_roleRegistry->get($role);
    }

    //beim überschreiben aufpassen wegen dem _isAllowedComponentClassCache
    //darum erstmal private gemacht
    private function _isAllowedComponentClass($userRow, $componentClass)
    {
        $role = $this->_getRole($userRow);

        $rules = $this->_getRules('Component', $componentClass, $role);
        if ($rules && $rules['type'] == Vps_Acl::TYPE_ALLOW) return true;

        $rules = $this->_getRules('Component', null, $role);
        if ($rules && $rules['type'] == Vps_Acl::TYPE_ALLOW) return true;

        //überklassen überprüfen
        //cache ist nötig wegen endlos-rekursion + performance
        $roleId = $role->getRoleId();
        if (isset($this->_isAllowedComponentClassCache[$roleId][$componentClass])) {
            return $this->_isAllowedComponentClassCache[$roleId][$componentClass];
        }
        $this->_isAllowedComponentClassCache[$roleId][$componentClass] = false;
        foreach (Vpc_Abstract::getComponentClasses() as $c) {
            foreach (Vpc_Abstract::getChildComponentClasses($c) as $cc) {
                if ($cc == $componentClass) {
                    if ($this->_isAllowedComponentClass($userRow, $c)) {
                        $this->_isAllowedComponentClassCache[$roleId][$componentClass] = true;
                        return true;
                    }
                }
            }
        }

        return false;
    }

    protected function _isAllowedComponentData($userRow, Vps_Component_Data $component)
    {
        $role = $this->_getRole($userRow);

        $allowed = false;
        while ($component) {
            if (isset($component->tags) && $component->tags) {
                foreach ($component->tags as $t) {
                    $rules = $this->_getRules('Tag', $t, $role);
                    if ($rules && $rules['type'] == Vps_Acl::TYPE_ALLOW) {
                        $allowed = true;
                        break;
                    }
                }
            }
            if ($component && $component->isPage) break;
            $component = $component->parent;
        }
        if (!$allowed) {
            $rules = $this->_getRules('Tag', null, $role);
            if ($rules && $rules['type'] == Vps_Acl::TYPE_ALLOW) {
                $allowed = true;
            }
        }
        return $allowed;
    }

    public function allowTag($role, $tag, $privilege = null)
    {
{'fraction_non_alphanumeric': 0.13532031517643028, 'fraction_numerical': 0.0, 'mean_word_length': 1.95180316818335, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 83, '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': '5355990', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2457, 'n_words': 478}