|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* TPermission.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 common |
|
10
|
|
|
* @since 1.0.0 |
|
11
|
|
|
* |
|
12
|
|
|
* @date 13.10.14 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
|
16
|
|
|
|
|
17
|
|
|
namespace IPub\Permissions; |
|
18
|
|
|
|
|
19
|
|
|
use Nette\Application; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Helper trait |
|
23
|
|
|
* |
|
24
|
|
|
* @package iPublikuj:Permissions! |
|
25
|
|
|
* @subpackage common |
|
26
|
|
|
* |
|
27
|
|
|
* @author Adam Kadlec <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
1 |
|
trait TPermission |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var Configuration |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $permissionConfiguration; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var Access\ICheckRequirements |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $requirementsChecker; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param Access\ICheckRequirements $requirementsChecker |
|
43
|
|
|
* @param Configuration $configuration |
|
44
|
|
|
*/ |
|
45
|
|
|
public function injectPermission( |
|
46
|
|
|
Access\ICheckRequirements $requirementsChecker, |
|
47
|
|
|
Configuration $configuration |
|
48
|
|
|
) { |
|
49
|
1 |
|
$this->requirementsChecker = $requirementsChecker; |
|
50
|
1 |
|
$this->permissionConfiguration = $configuration; |
|
51
|
1 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param mixed $element |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
* |
|
58
|
|
|
* @throws Application\ForbiddenRequestException |
|
59
|
|
|
* @throws Application\UI\InvalidLinkException |
|
60
|
|
|
*/ |
|
61
|
|
|
public function checkRequirements($element) : void |
|
62
|
|
|
{ |
|
63
|
1 |
|
$redirectUrl = $this->permissionConfiguration->getRedirectUrl([ |
|
64
|
1 |
|
'backlink' => $this->storeRequest(), |
|
|
|
|
|
|
65
|
|
|
]); |
|
66
|
|
|
|
|
67
|
|
|
try { |
|
68
|
1 |
|
parent::checkRequirements($element); |
|
69
|
|
|
|
|
70
|
1 |
|
if (!$this->requirementsChecker->isAllowed($element)) { |
|
71
|
1 |
|
throw new Application\ForbiddenRequestException; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
} catch (Application\ForbiddenRequestException $ex) { |
|
|
|
|
|
|
75
|
1 |
|
if ($redirectUrl) { |
|
76
|
|
|
$this->getPresenter()->redirectUrl($redirectUrl); |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
} else { |
|
79
|
1 |
|
throw $ex; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
1 |
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.