Permalink
Browse files

added {ifAuthorized} macro

  • Loading branch information...
1 parent d200881 commit c49a52a334e5802b584d4138d22a30608c84c272 @juzna committed Mar 17, 2012
Showing with 49 additions and 1 deletion.
  1. +49 −1 Nette/Latte/Macros/UIMacros.php
@@ -47,6 +47,7 @@ class UIMacros extends MacroSet
public static function install(Latte\Compiler $compiler)
{
$me = new static($compiler);
+ $me->addMacro('ifAuthorized', array($me, 'macroIfAuthorized'), array($me, 'macroIfAuthorizedEnd'));
$me->addMacro('include', array($me, 'macroInclude'));
$me->addMacro('includeblock', array($me, 'macroIncludeBlock'));
$me->addMacro('extends', array($me, 'macroExtends'));
@@ -388,10 +389,46 @@ public function macroControl(MacroNode $node, PhpWriter $writer)
*/
public function macroLink(MacroNode $node, PhpWriter $writer)
{
- return $writer->write('echo %escape(%modify(' . ($node->name === 'plink' ? '$_presenter' : '$_control') . '->link(%node.word, %node.array?)))');
+ return $writer->write('echo %escape(%modify(' . ($node->name === 'plink' ? '$_presenter' : '$_control') . '->link(%node.word, %node.array?)));')
+ . 'if (isset($_l->notAuthorizedLinks)) Nette\Latte\Macros\UIMacros::checkLinkPermissions($_presenter, $_l)';
}
+ /**
+ * {ifAuthorized destination [,] [params]}
+ * {ifAuthorized}
+ */
+ public function macroIfAuthorized(MacroNode $node, PhpWriter $writer)
+ {
+ // Validate if it's not nested
+ $parent = $node->parentNode;
+ while ($parent) {
+ if ($parent->name === 'ifAuthorized') throw new CompileException("{ifAuthorized} already opened and cannot be nested");
+ $parent = $parent->parentNode;
+ }
+
+ if ($node->data->capture = ($node->args === '')) {
+ return '$_l->notAuthorizedLinks = 0; ob_start();';
+ } else {
+ return $writer->write('try { $_presenter->link(%node.word, %node.array?); } catch (Nette\Application\UI\InvalidLinkException $e) {}')
+ . 'if ($_presenter->checkRequestRequirements($_presenter->lastCreatedRequest)):';
+ }
+ }
+
+
+ /**
+ * {/ifAuthorized}
+ */
+ public function macroIfAuthorizedEnd(MacroNode $node, PhpWriter $writer)
+ {
+ if ($node->data->capture) {
+ return 'if($_l->notAuthorizedLinks) ob_end_clean(); else ob_end_flush();'
+ . '$_l->notAuthorizedLinks = NULL;';
+ } else {
+ return 'endif';
+ }
+ }
+
/**
* {ifCurrent destination [,] [params]}
@@ -522,4 +559,15 @@ public static function renderSnippets(Nette\Application\UI\Control $control, \st
}
}
+
+ /**
+ * Check if last generated link goes to authorized action
+ * @param Nette\Application\UI\Presenter
+ * @param stdClass
+ */
+ public static function checkLinkPermissions($presenter, $local) {
+ if ($presenter->checkRequestRequirements($presenter->lastCreatedRequest) !== TRUE) {
+ $local->notAuthorizedLinks++;
+ }
+ }
}

0 comments on commit c49a52a

Please sign in to comment.