<commit_before><?php

namespace App\EventListener\Auth;

use App\Auth\AuthManager;
use App\Auth\LoginManagerInterface;
use App\Auth\AccountManagerInterface;
use Prob\Rewrite\Request;
use Prob\Handler\ProcInterface;
use App\EventListener\Auth\Exception\PermissionDenied;

class Validator
{
    /**
     * @var AccountManagerInterface
     */
    private $accountManager;

    /**
     * @var LoginManagerInterface
     */
    private $loginManager;

    private $controllerPermission = [];

    /**
     * @var ProcInterface
     */
    private $controller;

    public function __construct(array $controllerPermission)
    {
        $this->accountManager = AuthManager::getInstance()->getDefaultAccountManager();
        $this->loginManager = AuthManager::getInstance()->getDefaultLoginManager();

        $this->controllerPermission = $controllerPermission;
    }

    public function validate($controller)
    {
        $this->controller = $controller;

        if ($this->isAllowPermission() === false) {
            throw new PermissionDenied('This operation is not allowed');
        }
    }

    private function isAllowPermission()
    {
        if ($this->isExistControllerPermissionConfig() === false) {
            return AuthManager::getInstance()->isDefaultAllow();
        }

        return $this->isAllowRole();
    }

    private function isExistControllerPermissionConfig()
    {
        return isset($this->controllerPermission[$this->controller->getName()]);
    }

    private function isAllowRole()
    {
        if (isset($this->controllerPermission[$this->controller->getName()]['role']) === false) {
            return false;
        }

        return in_array(
                $this->accountManager->getRole($this->loginManager->getLoggedAccountId()),
                $this->controllerPermission[$this->controller->getName()]['role']
        );
    }
}<commit_msg>Role 권한이 무조건 deny되는 문제 수정<commit_after><?php

namespace App\EventListener\Auth;

use App\Auth\AuthManager;
use App\Auth\LoginManagerInterface;
use App\Auth\AccountManagerInterface;
use Prob\Rewrite\Request;
use Prob\Handler\ProcInterface;
use App\EventListener\Auth\Exception\PermissionDenied;

class Validator
{
    /**
     * @var AccountManagerInterface
     */
    private $accountManager;

    /**
     * @var LoginManagerInterface
     */
    private $loginManager;

    private $controllerPermission = [];

    /**
     * @var ProcInterface
     */
    private $controller;

    public function __construct(array $controllerPermission)
    {
        $this->accountManager = AuthManager::getInstance()->getDefaultAccountManager();
        $this->loginManager = AuthManager::getInstance()->getDefaultLoginManager();

        $this->controllerPermission = $controllerPermission;
    }

    public function validate($controller)
    {
        $this->controller = $controller;

        if ($this->isAllowPermission() === false) {
            throw new PermissionDenied('This operation is not allowed');
        }
    }

    private function isAllowPermission()
    {
        if ($this->isExistControllerPermissionConfig() === false) {
            return AuthManager::getInstance()->isDefaultAllow();
        }

        return $this->isAllowRole();
    }

    private function isExistControllerPermissionConfig()
    {
        return isset($this->controllerPermission[$this->controller->getName()]);
    }

    private function isAllowRole()
    {
        if (isset($this->controllerPermission[$this->controller->getName()]['role']) === false) {
            return false;
        }

        $accountRoles = $this->accountManager->getRole($this->loginManager->getLoggedAccountId());
        $allowRoles = $this->controllerPermission[$this->controller->getName()]['role'];

        foreach ($accountRoles as $role) {
            if (in_array($role, $allowRoles) === true) {
                return true;
            }
        }

        return false;
    }
}
{'fraction_non_alphanumeric': 0.12778904665314403, 'fraction_numerical': 0.0, 'mean_word_length': 2.8982213438735176, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6701903', 'n_tokens_mistral': 1028, 'n_tokens_neox': 1004, 'n_words': 183}