1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Menu\Matcher\Voter; |
15
|
|
|
|
16
|
|
|
use Knp\Menu\ItemInterface; |
17
|
|
|
use Knp\Menu\Matcher\Voter\VoterInterface; |
18
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Admin menu voter based on extra `admin`. |
24
|
|
|
* |
25
|
|
|
* @author Samusev Andrey <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class AdminVoter implements VoterInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var RequestStack |
31
|
|
|
*/ |
32
|
|
|
private $requestStack; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Request |
36
|
|
|
*/ |
37
|
|
|
private $request = null; |
38
|
|
|
|
39
|
|
|
public function __construct(RequestStack $requestStack = null) |
40
|
|
|
{ |
41
|
|
|
$this->requestStack = $requestStack; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @deprecated since version 3.x. Pass a RequestStack to the constructor instead. |
46
|
|
|
* |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
|
|
public function setRequest($request) |
50
|
|
|
{ |
51
|
|
|
@trigger_error( |
|
|
|
|
52
|
|
|
sprintf( |
53
|
|
|
'The %s() method is deprecated since version 3.x. |
54
|
|
|
Pass a Symfony\Component\HttpFoundation\RequestStack |
55
|
|
|
in the constructor instead.', |
56
|
|
|
__METHOD__), |
57
|
|
|
E_USER_DEPRECATED |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$this->request = $request; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function matchItem(ItemInterface $item) |
66
|
|
|
{ |
67
|
|
|
$admin = $item->getExtra('admin'); |
68
|
|
|
|
69
|
|
|
$request = $this->request; |
70
|
|
|
if (null !== $this->requestStack) { |
71
|
|
|
$request = $this->requestStack->getMasterRequest(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($admin instanceof AdminInterface |
75
|
|
|
&& $admin->hasRoute('list') && $admin->hasAccess('list') |
76
|
|
|
&& $request |
77
|
|
|
) { |
78
|
|
|
$requestCode = $request->get('_sonata_admin'); |
79
|
|
|
|
80
|
|
|
if ($admin->getCode() === $requestCode) { |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
foreach ($admin->getChildren() as $child) { |
85
|
|
|
if ($child->getBaseCodeRoute() === $requestCode) { |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$route = $item->getExtra('route'); |
92
|
|
|
if ($route && $request && $route == $request->get('_route')) { |
93
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return null; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: