|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Macros.php |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright More in license.md |
|
6
|
|
|
* @license https://www.ipublikuj.eu |
|
7
|
|
|
* @author Adam Kadlec <[email protected]> |
|
8
|
|
|
* @package iPublikuj:Permissions! |
|
9
|
|
|
* @subpackage Latte |
|
10
|
|
|
* @since 1.0.0 |
|
11
|
|
|
* |
|
12
|
|
|
* @date 10.10.14 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
|
16
|
|
|
|
|
17
|
|
|
namespace IPub\Permissions\Latte; |
|
18
|
|
|
|
|
19
|
|
|
use Nette\Utils; |
|
20
|
|
|
|
|
21
|
|
|
use Latte; |
|
22
|
|
|
use Latte\Compiler; |
|
23
|
|
|
use Latte\MacroNode; |
|
24
|
|
|
use Latte\Macros\MacroSet; |
|
25
|
|
|
use Latte\PhpWriter; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Permissions latte macros definition |
|
29
|
|
|
* |
|
30
|
|
|
* @package iPublikuj:Permissions! |
|
31
|
|
|
* @subpackage Latte |
|
32
|
|
|
* |
|
33
|
|
|
* @author Adam Kadlec <[email protected]> |
|
34
|
|
|
*/ |
|
35
|
|
|
final class Macros extends MacroSet |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @param Compiler $compiler |
|
39
|
|
|
* |
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function install(Compiler $compiler) : void |
|
43
|
|
|
{ |
|
44
|
|
|
$me = new static($compiler); |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {ifAllowed role => 'some role', resource => 'some resource'}...{/ifAllowed} |
|
48
|
|
|
*/ |
|
49
|
|
|
$me->addMacro('ifAllowed', [$me, 'macroIfAllowed'], [$me, 'macroIfAllowedEnd']); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* <a n:allowedHref="Presenter:action">...</a> |
|
53
|
|
|
*/ |
|
54
|
|
|
$me->addMacro('allowedHref', [$me, 'macroIfAllowedLink'], [$me, 'macroIfAllowedLinkEnd']); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param MacroNode $node |
|
59
|
|
|
* @param PhpWriter $writer |
|
60
|
|
|
* |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function macroIfAllowed(MacroNode $node, PhpWriter $writer) : string |
|
64
|
|
|
{ |
|
65
|
|
|
return $writer->write('if($presenter->context->getByType("IPub\Permissions\Access\LatteChecker")->isAllowed(%node.array)){'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param MacroNode $node |
|
70
|
|
|
* @param PhpWriter $writer |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function macroIfAllowedEnd(MacroNode $node, PhpWriter $writer) : string |
|
75
|
|
|
{ |
|
76
|
|
|
return $writer->write('}'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param MacroNode $node |
|
81
|
|
|
* @param PhpWriter $writer |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
* |
|
85
|
|
|
* @throws Latte\CompileException |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function macroIfAllowedLink(MacroNode $node, PhpWriter $writer) : string |
|
88
|
|
|
{ |
|
89
|
|
|
// This macro is allowed only as n:macro in <a ></a> element |
|
90
|
|
|
if (Utils\Strings::lower($node->htmlNode->name) !== 'a') { |
|
91
|
|
|
throw new Latte\CompileException("Macro n:allowedHref is allowed only in link element, you used it in {$node->htmlNode->name}."); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $writer->write('if($presenter->context->getByType("IPub\Permissions\Access\LinkChecker")->isAllowed(%node.word)){'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param MacroNode $node |
|
99
|
|
|
* @param PhpWriter $writer |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function macroIfAllowedLinkEnd(MacroNode $node, PhpWriter $writer) : string |
|
104
|
|
|
{ |
|
105
|
|
|
return $writer->write('}'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param MacroNode $node |
|
110
|
|
|
* @param PhpWriter $writer |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
public function macroHref(MacroNode $node, PhpWriter $writer) : string |
|
115
|
|
|
{ |
|
116
|
|
|
return $writer->write(' ?> href="<?php echo %escape(' . ($node->name === 'plink' ? '$_presenter' : '$_control') . '->link(%node.word, %node.array?)) ?>"<?php '); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|