1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TPermission.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license https://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec https://www.ipublikuj.eu |
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
|
|
|
use IPub\Permissions\Security; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Helper trait |
25
|
|
|
* |
26
|
|
|
* @package iPublikuj:Permissions! |
27
|
|
|
* @subpackage common |
28
|
|
|
* |
29
|
|
|
* @author Adam Kadlec <[email protected]> |
30
|
|
|
*/ |
31
|
1 |
|
trait TPermission |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var Configuration |
35
|
|
|
*/ |
36
|
|
|
protected $permissionConfiguration; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Access\ICheckRequirements |
40
|
|
|
*/ |
41
|
|
|
protected $requirementsChecker; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Access\ICheckRequirements $requirementsChecker |
45
|
|
|
* @param Configuration $configuration |
46
|
|
|
*/ |
47
|
|
|
public function injectPermission( |
48
|
|
|
Access\ICheckRequirements $requirementsChecker, |
49
|
|
|
Configuration $configuration |
50
|
|
|
) { |
51
|
1 |
|
$this->requirementsChecker = $requirementsChecker; |
52
|
1 |
|
$this->permissionConfiguration = $configuration; |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param mixed $element |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
* |
60
|
|
|
* @throws Application\ForbiddenRequestException |
61
|
|
|
*/ |
62
|
|
|
public function checkRequirements($element) : void |
63
|
|
|
{ |
64
|
1 |
|
$redirectUrl = $this->permissionConfiguration->getRedirectUrl([ |
65
|
1 |
|
'backlink' => $this->storeRequest(), |
|
|
|
|
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
try { |
69
|
1 |
|
parent::checkRequirements($element); |
70
|
|
|
|
71
|
1 |
|
if (!$this->requirementsChecker->isAllowed($element)) { |
72
|
1 |
|
throw new Application\ForbiddenRequestException; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
} catch (Application\ForbiddenRequestException $ex) { |
76
|
1 |
|
if ($redirectUrl) { |
77
|
|
|
$this->getPresenter()->redirectUrl($redirectUrl); |
|
|
|
|
78
|
|
|
|
79
|
|
|
} else { |
80
|
1 |
|
throw $ex; |
81
|
|
|
} |
82
|
|
|
} |
83
|
1 |
|
} |
84
|
|
|
} |
85
|
|
|
|
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
Idable
provides a methodequalsId
that 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.