Created
July 28, 2017 11:34
-
-
Save matej21/84554515cb83d8c5f56e01978f697553 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types = 1); | |
namespace App\Core\Latte; | |
use App\ImplementationException; | |
use Latte\Compiler; | |
use Latte\MacroNode; | |
use Latte\Macros\MacroSet; | |
use Latte\PhpWriter; | |
class SecurityMacros extends MacroSet | |
{ | |
public static function install(Compiler $compiler) | |
{ | |
$macros = new self($compiler); | |
$macros->addMacro('ifAllowed', [$macros, 'ifAllowed'], '}'); | |
} | |
public function ifAllowed(MacroNode $node, PhpWriter $writer) | |
{ | |
$scope = $node->tokenizer->fetchWord(); | |
$resource = $node->tokenizer->fetchWord(); | |
$action = $node->tokenizer->fetchWord(); | |
if ($node->tokenizer->fetchWord() !== FALSE) { | |
throw new ImplementationException("Macro ifAllowed accepts only 2 or 3 arguments."); | |
} | |
if ($action === FALSE) { | |
$action = $resource; | |
$resource = $scope; | |
$scope = FALSE; | |
} | |
$resource = 'App\\Model\\' . $resource; | |
return 'if ($this->global->uiPresenter->isAllowed(' | |
. ($scope ? $writer->formatWord($scope) : 'new App\Model\GlobalScope()') . ', [' | |
. $writer->formatWord($resource) . ', ' | |
. $writer->formatWord($action) | |
. '])) {'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment