Search code examples
phpsymfonyserviceoauth-2.0hwioauthbundle

override ResourceOwner class for Wordpress


I went to override WordpressResourceOwner into HWIOAuthBundle as it didn`t return user information response

I notice that every ResourceOwner work as Service and declared into oauth.xml


Solution

  • i had override registration twig into HWIOAuthBundle and create new bundle inherit from HWIOAuthBundle like answer of this question but I can`t work the same with WordpressResourceOwner

    after searching I had found we can override class of WordpressResourceOwner Service

    Steps:

    1) create new ResourceOwner

    EX:

    namespace project\OAuthBundle\OAuth\ResourceOwner;
    
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
    use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\GenericOAuth2ResourceOwner;
    use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
    
    /**
     * Override WordpressResourceOwner class into HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\WordpressResourceOwner 
     *
     * @author ahmedhamdy
     * 
     */
    class WordpressResourceOwner extends GenericOAuth2ResourceOwner
    {
        /**
         * {@inheritDoc}
         */
        protected $paths = array(
            'identifier'     => 'ID',
            'nickname'       => 'username',
            'realname'       => 'display_name',
            'email'          => 'email',
            'profilepicture' => 'avatar_URL',
        );
    
      /**
         * {@inheritDoc}
         */
        protected function doGetUserInformationRequest($url, array $parameters = array())
        {
            $apiAccessToken = $parameters['apiAccessToken'];
            $headers = array(
                0 => 'authorization: Bearer '.$apiAccessToken,
            );
            return $this->httpRequest($url,null,$headers);
        }
    
        /**
         * {@inheritDoc}
         */
        public function getUserInformation(array $accessToken, array $extraParameters = array())
        {
            $url = $this->normalizeUrl($this->options['infos_url'], array(
                'access_token' => $accessToken['access_token']
            ));
    
            $parameters = array(
                'apiAccessToken' => $accessToken['access_token'],
            );
    
            $content = $this->doGetUserInformationRequest($url,$parameters)->getContent();
            $response = $this->getUserResponse();
            $response->setResponse($content);
            $response->setResourceOwner($this);
            $response->setOAuthToken(new OAuthToken($accessToken));
    
            return $response;
        }
    
    
        /**
         * {@inheritDoc}
         */
        protected function configureOptions(OptionsResolverInterface $resolver)
        {
            parent::configureOptions($resolver);
    
            $resolver->setDefaults(array(
                'authorization_url' => 'https://public-api.wordpress.com/oauth2/authorize',
                'access_token_url'  => 'https://public-api.wordpress.com/oauth2/token',
                'infos_url'         => 'https://public-api.wordpress.com/rest/v1/me',
            ));
        }
    }
    

    2) create CompilerPass class to override service class like Symfony2 documentation

    EX:

    namespace project\OAuthBundle\DependencyInjection\Compiler;
    
    use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    
    /**
     * OverrideWordpressResourceOwner
     *
     * @author ahmedhamdy
     */
    
    class OverrideWordpressResourceOwner  implements CompilerPassInterface
    {
        public function process(ContainerBuilder $container)
        {
            $definition = $container->getDefinition("hwi_oauth.abstract_resource_owner.wordpress");
            $definition->setClass('ProfileTree\OAuthBundle\OAuth\ResourceOwner\WordpressResourceOwner');
        }
    }
    

    3) we must override build method into Bundle class like Symfony2 documentation

    EX:

    namespace Project\OAuthBundle;
    
    use Symfony\Component\HttpKernel\Bundle\Bundle;
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    use ProfileTree\OAuthBundle\DependencyInjection\Compiler\OverrideWordpressResourceOwner;
    
    class ProfileTreeOAuthBundle extends Bundle
    {
    
        public function getParent()
        {
            return 'HWIOAuthBundle';
        }
    
        public function build(ContainerBuilder $container)
        {
            parent::build($container);
    
            $container->addCompilerPass(new OverrideWordpressResourceOwner());
        }
    }
    

    4) last step calling compile method for ContainerBuilder into Bundle Extension like Symfony2 documentation

    EX:

    namespace Project\OAuthBundle\DependencyInjection;
    
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    use Symfony\Component\Config\FileLocator;
    use Symfony\Component\HttpKernel\DependencyInjection\Extension;
    use Symfony\Component\DependencyInjection\Loader;
    
    /**
     * This is the class that loads and manages your bundle configuration
     *
     * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
     */
    class ProfileTreeOAuthExtension extends Extension
    {
        /**
         * {@inheritDoc}
         */
        public function load(array $configs, ContainerBuilder $container)
        {
            $configuration = new Configuration();
            $config = $this->processConfiguration($configuration, $configs);
    
            $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
            $loader->load('services.yml');
    
            $container->compile();
    
        }
    }