1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KI\CoreBundle\Controller; |
4
|
|
|
|
5
|
|
|
use KI\UserBundle\Entity\User; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
7
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
10
|
|
|
|
11
|
|
|
class CoreController extends Controller |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var User |
15
|
|
|
*/ |
16
|
|
|
protected $user = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Initialise le controleur |
20
|
|
|
*/ |
21
|
|
|
public function setUser() |
22
|
|
|
{ |
23
|
|
|
$token = $this->get('security.token_storage')->getToken(); |
24
|
|
|
$this->user = $token ? $token->getUser() : null; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Permet de savoir si un utilisateur a un rôle ou non |
29
|
|
|
* @param string $role |
30
|
|
|
* @return boolean |
31
|
|
|
*/ |
32
|
|
|
protected function is($role) |
33
|
|
|
{ |
34
|
|
|
return $this->get('security.authorization_checker')->isGranted('ROLE_'.$role); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Éjecte tous les utilisateurs ne respectant pas la condition |
39
|
|
|
* @param boolean $bool |
40
|
|
|
* @return boolean |
41
|
|
|
*/ |
42
|
|
|
protected function trust($bool) |
43
|
|
|
{ |
44
|
|
|
if ($this->user && $bool || $this->is('ADMIN')) { |
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
throw new AccessDeniedException('Accès refusé'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Génère une réponse au format JSON en parsant les propriétés avec le FOSRestBundle |
53
|
|
|
* @param mixed $data Le contenu à renvoyer |
54
|
|
|
* @param int $status Le code d'erreur HTTP à renvoyer |
55
|
|
|
* @param array $headers Des headers spécifiques si nécéssaire |
56
|
|
|
* @return Response |
57
|
|
|
*/ |
58
|
|
|
public function json($data, $status = 200, $headers = [], $context = []) |
59
|
|
|
{ |
60
|
|
|
return new JsonResponse( |
61
|
|
|
$this->get('jms_serializer')->serialize($data, 'json'), |
62
|
|
|
$status, |
63
|
|
|
$headers, |
64
|
|
|
true |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Génère une réponse plain text |
70
|
|
|
* @param mixed $data Le contenu à renvoyer |
71
|
|
|
* @param int $code Le code d'erreur HTTP à renvoyer |
72
|
|
|
* @param array $headers Des headers spécifiques si nécéssaire |
73
|
|
|
* @return Response |
74
|
|
|
*/ |
75
|
|
|
public function htmlResponse($data, $code = 200, array $headers = []) |
76
|
|
|
{ |
77
|
|
|
return new Response($data, $code, $headers); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Génère la réponse relative au traitement d'un formulaire |
82
|
|
|
* @param array $data Le formulaire traité |
83
|
|
|
* @param object $parent Éventuellement l'objet parent |
|
|
|
|
84
|
|
|
* @return Response |
85
|
|
|
*/ |
86
|
|
|
public function formJson($data) |
87
|
|
|
{ |
88
|
|
|
switch ($data['code']) { |
89
|
|
|
case 400: |
90
|
|
|
return $this->json($data['form'], $data['code']); |
91
|
|
|
case 204: |
92
|
|
|
default: |
93
|
|
|
return $this->json($data['item'], $data['code']); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Retourne une configuration de uPont |
99
|
|
|
* @param string $path La clé de configuration |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getConfig($path) |
103
|
|
|
{ |
104
|
|
|
$config = $this->container->getParameter('upont'); |
105
|
|
|
|
106
|
|
|
if (!empty($path)) { |
107
|
|
|
$keys = preg_split('/[\.]/', $path); |
108
|
|
|
foreach ($keys as $key) { |
109
|
|
|
if (isset($config[$key])) { |
110
|
|
|
$config = $config[$key]; |
111
|
|
|
} else { |
112
|
|
|
throw new \OutOfBoundsException; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $config; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.