Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
/*.sublime*
/vendor
composer.lock
composer.phar

vendor

# IntelliJ project files
*.iml
*.iws
*.ipr
.idea/

# eclipse project file
.settings/
.classpath
.project

# NetBeans specific
nbproject/private/
build/
nbbuild/
dist/
nbdist/
nbactions.xml
nb-configuration.xml

# Sublime Text
*.sublime-workspace

# OS
.DS_Store
.Trashes
*~
7 changes: 7 additions & 0 deletions Exception/RetailCrmApiException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace RetailCrm\DeliveryModuleBundle\Exception;

class RetailCrmApiException extends \Exception
{
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020 RetailDriver LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 11 additions & 0 deletions Resources/translations/validators.en.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
integration_module_access:
server_unreachable_exception: 'Failed to connect to the integration server'
module_access_exception: 'Invalid login or password'

retailcrm_access:
access_denied: 'Access to the method "%method%" is denied'
curl_exception: 'Failed to connect to API'
service_unavailable: 'Service is temporarily unavailable'
invalid_json: 'API returned a response in an unsupported format'
requires_https: 'API address must start with https'
wrong_api_key: 'Invalid API key'
11 changes: 11 additions & 0 deletions Resources/translations/validators.es.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
integration_module_access:
server_unreachable_exception: 'No se ha podido conectar al servidor de integración'
module_access_exception: 'El Usuario o la contraseña no es correcta'

retailcrm_access:
access_denied: 'Acceso al método "%method%" está prohibido'
curl_exception: 'No se ha podido conectar al API'
service_unavailable: 'Servicio no está disponible temporalmente'
invalid_json: 'API ha devuelto la respuesta en el formato no soportado'
requires_https: 'Dirección del API tiene que comenzar con https'
wrong_api_key: 'El API-key no es correcto'
11 changes: 11 additions & 0 deletions Resources/translations/validators.ru.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
integration_module_access:
server_unreachable_exception: 'Не удалось подключиться к интеграционному серверу'
module_access_exception: 'Не верный логин или пароль'

retailcrm_access:
access_denied: 'Доступ к методу "%method%" запрещен'
curl_exception: 'Не удалось подключиться к API'
service_unavailable: 'Сервис временно недоступен'
invalid_json: 'API вернуло ответ в неподдерживаемом формате'
requires_https: 'Адрес API должен начинаться с https'
wrong_api_key: 'Неверный API-ключ'
2 changes: 2 additions & 0 deletions Service/ModuleManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function getAccount(): ?Account;

public function setAccount(Account $account): self;

public function checkAccess(): bool;

public function updateModuleConfiguration(): bool;

public function calculateDelivery(RequestCalculate $data): array;
Expand Down
23 changes: 23 additions & 0 deletions Validator/Constraints/IntegrationModuleAccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace RetailCrm\DeliveryModuleBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
* @Target({"CLASS"})
*/
class IntegrationModuleAccess extends Constraint
{
/** @var string */
public $path = 'login';

/**
* {@inheritdoc}
*/
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
}
44 changes: 44 additions & 0 deletions Validator/Constraints/IntegrationModuleAccessValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace RetailCrm\DeliveryModuleBundle\Validator\Constraints;

use RetailCrm\DeliveryModuleBundle\Exception\AbstractModuleException;
use RetailCrm\DeliveryModuleBundle\Exception\ServerUnreachableException;
use RetailCrm\DeliveryModuleBundle\Service\ModuleManagerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class IntegrationModuleAccessValidator extends ConstraintValidator
{
/** @var ModuleManagerInterface */
private $moduleManager;

public function __construct(ModuleManagerInterface $moduleManager)
{
$this->moduleManager = $moduleManager;
}

public function validate($account, Constraint $constraint)
{
if (!($constraint instanceof IntegrationModuleAccess)) {
throw new UnexpectedTypeException($constraint, IntegrationModuleAccess::class);
}

try {
$this->moduleManager->checkAccess();
} catch (ServerUnreachableException $e) {
$this->context
->buildViolation('integration_module_access.server_unreachable_exception')
->atPath($constraint->path)
->addViolation()
;
} catch (AbstractModuleException $e) {
$this->context
->buildViolation('integration_module_access.module_access_exception')
->atPath($constraint->path)
->addViolation()
;
}
}
}
23 changes: 23 additions & 0 deletions Validator/Constraints/RetailCrmAccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
* @Target({"CLASS"})
*/
class RetailCrmAccess extends Constraint
{
/** @var array */
public $requiredApiMethods = [];

/**
* {@inheritdoc}
*/
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
}
80 changes: 80 additions & 0 deletions Validator/Constraints/RetailCrmAccessValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace App\Validator\Constraints;

