Skip to content

Commit

Permalink
Merge pull request #8057 from mtshaw3/7985-refactor-api-bundle
Browse files Browse the repository at this point in the history
M3: Refactor Deprecations - ApiBundle #7985
  • Loading branch information
dongilbert committed Nov 27, 2019
2 parents fc6c370 + 9f961a3 commit b410104
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 95 deletions.
11 changes: 7 additions & 4 deletions app/bundles/ApiBundle/Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,16 @@
'forms' => [
'mautic.form.type.apiclients' => [
'class' => 'Mautic\ApiBundle\Form\Type\ClientType',
'arguments' => 'mautic.factory',
'alias' => 'client',
'arguments' => [
'request_stack',
'translator',
'validator',
'router',
'session',
],
],
'mautic.form.type.apiconfig' => [
'class' => 'Mautic\ApiBundle\Form\Type\ConfigType',
'alias' => 'apiconfig',
],
],
'other' => [
Expand Down Expand Up @@ -170,7 +174,6 @@
'mautic.validator.oauthcallback' => [
'class' => 'Mautic\ApiBundle\Form\Validator\Constraints\OAuthCallbackValidator',
'tag' => 'validator.constraint_validator',
'alias' => 'oauth_callback',
],
],
'models' => [
Expand Down
7 changes: 0 additions & 7 deletions app/bundles/ApiBundle/Controller/CommonApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1244,13 +1244,6 @@ protected function returnError($msg, $code = Codes::HTTP_INTERNAL_SERVER_ERROR,
'errors' => [
$error,
],
// @deprecated 2.6.0 to be removed in 3.0
'error' => [
'message' => $this->get('translator')->trans($msg, [], 'flashes')
.' (`error` is deprecated as of 2.6.0 and will be removed in 3.0. Use the `errors` array instead.)',
'code' => $code,
'details' => $details,
],
],
$code
);
Expand Down
16 changes: 1 addition & 15 deletions app/bundles/ApiBundle/DependencyInjection/Compiler/OAuthPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,7 @@ public function process(ContainerBuilder $container)
if ($container->hasDefinition('bazinga.oauth.security.authentication.provider')) {
//Add a addMethodCall to set factory
$container->getDefinition('bazinga.oauth.security.authentication.provider')->addMethodCall(
'setFactory', [new Reference('mautic.factory')]
);
}

if ($container->hasDefinition('bazinga.oauth.security.authentication.listener')) {
//Add a addMethodCall to set factory
$container->getDefinition('bazinga.oauth.security.authentication.listener')->addMethodCall(
'setFactory', [new Reference('mautic.factory')]
);
}

if ($container->hasDefinition('fos_oauth_server.security.authentication.listener')) {
//Add a addMethodCall to set factory
$container->getDefinition('fos_oauth_server.security.authentication.listener')->addMethodCall(
'setFactory', [new Reference('mautic.factory')]
'setTranslator', [new Reference('translator')]
);
}
}
Expand Down
3 changes: 0 additions & 3 deletions app/bundles/ApiBundle/EventListener/ApiSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ public function onKernelResponse(FilterResponseEvent $event)
'type' => $type,
],
],
// @deprecated 2.6.0 to be removed in 3.0
'error' => $data['error'],
'error_description' => $message.' (`error` and `error_description` are deprecated as of 2.6.0 and will be removed in 3.0. Use the `errors` array instead.)',
],
$statusCode
);
Expand Down
71 changes: 46 additions & 25 deletions app/bundles/ApiBundle/Form/Type/ClientType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@
namespace Mautic\ApiBundle\Form\Type;

use Mautic\ApiBundle\Form\Validator\Constraints\OAuthCallback;
use Mautic\CoreBundle\Factory\MauticFactory;
use Mautic\CoreBundle\Form\DataTransformer as Transformers;
use Mautic\CoreBundle\Form\EventListener\CleanFormSubscriber;
use Mautic\CoreBundle\Form\EventListener\FormExitSubscriber;
use Mautic\CoreBundle\Form\Type\FormButtonsType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\ValidatorInterface;

