Skip to content

Commit f3c39aa

Browse files
authored
Added validation for the ajax requests (#8)
* Added validation for the ajax requests * code style
1 parent a887a1e commit f3c39aa

File tree

3 files changed

+139
-17
lines changed

3 files changed

+139
-17
lines changed

Controller/WebUIController.php

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Symfony\Component\Intl\Intl;
1919
use Symfony\Component\Translation\MessageCatalogue;
2020
use Symfony\Component\Translation\Translator;
21+
use Translation\Bundle\Exception\MessageValidationException;
22+
use Translation\Bundle\Model\GuiMessageRepresentation;
2123
use Translation\Common\Exception\StorageException;
2224
use Translation\Symfony\Model\Message;
2325

@@ -110,19 +112,19 @@ public function showAction($configName, $locale, $domain)
110112
*/
111113
public function createAction(Request $request, $configName, $locale, $domain)
112114
{
113-
$json = $request->getContent();
114-
$data = json_decode($json, true);
115-
if (!isset($data['key']) || !isset($data['message'])) {
116-
throw new BadRequestHttpException('Payload must contain "key" and "message".');
115+
$storage = $this->get('php_translation.storage.file.'.$configName);
116+
try {
117+
$message = $this->getMessage($request, ['Create']);
118+
} catch (MessageValidationException $e) {
119+
return new Response($e->getMessage(), 400);
117120
}
118121

119-
$storage = $this->get('php_translation.storage.file.'.$configName);
120122
try {
121-
$storage->set($locale, $domain, $data['key'], $data['message']);
123+
$storage->set($locale, $domain, $message->getKey(), $message->getMessage());
122124
} catch (StorageException $e) {
123125
throw new BadRequestHttpException(sprintf(
124126
'Key "%s" does already exist for "%s" on domain "%s".',
125-
$data['key'],
127+
$message->getKey(),
126128
$locale,
127129
$domain
128130
), $e);
@@ -141,13 +143,13 @@ public function createAction(Request $request, $configName, $locale, $domain)
141143
*/
142144
public function editAction(Request $request, $configName, $locale, $domain)
143145
{
144-
$json = $request->getContent();
145-
$data = json_decode($json, true);
146-
if (!isset($data['key']) || !isset($data['message'])) {
147-
throw new BadRequestHttpException('Payload must contain "key" and "message".');
146+
try {
147+
$message = $this->getMessage($request, ['Edit']);
148+
} catch (MessageValidationException $e) {
149+
return new Response($e->getMessage(), 400);
148150
}
149151

150-
$this->get('php_translation.storage.file.'.$configName)->update($locale, $domain, $data['key'], $data['message']);
152+
$this->get('php_translation.storage.file.'.$configName)->update($locale, $domain, $message->getKey(), $message->getMessage());
151153

152154
return new Response('Translation updated');
153155
}
@@ -162,13 +164,13 @@ public function editAction(Request $request, $configName, $locale, $domain)
162164
*/
163165
public function deleteAction(Request $request, $configName, $locale, $domain)
164166
{
165-
$json = $request->getContent();
166-
$data = json_decode($json, true);
167-
if (!isset($data['key'])) {
168-
throw new BadRequestHttpException('Payload must contain "key".');
167+
try {
168+
$message = $this->getMessage($request, ['Delete']);
169+
} catch (MessageValidationException $e) {
170+
return new Response($e->getMessage(), 400);
169171
}
170172

171-
$this->get('php_translation.storage.file.'.$configName)->delete($locale, $domain, $data['key']);
173+
$this->get('php_translation.storage.file.'.$configName)->delete($locale, $domain, $message->getKey());
172174

173175
return new Response('Message was deleted');
174176
}
@@ -194,4 +196,30 @@ private function getConfiguration(&$configName)
194196

195197
return $config;
196198
}
199+
200+
/**
201+
* @param Request $request
202+
* @param array $validationGroups
203+
*
204+
* @return GuiMessageRepresentation
205+
*/
206+
private function getMessage(Request $request, array $validationGroups = [])
207+
{
208+
$json = $request->getContent();
209+
$data = json_decode($json, true);
210+
$message = new GuiMessageRepresentation();
211+
if (isset($data['key'])) {
212+
$message->setKey($data['key']);
213+
}
214+
if (isset($data['message'])) {
215+
$message->setMessage($data['message']);
216+
}
217+
218+
$errors = $this->get('validator')->validate($message, null, $validationGroups);
219+
if (count($errors) > 0) {
220+
throw MessageValidationException::create();
221+
}
222+
223+
return $message;
224+
}
197225
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <[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 Translation\Bundle\Exception;
13+
14+
use Translation\Common\Exception;
15+
16+
class MessageValidationException extends \Exception implements Exception
17+
{
18+
public static function create($message = 'Validation of the translation message failed.')
19+
{
20+
return new self($message);
21+
}
22+
}

Model/GuiMessageRepresentation.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <[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 Translation\Bundle\Model;
13+
14+
use Symfony\Component\Validator\Constraints as Assert;
15+
16+
/**
17+
* @author Tobias Nyholm <[email protected]>
18+
*/
19+
class GuiMessageRepresentation
20+
{
21+
/**
22+
* @var string
23+
* @Assert\NotBlank(groups={"Create", "Edit", "Delete"})
24+
*/
25+
private $key;
26+
27+
/**
28+
* @var string
29+
* @Assert\NotBlank(groups={"Create", "Edit"})
30+
*/
31+
private $message;
32+
33+
/**
34+
* @return string
35+
*/
36+
public function getKey()
37+
{
38+
return $this->key;
39+
}
40+
41+
/**
42+
* @param string $key
43+
*
44+
* @return GuiMessageRepresentation
45+
*/
46+
public function setKey($key)
47+
{
48+
$this->key = $key;
49+
50+
return $this;
51+
}
52+
53+
/**
54+
* @return string
55+
*/
56+
public function getMessage()
57+
{
58+
return $this->message;
59+
}
60+
61+
/**
62+
* @param string $message
63+
*
64+
* @return GuiMessageRepresentation
65+
*/
66+
public function setMessage($message)
67+
{
68+
$this->message = $message;
69+
70+
return $this;
71+
}
72+
}

0 commit comments

Comments
 (0)