<commit_before><?php

namespace Ilios\AuthenticationBundle\Voter;

use Ilios\CoreBundle\Entity\CurriculumInventoryReportInterface;
use Ilios\CoreBundle\Entity\Manager\PermissionManagerInterface;
use Ilios\CoreBundle\Entity\UserInterface;

/**
 * Class CurriculumInventoryReportVoter
 * @package Ilios\AuthenticationBundle\Voter
 */
class CurriculumInventoryReportVoter extends AbstractVoter
{
    /**
     * @var PermissionManagerInterface
     */
    protected $permissionManager;

    /**
     * @param PermissionManagerInterface $permissionManager
     */
    public function __construct(PermissionManagerInterface $permissionManager)
    {
        $this->permissionManager = $permissionManager;
    }

    /**
     * {@inheritdoc}
     */
    protected function getSupportedClasses()
    {
        return array('Ilios\CoreBundle\Entity\CurriculumInventoryReportInterface');
    }

    /**
     * @param string $attribute
     * @param CurriculumInventoryReportInterface $report
     * @param UserInterface $user
     * @return bool
     */
    protected function isGranted($attribute, $report, $user = null)
    {
        if (!$user instanceof UserInterface) {
            return false;
        }

        switch ($attribute) {
            case self::VIEW:
                return $this->isViewGranted($report, $user);
                break;
            case self::CREATE:
                return $this->isCreateGranted($report, $user);
                break;
            case self::EDIT:
                return $this->isEditGranted($report, $user);
                break;
            case self::DELETE:
                return $this->isDeleteGranted($report, $user);
        }

        return false;
    }

    /**
     * @param CurriculumInventoryReportInterface $report
     * @param UserInterface $user
     * @return bool
     */
    protected function isViewGranted($report, $user)
    {
        // Only grant VIEW permissions to users with at least one of
        // 'Course Director' and 'Developer' roles.
        // - and -
        // the user must be associated with the school owning the report's program
        // either by its primary school attribute
        //     - or - by READ rights for the school
        // via the permissions system.
        return (
            $this->userHasRole($user, ['Course Director', 'Developer'])
            && (
                $this->schoolsAreIdentical($user->getSchool(), $report->getProgram()->getSchool())
                || $this->permissionManager->userHasReadPermissionToSchool(
                    $user,
                    $report->getProgram()->getSchool()
                )
            )
        );
    }

    /**
     * @param CurriculumInventoryReportInterface $report
     * @param UserInterface $user
     * @return bool
     */
    protected function isEditGranted($report, $user)
    {
        // HALT!
        // Reports cannot be edited once they have been exported.
        if ($report->getExport()) {
            return false;
        }
        return $this->isCreateGranted($report, $user);
    }

    /**
     * @param CurriculumInventoryReportInterface $report
     * @param UserInterface $user
     * @return bool
     */
    protected function isCreateGranted($report, $user)
    {
        // Only grant CREATE, permissions to users with at least one of
        // 'Course Director' and 'Developer' roles.
        // - and -
        // the user must be associated with the school owning the report's program
        // either by its primary school attribute
        //     - or - by WRITE rights for the school
        // via the permissions system.
        return (
            $this->userHasRole($user, ['Course Director', 'Developer'])
            && (
                $this->schoolsAreIdentical($user->getSchool(), $report->getProgram()->getSchool())
                || $this->permissionManager->userHasWritePermissionToSchool(
                    $user,
                    $report->getProgram()->getSchool()
                ))
        );
    }

    /**
     * @param CurriculumInventoryReportInterface $report
     * @param UserInterface $user
     * @return bool
     */
    protected function isDeleteGranted($report, $user)
    {<commit_msg>safely get school from ci report in report voter.<commit_after><?php

namespace Ilios\AuthenticationBundle\Voter;

use Ilios\CoreBundle\Entity\CurriculumInventoryReportInterface;
use Ilios\CoreBundle\Entity\Manager\PermissionManagerInterface;
use Ilios\CoreBundle\Entity\UserInterface;

/**
 * Class CurriculumInventoryReportVoter
 * @package Ilios\AuthenticationBundle\Voter
 */
class CurriculumInventoryReportVoter extends AbstractVoter
{
    /**
     * @var PermissionManagerInterface
     */
    protected $permissionManager;

    /**
     * @param PermissionManagerInterface $permissionManager
     */
    public function __construct(PermissionManagerInterface $permissionManager)
    {
        $this->permissionManager = $permissionManager;
    }

    /**
     * {@inheritdoc}
     */
    protected function getSupportedClasses()
    {
        return array('Ilios\CoreBundle\Entity\CurriculumInventoryReportInterface');
    }

    /**
     * @param string $attribute
     * @param CurriculumInventoryReportInterface $report
     * @param UserInterface $user
     * @return bool
     */
    protected function isGranted($attribute, $report, $user = null)
    {
        if (!$user instanceof UserInterface) {
            return false;
        }

        switch ($attribute) {
            case self::VIEW:
                return $this->isViewGranted($report, $user);
                break;
            case self::CREATE:
                return $this->isCreateGranted($report, $user);
                break;
            case self::EDIT:
                return $this->isEditGranted($report, $user);
                break;
            case self::DELETE:
                return $this->isDeleteGranted($report, $user);
        }

        return false;
    }

    /**
     * @param CurriculumInventoryReportInterface $report
     * @param UserInterface $user
     * @return bool
     */
    protected function isViewGranted($report, $user)
    {
        // Only grant VIEW permissions to users with at least one of
        // 'Course Director' and 'Developer' roles.
        // - and -
        // the user must be associated with the school owning the report's program
        // either by its primary school attribute
        //     - or - by READ rights for the school
        // via the permissions system.
        return (
            $this->userHasRole($user, ['Course Director', 'Developer'])
            && (
                $this->schoolsAreIdentical($user->getSchool(), $report->getSchool())
                || $this->permissionManager->userHasReadPermissionToSchool(
                    $user,
                    $report->getSchool()
                )
            )
        );
    }

    /**
     * @param CurriculumInventoryReportInterface $report
     * @param UserInterface $user
     * @return bool
     */
    protected function isEditGranted($report, $user)
    {
        // HALT!
        // Reports cannot be edited once they have been exported.
        if ($report->getExport()) {
            return false;
        }
        return $this->isCreateGranted($report, $user);
    }

    /**
     * @param CurriculumInventoryReportInterface $report
     * @param UserInterface $user
     * @return bool
     */
    protected function isCreateGranted($report, $user)
    {
        // Only grant CREATE, permissions to users with at least one of
        // 'Course Director' and 'Developer' roles.
        // - and -
        // the user must be associated with the school owning the report's program
        // either by its primary school attribute
        //     - or - by WRITE rights for the school
        // via the permissions system.
        return (
            $this->userHasRole($user, ['Course Director', 'Developer'])
            && (
                $this->schoolsAreIdentical($user->getSchool(), $report->getSchool())
                || $this->permissionManager->userHasWritePermissionToSchool(
                    $user,
                    $report->getSchool()
                ))
        );
    }

    /**
     * @param CurriculumInventoryReportInterface $report
     * @param UserInterface $user
     * @return bool
     */
    protected function isDeleteGranted($report, $user)
    {
{'fraction_non_alphanumeric': 0.10435915659796256, 'fraction_numerical': 0.0, 'mean_word_length': 2.011055634807418, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5564843', 'n_tokens_mistral': 2220, 'n_tokens_neox': 2110, 'n_words': 536}