<commit_before><?php
/*
 * This file is part of the login-cidadao project or it's bundles.
 *
 * (c) <NAME> <guilhermednt on github>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace LoginCidadao\OAuthBundle\Form;

use Doctrine\ORM\EntityRepository;
use LoginCidadao\CoreBundle\Form\Type\AjaxChoiceType;
use LoginCidadao\CoreBundle\Form\Type\SwitchType;
use LoginCidadao\OAuthBundle\Entity\Organization;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use LoginCidadao\OAuthBundle\Model\OrganizationInterface;

class OrganizationType extends AbstractType
{
    /** @var AuthorizationCheckerInterface */
    protected $authorizationChecker;

    /** @var TokenStorageInterface */
    protected $tokenStorage;

    public function __construct(
        AuthorizationCheckerInterface $authorizationChecker,
        TokenStorageInterface $tokenStorage
    ) {
        $this->authorizationChecker = $authorizationChecker;
        $this->tokenStorage = $tokenStorage;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class, ['label' => 'organizations.form.name.label'])
            ->add('domain', TextType::class, ['label' => 'organizations.form.domain.label'])
            ->add('sectorIdentifierUri', UrlType::class, ['label' => 'organizations.form.sectorIdentifierUri.label']);

        if ($this->authorizationChecker->isGranted('ROLE_ORGANIZATIONS_CAN_TRUST')) {
            $builder->add('trusted', SwitchType::class, [
                'label' => 'organizations.form.trusted.label',
                'required' => false,
            ]);
        }
        if ($this->authorizationChecker->isGranted('ROLE_ORGANIZATIONS_VALIDATE')
            && $builder->getData()->getId()
        ) {
            $builder->add('validationUrl', UrlType::class, [
                'required' => false,
                'label' => 'organizations.form.validationUrl.label',
            ]);
        }

        $organization = $builder->getData();
        $this->prepareMembersField($builder, $organization);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Organization::class,
        ]);
    }

    private function prepareMembersField(
        FormBuilderInterface $builder,
        OrganizationInterface $organization
    ) {
        $checker = $this->authorizationChecker;
        $person = $this->tokenStorage->getToken()->getUser();

        if (!$checker->isGranted('ROLE_ORGANIZATIONS_MANAGE_MEMBERS_ANY_ORG') &&
            !$checker->isGranted('ROLE_ORGANIZATIONS_MANAGE_MEMBERS')
        ) {
            return;
        }

        if (false === array_search($person, $organization->getMembers()) &&
            !$checker->isGranted('ROLE_ORGANIZATIONS_MANAGE_MEMBERS_ANY_ORG')
        ) {
            return;
        }

        $builder->addEventListener(
            FormEvents::PRE_SUBMIT,
            function (FormEvent $event) {
                $entity = $event->getData();
                $form = $event->getForm();

                $qb = function (EntityRepository $er) use ($entity) {
                    $sql = $er->createQueryBuilder('u');
                    if (!empty($entity['members'])) {
                        $sql->where('u.id in (:members)');<commit_msg>ensure  is an array PROCERGS#808<commit_after><?php
/*
 * This file is part of the login-cidadao project or it's bundles.
 *
 * (c) <NAME> <guilhermednt on github>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace LoginCidadao\OAuthBundle\Form;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityRepository;
use LoginCidadao\CoreBundle\Form\Type\AjaxChoiceType;
use LoginCidadao\CoreBundle\Form\Type\SwitchType;
use LoginCidadao\OAuthBundle\Entity\Organization;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use LoginCidadao\OAuthBundle\Model\OrganizationInterface;

class OrganizationType extends AbstractType
{
    /** @var AuthorizationCheckerInterface */
    protected $authorizationChecker;

    /** @var TokenStorageInterface */
    protected $tokenStorage;

    public function __construct(
        AuthorizationCheckerInterface $authorizationChecker,
        TokenStorageInterface $tokenStorage
    ) {
        $this->authorizationChecker = $authorizationChecker;
        $this->tokenStorage = $tokenStorage;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class, ['label' => 'organizations.form.name.label'])
            ->add('domain', TextType::class, ['label' => 'organizations.form.domain.label'])
            ->add('sectorIdentifierUri', UrlType::class, ['label' => 'organizations.form.sectorIdentifierUri.label']);

        if ($this->authorizationChecker->isGranted('ROLE_ORGANIZATIONS_CAN_TRUST')) {
            $builder->add('trusted', SwitchType::class, [
                'label' => 'organizations.form.trusted.label',
                'required' => false,
            ]);
        }
        if ($this->authorizationChecker->isGranted('ROLE_ORGANIZATIONS_VALIDATE')
            && $builder->getData()->getId()
        ) {
            $builder->add('validationUrl', UrlType::class, [
                'required' => false,
                'label' => 'organizations.form.validationUrl.label',
            ]);
        }

        $organization = $builder->getData();
        $this->prepareMembersField($builder, $organization);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Organization::class,
        ]);
    }

    private function prepareMembersField(
        FormBuilderInterface $builder,
        OrganizationInterface $organization
    ) {
        $checker = $this->authorizationChecker;
        $person = $this->tokenStorage->getToken()->getUser();

        if (!$checker->isGranted('ROLE_ORGANIZATIONS_MANAGE_MEMBERS_ANY_ORG') &&
            !$checker->isGranted('ROLE_ORGANIZATIONS_MANAGE_MEMBERS')
        ) {
            return;
        }

        $members = $organization->getMembers();
        if ($members instanceof Collection) {
            $members = $members->toArray();
        }
        if (false === array_search($person, $members) &&
            !$checker->isGranted('ROLE_ORGANIZATIONS_MANAGE_MEMBERS_ANY_ORG')
        ) {
            return;
        }

        $builder->addEventListener(
            FormEvents::PRE_SUBMIT,
            function (FormEvent $event) {
                $entity = $event->getData();
                $form = $event->getForm();

                $qb = function (EntityRepository $er) use ($entity) {
                    $sql = $er->createQueryBuilder('u');
                    if (!empty($entity['members'])) {
                        $sql->where('u.id in (:members)');
{'fraction_non_alphanumeric': 0.12374418034795394, 'fraction_numerical': 0.00036755697133055625, 'mean_word_length': 2.8908484270734034, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 82, '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': '2887481', 'n_tokens_mistral': 2293, 'n_tokens_neox': 2241, 'n_words': 383}