<commit_before><?php

namespace Rednose\FrameworkBundle\Acl;

use Rednose\FrameworkBundle\Acl\Model\ObjectAclData;
use Symfony\Component\Security\Acl\Permission\MaskBuilder;
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;

class ObjectAclManipulator
{
    /**
     * @var AclManipulator
     */
    protected $aclManipulator;

    /**
     * @var string
     */
    protected $maskBuilderClass;

    /**
     * @var array
     */
    protected $permissions;

    /**
     * @var array
     */
    protected $masks;

    /**
     * Constructor.
     *
     * @param AclManipulator $aclManipulator
     * @param string         $maskBuilderClass
     * @param array          $permissions
     */
    public function __construct(AclManipulator $aclManipulator, $maskBuilderClass, array $permissions = array('VIEW', 'EDIT', 'APPROVE'))
    {
        $this->aclManipulator   = $aclManipulator;
        $this->maskBuilderClass = $maskBuilderClass;
        $this->permissions      = $permissions;

        // Initialize masks.
        $this->masks = array();

        $reflectionClass = new \ReflectionClass(new $maskBuilderClass());

        foreach ($permissions as $permission) {
            $this->masks[$permission] = $reflectionClass->getConstant('MASK_' . $permission);
        }
    }

    /**
     * @param object $object
     * @param array  $identities
     *
     * @return ObjectAclData
     */
    public function createObjectAclData($object, array $identities)
    {
        $acl = $this->aclManipulator->getAcl($object);

        $data = array();

        foreach ($identities as $identity) {
            $securityIdentity = new UserSecurityIdentity($identity->getName(), get_class($identity));

            $entry = array();

            foreach ($this->permissions as $permission) {
                $entry[$permission] = $this->aclManipulator->isGrantedMask($acl, $securityIdentity, $this->masks[$permission]);
            }

            $data[$identity->getId()] = $entry;
        }

        $objectAclData = new ObjectAclData($object, $identities, $this->permissions);
        $objectAclData->setData($data);

        return $objectAclData;
    }

    /**
     * @param ObjectAclData $objectAclData
     */
    public function updateObjectAclData(ObjectAclData $objectAclData)
    {
        $data = $objectAclData->getData();

        foreach ($objectAclData->getIdentities() as $identity) {
            $securityIdentity = new UserSecurityIdentity($identity->getName(), get_class($identity));

            /** @var \Rednose\FrameworkBundle\Acl\Permission\MaskBuilder $maskBuilder */
            $maskBuilder = new $this->maskBuilderClass();

            foreach ($objectAclData->getPermissions() as $permission) {
                if ($data[$identity->getId()][$permission]) {
                    $maskBuilder->add($permission);
                }
            }
<commit_msg>User based document type permissions.<commit_after><?php

namespace Rednose\FrameworkBundle\Acl;

use Rednose\FrameworkBundle\Acl\Model\ObjectAclData;
use Symfony\Component\Security\Acl\Permission\MaskBuilder;
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;

class ObjectAclManipulator
{
    /**
     * @var AclManipulator
     */
    protected $aclManipulator;

    /**
     * @var string
     */
    protected $maskBuilderClass;

    /**
     * @var array
     */
    protected $permissions;

    /**
     * @var array
     */
    protected $masks;

    /**
     * Constructor.
     *
     * @param AclManipulator $aclManipulator
     * @param string         $maskBuilderClass
     * @param array          $permissions
     */
    public function __construct(AclManipulator $aclManipulator, $maskBuilderClass, array $permissions = array('VIEW', 'EDIT', 'APPROVE'))
    {
        $this->aclManipulator   = $aclManipulator;
        $this->maskBuilderClass = $maskBuilderClass;
        $this->permissions      = $permissions;

        // Initialize masks.
        $this->masks = array();

        $reflectionClass = new \ReflectionClass(new $maskBuilderClass());

        foreach ($permissions as $permission) {
            $this->masks[$permission] = $reflectionClass->getConstant('MASK_' . $permission);
        }
    }

    /**
     * @param object $object
     * @param array  $identities
     *
     * @return ObjectAclData
     */
    public function createObjectAclData($object, array $identities)
    {
        $acl = $this->aclManipulator->getAcl($object);

        $data = array();

        foreach ($identities as $identity) {
            $securityIdentity = new UserSecurityIdentity($identity, get_class($identity));

            $entry = array();

            foreach ($this->permissions as $permission) {
                $entry[$permission] = $this->aclManipulator->isGrantedMask($acl, $securityIdentity, $this->masks[$permission]);
            }

            $data[$identity->getId()] = $entry;
        }

        $objectAclData = new ObjectAclData($object, $identities, $this->permissions);
        $objectAclData->setData($data);

        return $objectAclData;
    }

    /**
     * @param ObjectAclData $objectAclData
     */
    public function updateObjectAclData(ObjectAclData $objectAclData)
    {
        $data = $objectAclData->getData();

        foreach ($objectAclData->getIdentities() as $identity) {
            $securityIdentity = new UserSecurityIdentity($identity, get_class($identity));

            /** @var \Rednose\FrameworkBundle\Acl\Permission\MaskBuilder $maskBuilder */
            $maskBuilder = new $this->maskBuilderClass();

            foreach ($objectAclData->getPermissions() as $permission) {
                if ($data[$identity->getId()][$permission]) {
                    $maskBuilder->add($permission);
                }
            }

{'fraction_non_alphanumeric': 0.1293935217091661, 'fraction_numerical': 0.0, 'mean_word_length': 2.4512485136741975, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '4983033', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1620, 'n_words': 290}