<commit_before><?php

namespace Bbatsche\Entrust\Traits;

use Bbatsche\Entrust\EntrustFacade as Entrust;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\App;

trait EntrustControllerTrait
{
    protected $entrustPerms = [];
    protected $entrustRoles = [];

    protected $entrustAllowMissing = true;
    protected $entrustRequireAll   = false;

    protected $entrustPermissionCallback = '';
    protected $entrustRoleCallback = '';

    public function entrustPermissionFilter(Route $route, Request $request)
    {
        return $this->handleEntrustFilter('entrustPerms', 'entrustPermissionCallback', 'can', $route, $request);
    }

    public function entrustRoleFilter(Route $route, Request $request)
    {
        return $this->handleEntrustFilter('entrustRoles', 'entrustRoleCallback', 'is', $route, $request);
    }

    private function handleEntrustFilter($collectionName, $callbackName, $entrustMethod, $route, $request)
    {
        $filterPassed  = $this->entrustAllowMissing;
        $entrustNames  = null;
        $entrustFailed = array();

        list($class, $method) = explode('@', $route->getActionName());

        $collection = $this->$collectionName;

        if (!empty($collection[$method])) {
            $entrustNames = $collection[$method];

            if (is_array($entrustNames)) {
                $entrustMethod .= $this->entrustRequireAll ? 'All' : 'Any';

                if ($user = Entrust::user()) {
                    $filterPassed = $user->$entrustMethod($entrustNames, $entrustFailed);
                } else {
                    $filterPassed = false;
                    $entrustFailed = $entrustNames;
                }
            } else {
                $filterPassed = Entrust::$entrustMethod($entrustNames);
            }
        }

        if (!$filterPassed) {
            if (!empty($this->$callbackName) && is_string($this->$callbackName)) {
                // callbackName is the name of a function

                $callback = array($this, $this->$callbackName);
            } elseif (is_callable($this->$callbackName)) {
                // callbackName is a closure (or I guess some other callable type?)

                $callback = $this->$callbackName;
            } else {
                App::abort(403);
            }

            return call_user_func($callback, $method, $entrustFailed, $entrustNames, $route, $request);<commit_msg>Allow controllers to use actual functions as filter callbacks<commit_after><?php

namespace Bbatsche\Entrust\Traits;

use Bbatsche\Entrust\EntrustFacade as Entrust;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\App;

trait EntrustControllerTrait
{
    protected $entrustPerms = [];
    protected $entrustRoles = [];

    protected $entrustAllowMissing = true;
    protected $entrustRequireAll   = false;

    protected $entrustPermissionCallback = '';
    protected $entrustRoleCallback = '';

    public function entrustPermissionFilter(Route $route, Request $request)
    {
        return $this->handleEntrustFilter('entrustPerms', 'entrustPermissionCallback', 'can', $route, $request);
    }

    public function entrustRoleFilter(Route $route, Request $request)
    {
        return $this->handleEntrustFilter('entrustRoles', 'entrustRoleCallback', 'is', $route, $request);
    }

    private function handleEntrustFilter($collectionName, $callbackName, $entrustMethod, $route, $request)
    {
        $filterPassed  = $this->entrustAllowMissing;
        $entrustNames  = null;
        $entrustFailed = array();

        list($class, $method) = explode('@', $route->getActionName());

        $collection = $this->$collectionName;

        if (!empty($collection[$method])) {
            $entrustNames = $collection[$method];

            if (is_array($entrustNames)) {
                $entrustMethod .= $this->entrustRequireAll ? 'All' : 'Any';

                if ($user = Entrust::user()) {
                    $filterPassed = $user->$entrustMethod($entrustNames, $entrustFailed);
                } else {
                    $filterPassed = false;
                }
            } elseif(!($filterPassed = Entrust::$entrustMethod($entrustNames))) {
                // Filter failed; reassign failed values with required values since it was singular
                $entrustFailed = $entrustNames;
            }
        }

        if (!$filterPassed) {
            $reflection = new \ReflectionClass($this);

            if (!empty($this->$callbackName) && is_string($this->$callbackName)) {
                // callbackName is the name of a function

                $callback = array($this, $this->$callbackName);
            } elseif (is_callable($this->$callbackName)) {
                // callbackName is a closure

                $callback = $this->$callbackName;
            } elseif ($reflection->hasMethod($callbackName)) {
                // callbackName is a function in the class

                $callback = array($this, $callbackName);
            } else {
                App::abort(403);
            }

            return call_user_func($callback, $method, $entrustFailed, $entrustNames, $route, $request);
{'fraction_non_alphanumeric': 0.12447257383966245, 'fraction_numerical': 0.0011507479861910242, 'mean_word_length': 2.495308310991957, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, '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': '361626', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1397, 'n_words': 296}