roensby / symfony-drupal-jsonapi
A Symfony JSONAPI client with built-in support for Drupal entities.
1.8.0
2020-11-02 13:39 UTC
Requires
- php: ^7.1.3
- ext-json: *
- doctrine/annotations: ^1.7
- doctrine/cache: ^1.8
- phpdocumentor/reflection-docblock: ^4.3
- ramsey/uuid: ^3.8
- symfony/config: ^5.0
- symfony/dependency-injection: ^5.0
- symfony/http-client: ^5.0
- symfony/property-info: ^5.0
- symfony/security-bundle: ^5.0
- symfony/serializer: ^5.0
- symfony/validator: ^5.0
- symfony/yaml: ^5.0
Requires (Dev)
- phpstan/extension-installer: ^1.0
- phpstan/phpstan-symfony: ^0.12.0
- symfony/form: ^5.0
- symfony/framework-bundle: ^5.0
- symfony/phpunit-bridge: ^5.0
- symfony/security-bundle: ^5.0
- symfony/var-dumper: ^5.0
README
A Symfony JSONAPI client with built-in support for Drupal entities using OAuth2 for authentication.
Installation instructions
Run
composer require roensby/symfony-drupal-jsonapi
Configure the library:
symfony_drupal_jsonapi: jsonapi: # The Drupal jsonapi endpoint. endpoint: 'http://example/jsonapi' # The Drupal CSRF endpoint. csrf_endpoint: 'http://example/session/token' oauth2: # The Drupal simple_oauth module endpoint. endpoint: 'http://example/oauth/token' # A uuid string given by a Drupal consumer entity. client_id: 'CLIENT ID' # A secret string shared between your Symfony application and Drupal. client_secret: 'CLIENT SECRET' # Use the interactive login if users log in. interactive_login: # Where to redirect when authentication fails. login_route: app_login # Where to redirect when authentication succeeds. login_redirect: frontpage # Use preset user for non-interactive applications. preset_user: # A Drupal username. username: 'USERNAME' # The corresponding password. password: 'SOME PASSWORD'
Use a combination of either the
LoginAuthenticator
orPresetAuthenticator
class with theOAuthRefreshAuthenticator
class.
Usage instructions
Load taxonomy term of bundle 'tag' by tid
<?php
namespace App\Controller;
use Roensby\SymfonyDrupalJsonApi\Entity\Core\Tag;
use Roensby\SymfonyDrupalJsonApi\Entity\Core\TagInterface;
use Roensby\SymfonyDrupalJsonApi\Factory\EntityFactory;
use Roensby\SymfonyDrupalJsonApi\JsonApi\Filter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
class MyController extends AbstractController {
/**
* Renders a tag.
*/
public function getTag(string $tid): Response
{
/** @var EntityFactory $entityFactory */
$entityFactory = $this->get(EntityFactory::class);
/** @var TagInterface $tag */
$tag = $entityFactory->query(Tag::class)
->addFilter(new Filter('drupal_internal__tid', '=', $tid))
->load();
return $this->render('my_template', [
'description' => $tag->getDescription(),
'name' => $tag->getName(),
]);
}
}
Security
Setting up the GuardAuthenticator class.
For more information, see Custom Authentication System with Guard .
security:
encoders:
Roensby\SymfonyDrupalJsonApi\Security\User\OAuthUser:
algorithm: auto
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
app_user_provider:
id: Roensby\SymfonyDrupalJsonApi\Security\User\UserProvider
firewalls:
main:
guard:
authenticators:
- Roensby\SymfonyDrupalJsonApi\Security\GuardAuthenticator\LoginAuthenticator
- Roensby\SymfonyDrupalJsonApi\Security\GuardAuthenticator\OAuthRefreshAuthenticator
entry_point: Roensby\SymfonyDrupalJsonApi\Security\LoginAuthenticator
Uses OAuth to authenticate requests.
For more information, see
the Simple OAuth module.