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