Skip to content

Instantly share code, notes, and snippets.

@matej21
Created July 28, 2017 11:34
Show Gist options
  • Save matej21/84554515cb83d8c5f56e01978f697553 to your computer and use it in GitHub Desktop.
Save matej21/84554515cb83d8c5f56e01978f697553 to your computer and use it in GitHub Desktop.
<?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