|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Majora\Bundle\FrameworkExtraBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
6
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Base trait for controllers traits. |
|
10
|
|
|
* |
|
11
|
|
|
* @property \Symfony\Component\DependencyInjection\ContainerInterface $container |
|
12
|
|
|
*/ |
|
13
|
|
|
trait ControllerTrait |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* return controller security mapping as "intention" => "credentials" |
|
17
|
|
|
* |
|
18
|
|
|
* @example : |
|
19
|
|
|
* return array( |
|
20
|
|
|
* 'list' => array('fetch'), |
|
21
|
|
|
* 'get' => array('read'), |
|
22
|
|
|
* 'create' => array('write'), |
|
23
|
|
|
* 'udpate' => array('write') |
|
24
|
|
|
* ); |
|
25
|
|
|
* |
|
26
|
|
|
* @see ControllerTrait::checkSecurity() |
|
27
|
|
|
* |
|
28
|
|
|
* @return array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function getSecurityMapping() |
|
31
|
|
|
{ |
|
32
|
|
|
return array(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* checks security for given intention |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $intention |
|
39
|
|
|
* @param mixed $resource |
|
40
|
|
|
* |
|
41
|
|
|
* @return boolean |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function checkSecurity($intention, $resource = null) |
|
44
|
|
|
{ |
|
45
|
|
|
$securityMapping = $this->getSecurityMapping(); |
|
46
|
|
|
|
|
47
|
|
|
return $this->container->get('security.authorization_checker')->isGranted( |
|
48
|
|
|
(array) (empty($securityMapping[$intention]) ? |
|
49
|
|
|
$intention : |
|
50
|
|
|
$securityMapping[$intention] |
|
51
|
|
|
), |
|
52
|
|
|
$resource |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Extract available query filter from request. |
|
58
|
|
|
* |
|
59
|
|
|
* @param Request $request |
|
60
|
|
|
* |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function extractQueryFilter(Request $request) |
|
64
|
|
|
{ |
|
65
|
|
|
return array_map( |
|
66
|
|
|
function ($value) { |
|
67
|
|
|
return array_filter(explode(',', trim($value, ',')), function ($var) { |
|
68
|
|
|
return !empty($var); |
|
69
|
|
|
}); |
|
70
|
|
|
}, |
|
71
|
|
|
$request->query->all() |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Retrieves entity for given id into given repository service. |
|
77
|
|
|
* |
|
78
|
|
|
* @param $entityId |
|
79
|
|
|
* @param $loaderId |
|
80
|
|
|
* |
|
81
|
|
|
* @return Object |
|
82
|
|
|
* |
|
83
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function retrieveOr404($entityId, $loaderId) |
|
86
|
|
|
{ |
|
87
|
|
|
if (!$this->container->has($loaderId)) { |
|
88
|
|
|
throw new NotFoundHttpException(sprintf('Unknow required loader : "%s"', |
|
89
|
|
|
$loaderId |
|
90
|
|
|
)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (!$entity = $this->container->get($loaderId)->retrieve($entityId)) { |
|
94
|
|
|
throw $this->create404($entityId, $loaderId); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $entity; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* create a formatted http not found exception. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $entityId |
|
104
|
|
|
* @param string $loaderId |
|
105
|
|
|
* |
|
106
|
|
|
* @return NotFoundHttpException |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function create404($entityId, $loaderId) |
|
109
|
|
|
{ |
|
110
|
|
|
return new NotFoundHttpException(sprintf('Entity with id "%s" not found%s.', |
|
111
|
|
|
$entityId, |
|
112
|
|
|
$this->container->getParameter('kernel.debug') ? |
|
113
|
|
|
sprintf(' : (looked into "%s")', $loaderId) : |
|
114
|
|
|
'' |
|
115
|
|
|
)); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|