/**
* Class ClientType.
Expand All @@ -45,19 +51,33 @@ class ClientType extends AbstractType
private $apiMode;

/**
* @var \Symfony\Bundle\FrameworkBundle\Routing\Router
* @var \Symfony\Component\Routing\RouterInterface
*/
private $router;

/**
* @param MauticFactory $factory
* Constructor.
*
* @param RequestStack $requestStack
* @param TranslatorInterface $translator
* @param ValidatorInterface $validator
* @param Session $session
* @param RouterInterface $router
*/
public function __construct(MauticFactory $factory)
{
$this->translator = $factory->getTranslator();
$this->validator = $factory->getValidator();
$this->apiMode = $factory->getRequest()->get('api_mode', $factory->getSession()->get('mautic.client.filter.api_mode', 'oauth1a'));
$this->router = $factory->getRouter();
public function __construct(
RequestStack $requestStack,
TranslatorInterface $translator,
ValidatorInterface $validator,
Session $session,
RouterInterface $router
) {
$this->translator = $translator;
$this->validator = $validator;
$this->apiMode = $requestStack->getCurrentRequest()->get(
'api_mode',
$session->get('mautic.client.filter.api_mode', 'oauth1a')
);
$this->router = $router;
}

/**
Expand All @@ -71,7 +91,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
if (!$options['data']->getId()) {
$builder->add(
'api_mode',
'choice',
ChoiceType::class,
[
'mapped' => false,
'label' => 'mautic.api.client.form.auth_protocol',
Expand All @@ -81,19 +101,20 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'onchange' => 'Mautic.refreshApiClientForm(\''.$this->router->generate('mautic_client_action', ['objectAction' => 'new']).'\', this)',
],
'choices' => [
'oauth1a' => 'OAuth 1.0a',
'oauth2' => 'OAuth 2',
'OAuth 1.0a' => 'oauth1a',
'OAuth 2' => 'oauth2',
],
'required' => false,
'empty_value' => false,
'data' => $this->apiMode,
'choices_as_values' => true,
'required' => false,
'empty_value' => false,
'data' => $this->apiMode,
]
);
}

$builder->add(
'name',
'text',
TextType::class,
[
'label' => 'mautic.core.name',
'label_attr' => ['class' => 'control-label'],
Expand All @@ -106,7 +127,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder->add(
$builder->create(
'redirectUris',
'text',
TextType::class,
[
'label' => 'mautic.api.client.redirecturis',
'label_attr' => ['class' => 'control-label'],
Expand All @@ -121,7 +142,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)

$builder->add(
'publicId',
'text',
TextType::class,
[
'label' => 'mautic.api.client.form.clientid',
'label_attr' => ['class' => 'control-label'],
Expand All @@ -135,7 +156,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)

$builder->add(
'secret',
'text',
TextType::class,
[
'label' => 'mautic.api.client.form.clientsecret',
'label_attr' => ['class' => 'control-label'],
Expand Down Expand Up @@ -181,7 +202,7 @@ function (FormEvent $event) use ($translator, $validator) {
$builder->add(
$builder->create(
'callback',
'text',
TextType::class,
[
'label' => 'mautic.api.client.form.callback',
'label_attr' => ['class' => 'control-label'],
Expand All @@ -196,7 +217,7 @@ function (FormEvent $event) use ($translator, $validator) {

$builder->add(
'consumerKey',
'text',
TextType::class,
[
'label' => 'mautic.api.client.form.consumerkey',
'label_attr' => ['class' => 'control-label'],
Expand All @@ -213,7 +234,7 @@ function (FormEvent $event) use ($translator, $validator) {

$builder->add(
'consumerSecret',
'text',
TextType::class,
[
'label' => 'mautic.api.client.form.consumersecret',
'label_attr' => ['class' => 'control-label'],
Expand Down Expand Up @@ -265,7 +286,7 @@ function (FormEvent $event) use ($translator, $validator) {
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
public function configureOptions(OptionsResolver $resolver)
{
$dataClass = ($this->apiMode == 'oauth2') ? 'Mautic\ApiBundle\Entity\oAuth2\Client' : 'Mautic\ApiBundle\Entity\oAuth1\Consumer';
$resolver->setDefaults(
Expand All @@ -278,7 +299,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
/**
* {@inheritdoc}
*/
public function getName()
public function getBlockPrefix()
{
return 'client';
}
Expand Down
9 changes: 5 additions & 4 deletions app/bundles/ApiBundle/Form/Type/ConfigType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Mautic\CoreBundle\Form\Type\YesNoButtonGroupType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\NotBlank;

Expand Down Expand Up @@ -53,7 +54,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)

