<commit_before><?php

namespace Bkstg\CoreBundle\Security;

use Bkstg\CoreBundle\User\UserInterface;
use Bkstg\NoticeBoardBundle\Entity\Post;
use MidnightLuke\GroupSecurityBundle\Model\GroupableInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;

/**
 * This class should be extended and overridden for groupable entities.
 */
abstract class GroupableEntityVoter extends Voter
{
    protected $decision_manager;

    public function __construct(AccessDecisionManagerInterface $decision_manager)
    {
        $this->decision_manager = $decision_manager;
    }

    const VIEW = 'view';
    const EDIT = 'edit';

    /**
     * {@inheritdoc}
     */
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
    {
        $user = $token->getUser();

        if (!$user instanceof UserInterface) {
            return false;
        }

        $groupable = $subject;

        switch ($attribute) {
            case self::VIEW:
                return $this->canView($groupable, $token);
            case self::EDIT:
                return $this->canEdit($groupable, $token);
        }
    }

    /**
     * Grants edit access to group admins for groupable entities.
     */
    public function canEdit(GroupableInterface $groupable, TokenInterface $token)
    {
        $user = $token->getUser();
        foreach ($groupable->getGroups() as $group) {
            if ($this->decision_manager->decide($token, ['GROUP_ROLE_ADMIN'], $group)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Grants view access to group members for groupable entities.
     */
    public function canView(GroupableInterface $groupable, TokenInterface $token)<commit_msg>Loosening up the groupable entity voter.<commit_after><?php

namespace Bkstg\CoreBundle\Security;

use Bkstg\CoreBundle\User\UserInterface;
use Bkstg\NoticeBoardBundle\Entity\Post;
use MidnightLuke\GroupSecurityBundle\Model\GroupableInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;

/**
 * This class should be extended and overridden for groupable entities.
 *
 * Provides a simple base for groupable entities where group users can view
 * entities within the group, while group editors can edit entities.
 */
abstract class GroupableEntityVoter extends Voter
{
    const VIEW = 'view';
    const EDIT = 'edit';

    protected $decision_manager;

    /**
     * Build a new groupable entity voter.
     *
     * @param AccessDecisionManagerInterface $decision_manager The decision manager service.
     */
    public function __construct(AccessDecisionManagerInterface $decision_manager)
    {
        $this->decision_manager = $decision_manager;
    }

    /**
     * {@inheritdoc}
     */
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
    {
        $user = $token->getUser();

        if (!$user instanceof UserInterface) {
            return false;
        }

        $groupable = $subject;

        switch ($attribute) {
            case self::VIEW:
                return $this->canView($groupable, $token);
            case self::EDIT:
                return $this->canEdit($groupable, $token);
        }
    }

    /**
     * Grants edit access to group editors for groupable entities.
     */
    public function canEdit(GroupableInterface $groupable, TokenInterface $token)
    {
        $user = $token->getUser();
        foreach ($groupable->getGroups() as $group) {
            if ($this->decision_manager->decide($token, ['GROUP_ROLE_EDITOR'], $group)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Grants view access to group members for groupable entities.
     */
    public function canView(GroupableInterface $groupable, TokenInterface $token)
{'fraction_non_alphanumeric': 0.10121065375302664, 'fraction_numerical': 0.0, 'mean_word_length': 2.825, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '5878998', 'n_tokens_mistral': 1134, 'n_tokens_neox': 1097, 'n_words': 273}