From 5d63aa5eac78d9cd3705456e01c66c9182d82ded Mon Sep 17 00:00:00 2001 From: Maxandre Date: Wed, 31 Jul 2019 06:34:54 +0000 Subject: [PATCH 1/2] corrected bad translation --- templates/manager/manager.js.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/manager/manager.js.twig b/templates/manager/manager.js.twig index d58adbd..295f5b5 100644 --- a/templates/manager/manager.js.twig +++ b/templates/manager/manager.js.twig @@ -160,7 +160,7 @@ function handleLinkJson(data) { inserting: true }, { - title: "{{ 'app.linkmanager.table.uuid'|trans }}", + title: "{{ 'app.linkmanager.table.url'|trans }}", name: "url", type: "text", css: "jsgrid-inverted", From 6dfe715c4544b3c0f7a5de5efa7265e9148acd42 Mon Sep 17 00:00:00 2001 From: Maxandre Date: Thu, 1 Aug 2019 07:51:00 +0000 Subject: [PATCH 2/2] added translation + corrected refirect + ajax jsgrid validation --- config/routes/api.yaml | 8 ++ config/routes/routes.yaml | 6 ++ config/services.yaml | 5 +- public/js/fos_js_routes.json | 1 + public/js/jsgrid/et.js | 2 +- src/Controller/ApiController.php | 45 ++++++++++ src/Service/LinkManager.php | 12 ++- .../Constraints/NoRedirectValidator.php | 2 +- src/Validator/Constraints/ValidUuid.php | 4 +- .../Constraints/ValidUuidValidator.php | 26 +++++- templates/base.html.twig | 2 + templates/manager/manager.js.twig | 85 +++++++------------ templates/security/login.js.twig | 3 - 13 files changed, 132 insertions(+), 69 deletions(-) create mode 100644 config/routes/api.yaml create mode 100644 public/js/fos_js_routes.json create mode 100644 src/Controller/ApiController.php diff --git a/config/routes/api.yaml b/config/routes/api.yaml new file mode 100644 index 0000000..f8a36a1 --- /dev/null +++ b/config/routes/api.yaml @@ -0,0 +1,8 @@ +# config/routes/api.yaml + +api_validate_link: + path: /validate + controller: App\Controller\ApiController:validateLink + condition: "request.isXmlHttpRequest()" + options: + expose: true \ No newline at end of file diff --git a/config/routes/routes.yaml b/config/routes/routes.yaml index b6d33f3..01616f9 100644 --- a/config/routes/routes.yaml +++ b/config/routes/routes.yaml @@ -1,4 +1,5 @@ # config/routes/routes.yaml +# Dont forget to update ValidUuidValidator.php ! app_main_route_changelocale: path: /changelocale/{_locale} @@ -21,6 +22,11 @@ app_security: requirements: _locale: '%app.locales%' +# User management routes +app_api: + resource: 'api.yaml' + prefix: /api/ + app_handle_home_form: path: /{_locale}/form/home controller: App\Controller\HomeController:handleHomeForm diff --git a/config/services.yaml b/config/services.yaml index 9e10cce..cd9f928 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -54,4 +54,7 @@ services: $geo2IpLogger: '@monolog.logger.geo2ip' App\Service\Geolocalization\Geo2IpManager: - $geo2IpLogger: '@monolog.logger.geo2ip' \ No newline at end of file + $geo2IpLogger: '@monolog.logger.geo2ip' + + App\Validator\Constraints\ValidUuidValidator: + $locales: '%app.locales%' \ No newline at end of file diff --git a/public/js/fos_js_routes.json b/public/js/fos_js_routes.json new file mode 100644 index 0000000..d7430a1 --- /dev/null +++ b/public/js/fos_js_routes.json @@ -0,0 +1 @@ +{"base_url":"","routes":{"api_validate_link":{"tokens":[["text","\/api\/validate"]],"defaults":[],"requirements":[],"hosttokens":[],"methods":[],"schemes":[]}},"prefix":"","host":"localhost","port":"","scheme":"http"} \ No newline at end of file diff --git a/public/js/jsgrid/et.js b/public/js/jsgrid/et.js index c6abc39..ea1a78f 100644 --- a/public/js/jsgrid/et.js +++ b/public/js/jsgrid/et.js @@ -33,7 +33,7 @@ }, validators: { - required: { message: "Kohustuslik väli Field is required" }, + required: { message: "Kohustuslik väli" }, rangeLength: { message: "Väljal olev väärtus ei vasta seatud vahemikule" }, minLength: { message: "Väljal olev väärtus on liiga pikk" }, maxLength: { message: "Väljal olev väärtus on liiga lühike" }, diff --git a/src/Controller/ApiController.php b/src/Controller/ApiController.php new file mode 100644 index 0000000..1064edc --- /dev/null +++ b/src/Controller/ApiController.php @@ -0,0 +1,45 @@ +uriManager = $uriManager; + } + + public function validateLink(Request $request, ValidatorInterface $validator) + { + $link = new Link($request); + $link->setUuid($request->query->get('uuid')) + ->setUrl($this->uriManager->format($request->query->get('url'))); + + $violationList = $validator->validate($link); + + if (count($violationList) === 0) { + return new JsonResponse(['status' => 'ok']); + } + $messages = []; + foreach ($violationList as $violation) { + $messages[$violation->getPropertyPath()][] = $violation->getMessage(); + } + + return new JsonResponse(['status' => 'ko', 'messages' => $messages]); + } +} diff --git a/src/Service/LinkManager.php b/src/Service/LinkManager.php index d003c05..0ad0cff 100644 --- a/src/Service/LinkManager.php +++ b/src/Service/LinkManager.php @@ -21,6 +21,7 @@ class LinkManager private $em; private $validator; private $linkSecLogger; + private $uriManager; /** * LinkManager constructor. @@ -29,11 +30,16 @@ class LinkManager * @param ValidatorInterface $validator * @param Logger $linkSecLogger */ - public function __construct(EntityManagerInterface $em, ValidatorInterface $validator, Logger $linkSecLogger) - { + public function __construct( + EntityManagerInterface $em, + ValidatorInterface $validator, + Logger $linkSecLogger, + UriManager $uriManager + ) { $this->em = $em; $this->validator = $validator; $this->linkSecLogger = $linkSecLogger; + $this->uriManager = $uriManager; } /** @@ -101,7 +107,7 @@ public function createOrUpdate($linkArray, $request, $user) $link = new Link($request); $link ->setUuid($linkArray['uuid']) - ->setUrl($linkArray['url']) + ->setUrl($this->uriManager->format($linkArray['url'])) ->setUser($user); } else { $linkSave = clone $link; diff --git a/src/Validator/Constraints/NoRedirectValidator.php b/src/Validator/Constraints/NoRedirectValidator.php index 0715792..f832cd7 100644 --- a/src/Validator/Constraints/NoRedirectValidator.php +++ b/src/Validator/Constraints/NoRedirectValidator.php @@ -48,7 +48,7 @@ public function validate($value, Constraint $constraint) $redirectHost = $this->parser->parse($redirectUrl)['host']; // Build violation if the website redirects to another website. - if ($redirectHost !== $urlHost) { + if (!in_array($redirectHost, ['www.'.$urlHost, $urlHost])) { $this->context->buildViolation($constraint->message)->addViolation(); } } diff --git a/src/Validator/Constraints/ValidUuid.php b/src/Validator/Constraints/ValidUuid.php index 7c2ba30..a64c43c 100644 --- a/src/Validator/Constraints/ValidUuid.php +++ b/src/Validator/Constraints/ValidUuid.php @@ -15,6 +15,6 @@ */ class ValidUuid extends Constraint { - public $messageChar = "UUID should contain only letters and '-', '_', '~'"; - public $messageForbidden = 'This value is forbidden.'; + public $messageChar = "app.validator.uuid.chars"; + public $messageForbidden = 'app.validator.uuid.forbidden'; } diff --git a/src/Validator/Constraints/ValidUuidValidator.php b/src/Validator/Constraints/ValidUuidValidator.php index 29e2890..28fe58f 100644 --- a/src/Validator/Constraints/ValidUuidValidator.php +++ b/src/Validator/Constraints/ValidUuidValidator.php @@ -13,9 +13,27 @@ class ValidUuidValidator extends ConstraintValidator { - const FORBIDDEN = ['app', 'security', 'link', 'form']; + const FORBIDDEN = [ + 'app', + 'security', + 'link', + 'form', + 'api', + 'changelocale', + 'register', + 'js', + '_error', + '_wdt', + '_profiler' + ]; const REGEX_CHAR = '/^[A-z0-9-_~]+$/'; - const REGEX_FORBIDDEN = '/^(app|security|link|form)$/'; + + private $locales; + + public function __construct($locales) + { + $this->locales = explode('|', $locales); + } public function validate($value, Constraint $constraint) { @@ -24,7 +42,9 @@ public function validate($value, Constraint $constraint) $this->context->buildViolation($constraint->messageChar)->addViolation(); } - if (preg_match(self::REGEX_FORBIDDEN, $value, $matches)) { + if (in_array($value, self::FORBIDDEN) || + in_array($value, $this->locales) + ) { $this->context->buildViolation($constraint->messageForbidden)->addViolation(); } } diff --git a/templates/base.html.twig b/templates/base.html.twig index 3b4df85..3522192 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -31,6 +31,8 @@ + + {% if app.user is not null %}{% endif %} {% block javascripts %}{% endblock %} diff --git a/templates/manager/manager.js.twig b/templates/manager/manager.js.twig index 295f5b5..a529268 100644 --- a/templates/manager/manager.js.twig +++ b/templates/manager/manager.js.twig @@ -10,59 +10,6 @@ function handleLinkJson(data) { jsGrid.locale("{{ app.request.getLocale()|lower }}"); - jsGrid.validators.url = { - message: "{{ 'app.linkmanager.error.notvalid'|trans }}", - validator: function(value, item) { - return (/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}(:[0-9]{1,6})?(\/.*)?$/.test(value) && - !startsWith(value, 'lessn.io/') && - !startsWith(value, 'http://lessn.io/') && - !startsWith(value, 'http://www.lessn.io/') && - !startsWith(value, 'https://lessn.io/') && - !startsWith(value, 'https://www.lessn.io/') - ); - } - } - - jsGrid.validators.uuidchar = { - message: "{{ 'app.linkmanager.error.notvalid'|trans }} letters and '-', '_'", - validator: function(value, item) { - return /^[A-z0-9-_~]+$/.test(value); - } - } - - jsGrid.validators.uuidforbidden = { - message: "{{ 'app.linkmanager.error.forbidden'|trans }}", - validator: function(value, item) { - return !(jQuery.inArray(value, ['app', 'security', 'link', 'form']) !== -1) - } - } - - {# todo : Add len validator #} - {#jsGrid.validators.uuidmaxlen = { - message: "This value ", - validator: function(value, item) { - return !(jQuery.inArray(value, ['app', 'security', 'link', 'form']) !== -1) - } - }#} - - jsGrid.validators.uuidUnique = { - message: "{{ 'app.linkmanager.error.taken'|trans }}", - validator: function(value, item) { - var valid = false; - - $.ajax({ - type: 'GET', - url: '{{ path('app_link_manager_check_uuid') }}', - data: {uuid: value}, - dataType: 'json', - async: false - }).done(function (data) { - valid = data; - }); - return valid; - } - } - var customFinishInsert = function(grid) { jsGrid.loadStrategies.DirectLoadingStrategy.call(this, grid); }; @@ -148,6 +95,36 @@ function handleLinkJson(data) { } }, + onItemInserting: function (args) { + $.ajax({ + type: 'GET', + url: Routing.generate('api_validate_link', {uuid: args.item.uuid, url: args.item.url}), + dataType: 'json', + async: false + }).done(function (data) { + if (data.status === 'ko') { + args.cancel = true; + + var msgList = ''; + Object.keys(data.messages).forEach(key => { + msgList = msgList + '' + key.toUpperCase() + ' : ' + data.messages[key] + '
'; + }); + + $('#main_modal').modal('show'); + // language=HTML + $('.modal-content').html( + ` + + OK + `); + } + }); + }, + fields: [ { title: "{{ 'app.linkmanager.table.uuid'|trans }}", @@ -155,7 +132,6 @@ function handleLinkJson(data) { type: "text", width: 40, css: "jsgrid-inverted", - validate: ["uuidchar", 'uuidforbidden', 'uuidUnique'], editing: false, inserting: true }, @@ -164,7 +140,6 @@ function handleLinkJson(data) { name: "url", type: "text", css: "jsgrid-inverted", - validate: ["required", "url"] }, { title: "{{ 'app.linkmanager.createdon'|trans }}", diff --git a/templates/security/login.js.twig b/templates/security/login.js.twig index 2a39dea..d1f36df 100644 --- a/templates/security/login.js.twig +++ b/templates/security/login.js.twig @@ -2,7 +2,6 @@ form = $('#loginform'); form.submit(function (event) { event.preventDefault(); - console.log(form.serializeArray()); $.ajax({ url: form.attr('action'), // url where to submit the request @@ -13,8 +12,6 @@ form.submit(function (event) { success: function (data) { // you can see the result from the console // tab of the developer tools - console.log(data[0] === false); - console.log(data[1]); if (data[0] === false) { $('.loginerror').html(data[1])