$builder->add(
'api_oauth2_access_token_lifetime',
'number',
NumberType::class,
[
'label' => 'mautic.api.config.form.api.oauth2_access_token_lifetime',
'attr' => [
Expand All @@ -73,7 +74,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)

$builder->add(
'api_oauth2_refresh_token_lifetime',
'number',
NumberType::class,
[
'label' => 'mautic.api.config.form.api.oauth2_refresh_token_lifetime',
'attr' => [
Expand All @@ -93,7 +94,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)

$builder->add(
'api_rate_limiter_limit',
'number',
NumberType::class,
[
'label' => 'mautic.api.config.form.api.rate_limiter_limit',
'attr' => [
Expand All @@ -115,7 +116,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
/**
* {@inheritdoc}
*/
public function getName()
public function getBlockPrefix()
{
return 'apiconfig';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@
namespace Mautic\ApiBundle\Security\OAuth1\Authentication\Provider;

use Bazinga\OAuthServerBundle\Security\Authentification\Token\OAuthToken;
use Mautic\CoreBundle\Factory\MauticFactory;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Translation\TranslatorInterface;

/**
* Class OAuthProvider.
*/
class OAuthProvider extends \Bazinga\OAuthServerBundle\Security\Authentification\Provider\OAuthProvider
{
/**
* @var MauticFactory
* @var \Symfony\Bundle\FrameworkBundle\Translation\Translator
*/
private $factory;
private $translator;

/**
* @param MauticFactory $factory
* @param TranslatorInterface $translator
*/
public function setFactory(MauticFactory $factory)
public function setTranslator(TranslatorInterface $translator)
{
$this->factory = $factory;
$this->translator = $translator;
}

/**
Expand All @@ -43,8 +43,6 @@ public function authenticate(TokenInterface $token)
return null;
}

$translator = $this->factory->getTranslator();

$requestParameters = $token->getRequestParameters();
$requestMethod = $token->getRequestMethod();
$requestUrl = $token->getRequestUrl();
Expand All @@ -66,7 +64,7 @@ public function authenticate(TokenInterface $token)
return $token;
}

throw new AuthenticationException($translator->trans('mautic.api.oauth.auth.failed'));
throw new AuthenticationException($this->translator->trans('mautic.api.oauth.auth.failed'));
}

/**
Expand Down
14 changes: 0 additions & 14 deletions app/bundles/ApiBundle/Security/OAuth1/Firewall/OAuthListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Mautic\ApiBundle\Security\OAuth1\Firewall;

use Bazinga\OAuthServerBundle\Security\Authentification\Token\OAuthToken;
use Mautic\CoreBundle\Factory\MauticFactory;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
Expand All @@ -24,19 +23,6 @@
*/
class OAuthListener extends \Bazinga\OAuthServerBundle\Security\Firewall\OAuthListener
{
/**
* @var MauticFactory
*/
private $factory;

/**
* @param MauticFactory $factory
*/
public function setFactory(MauticFactory $factory)
{
$this->factory = $factory;
}

/**
* @author William DURAND <william.durand1@gmail.com>
*
Expand Down

0 comments on commit b410104

Please sign in to comment.