use RetailCrm\DeliveryModuleBundle\Exception\RetailCrmApiException;
use RetailCrm\DeliveryModuleBundle\Service\ModuleManagerInterface;
use RetailCrm\Exception\CurlException;
use RetailCrm\Exception\InvalidJsonException;
use RetailCrm\Exception\LimitException;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class RetailCrmAccessValidator extends ConstraintValidator
{
/** @var ModuleManagerInterface */
private $moduleManager;

public function __construct(ModuleManagerInterface $moduleManager)
{
$this->moduleManager = $moduleManager;
}

public function validate($account, Constraint $constraint)
{
if (!($constraint instanceof RetailCrmAccess)) {
throw new UnexpectedTypeException($constraint, RetailCrmAccess::class);
}

$client = $this->moduleManager->getRetailCrmClient();

try {
$response = $client->request->credentials();
if (!$response->isSuccessful()) {
throw new RetailCrmApiException($response->offsetGet('errorMsg'));
}

$credentials = $response->offsetGet('credentials');
foreach ($constraint->requiredApiMethods as $method) {
if (!in_array($method, $credentials)) {
$this->context
->buildViolation('retailcrm_access.access_denied', ['%method%' => $method])
->atPath('crmApiKey')
->addViolation()
;
}
}
} catch (CurlException $e) {
$this->context
->buildViolation('retailcrm_access.curl_exception')
->atPath('crmUrl')
->addViolation()
;
} catch (LimitException $e) {
$this->context
->buildViolation('retailcrm_access.service_unavailable')
->atPath('crmUrl')
->addViolation()
;
} catch (InvalidJsonException $e) {
$this->context
->buildViolation('retailcrm_access.invalid_json')
->atPath('crmUrl')
->addViolation()
;
} catch (\InvalidArgumentException $e) {
$this->context
->buildViolation('retailcrm_access.requires_https')
->atPath('crmUrl')
->addViolation()
;
} catch (RetailCrmApiException $e) {
$this->context
->buildViolation('retailcrm_access.wrong_api_key')
->atPath('crmUrl')
->addViolation()
;
}
}
}
30 changes: 15 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@
"RetailCrm\\DeliveryModuleBundle\\": ""
}
},
"config": {
"sort-packages": true
},
"require": {
"php": "^7.3",
"ramsey/uuid": "^3.9",
"ramsey/uuid-doctrine": "^1.5",
"symfony/framework-bundle": "^3.4|^4.0|^5.0",
"ext-json": "*",
"ext-zip": "*",
"doctrine/orm": "^2.7",
"jms/serializer": "^3.4",
"jms/serializer-bundle": "^3.5",
"doctrine/orm": "^2.7",
"symfony/lock": "^5.0",
"retailcrm/api-client-php": "dev-add-logger@dev",
"symfony/translation": "^5.0",
"symfony/routing": "^5.0"
"ramsey/uuid": "^3.9",
"ramsey/uuid-doctrine": "^1.5",
"retailcrm/api-client-php": "^5.0.0",
"symfony/framework-bundle": "^3.4|^4.0|^5.1",
"symfony/lock": "^5.1",
"symfony/routing": "^5.1",
"symfony/translation": "^5.1",
"symfony/validator": "^5.1"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/raulleo/api-client-php.git"
}
],
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.3"
}
}
}
Loading