<commit_before><?php
/**
 * @author <NAME> <<EMAIL>>
 * @copyright Zicht Online <http://www.zicht.nl>
 */

namespace Zicht\Bundle\AdminBundle\Security\Voter;

use \Sonata\AdminBundle\Admin\Pool;
use \Symfony\Component\DependencyInjection\ContainerInterface;
use \Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use \Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use \Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Stopwatch\Stopwatch;


/**
 * Delegates to the sonata ROLE_ voter based on an entity
 */
class AdminVoter implements VoterInterface
{
    /**
     * The 'view' attribute
     */
    const VIEW = 'VIEW';

    /**
     * @var Pool
     */
    protected $pool;

    /**
     * @var ContainerInterface
     */
    private $serviceContainer;

    function __construct(Pool $pool, ContainerInterface $serviceContainer)
    {
        $this->pool = $pool;
        $this->serviceContainer = $serviceContainer;
    }

    /**
     * @{inheritDoc}
     */
    public function supportsAttribute($attribute)
    {
        return in_array($attribute, array('EDIT', 'DELETE', 'VIEW', 'CREATE'));
    }

    /**
     * @{inheritDoc}
     */
    public function supportsClass($class)
    {
        // support any class that has an associated sonata admin
        return $this->pool->hasAdminByClass($class);
    }

    /**
     * @{inheritDoc}
     */
    public function vote(TokenInterface $token, $object, array $attributes)
    {
        $class = get_class($object);

        // check if class of this object is supported by this voter
        if ($this->supportsClass($class)) {
            /** @var AccessDecisionManagerInterface $accessDecisionManager */
            $accessDecisionManager = $this->serviceContainer->get('security.access.decision_manager');
            foreach ($this->mapAttributesToRoles($class, $attributes) as $mappedAttr) {
                if ($accessDecisionManager->decide($token, array($mappedAttr), $object)) {
                    return VoterInterface::ACCESS_GRANTED;
                }
            }
        }
        return VoterInterface::ACCESS_ABSTAIN;
    }

    /**
     * Maps regular attributes such as VIEW, EDIT, etc, to their respective SONATA role name,
     * such as ROLE_FOO_BAR_BAZ_EDIT
     *
     * @param string $class
     * @param string[] $attributes
     * @return array
     */
    protected function mapAttributesToRoles($class, $attributes)
    {
        /** @var \Sonata\AdminBundle\Security\Handler\RoleSecurityHandler */
        $roleSecurityHandler = $this->serviceContainer->get('sonata.admin.security.handler');

        $mappedAttributes = array();
        foreach ($this->pool->getAdminClasses() as $adminClass => $adminCodes) {
            if ($class instanceof $adminClass) {
                foreach ($adminCodes as $adminCode) {
                    $admin = $this->pool->getAdminByAdminCode($adminCode);
                    $baseRole = $roleSecurityHandler->getBaseRole($admin);

                    foreach ($attributes as $attr) {
                        if ($this->supportsAttribute($attr)) {
                            $mappedAttributes[]= sprintf($baseRole, $attr);
                        }
                    }
                }<commit_msg>for an unknown reason both $class and $adminClass are strings in this version of symfony/sonata<commit_after><?php
/**
 * @author <NAME> <<EMAIL>>
 * @copyright Zicht Online <http://www.zicht.nl>
 */

namespace Zicht\Bundle\AdminBundle\Security\Voter;

use \Sonata\AdminBundle\Admin\Pool;
use \Symfony\Component\DependencyInjection\ContainerInterface;
use \Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use \Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use \Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Stopwatch\Stopwatch;


/**
 * Delegates to the sonata ROLE_ voter based on an entity
 */
class AdminVoter implements VoterInterface
{
    /**
     * The 'view' attribute
     */
    const VIEW = 'VIEW';

    /**
     * @var Pool
     */
    protected $pool;

    /**
     * @var ContainerInterface
     */
    private $serviceContainer;

    function __construct(Pool $pool, ContainerInterface $serviceContainer)
    {
        $this->pool = $pool;
        $this->serviceContainer = $serviceContainer;
    }

    /**
     * @{inheritDoc}
     */
    public function supportsAttribute($attribute)
    {
        return in_array($attribute, array('EDIT', 'DELETE', 'VIEW', 'CREATE'));
    }

    /**
     * @{inheritDoc}
     */
    public function supportsClass($class)
    {
        // support any class that has an associated sonata admin
        return $this->pool->hasAdminByClass($class);
    }

    /**
     * @{inheritDoc}
     */
    public function vote(TokenInterface $token, $object, array $attributes)
    {
        $class = get_class($object);

        // check if class of this object is supported by this voter
        if ($this->supportsClass($class)) {
            /** @var AccessDecisionManagerInterface $accessDecisionManager */
            $accessDecisionManager = $this->serviceContainer->get('security.access.decision_manager');
            foreach ($this->mapAttributesToRoles($class, $attributes) as $mappedAttr) {
                if ($accessDecisionManager->decide($token, array($mappedAttr), $object)) {
                    return VoterInterface::ACCESS_GRANTED;
                }
            }
        }
        return VoterInterface::ACCESS_ABSTAIN;
    }

    /**
     * Maps regular attributes such as VIEW, EDIT, etc, to their respective SONATA role name,
     * such as ROLE_FOO_BAR_BAZ_EDIT
     *
     * @param string $class
     * @param string[] $attributes
     * @return array
     */
    protected function mapAttributesToRoles($class, $attributes)
    {
        /** @var \Sonata\AdminBundle\Security\Handler\RoleSecurityHandler */
        $roleSecurityHandler = $this->serviceContainer->get('sonata.admin.security.handler');

        $mappedAttributes = array();
        foreach ($this->pool->getAdminClasses() as $adminClass => $adminCodes) {
            if ($class === $adminClass || $class instanceof $adminClass) {
                foreach ($adminCodes as $adminCode) {
                    $admin = $this->pool->getAdminByAdminCode($adminCode);
                    $baseRole = $roleSecurityHandler->getBaseRole($admin);

                    foreach ($attributes as $attr) {
                        if ($this->supportsAttribute($attr)) {
                            $mappedAttributes[]= sprintf($baseRole, $attr);
                        }
                    }
                }
{'fraction_non_alphanumeric': 0.12143705463182898, 'fraction_numerical': 0.0, 'mean_word_length': 2.430244399185336, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7057061', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1795, 'n_words': 388}