自定义扩展 - CurrentUserExtension,Symfony 错误
Custom Extension - CurrentUserExtension, Symfony error
我想在我的 API 中创建一个自定义扩展,创建感谢 api-platform. I tried to follow the documentation about extensions 做完全相同的事情:获取当前用户拥有的数据。但是我有以下错误:
(1/1) FatalThrowableError
Type error: Argument 1 passed to AppBundle\Doctrine\ORM\Extension\CurrentUserExtension::__construct() must implement interface Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface, none given, called in /var/www/api/var/cache/dev/appDevDebugProjectContainer.php on line 579
我试图删除缓存文件,但我遇到了同样的问题。
我的配置:
symfony/symfony v3.3.10
api-platform/schema-generator : v1.2.0
api_filters.yml :
services:
...
'AppBundle\Doctrine\ORM\Extension\CurrentUserExtension':
tags:
- { name: api_platform.doctrine.orm.query_extension.collection, priority: 9 }
- { name: api_platform.doctrine.orm.query_extension.item }
CurrentUserExtension.php :
<?php
namespace AppBundle\Doctrine\ORM\Extension;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use AppBundle\Entity\User;
use AppBundle\Entity\Voyage;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
final class CurrentUserExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface
{
private $tokenStorage;
private $authorizationChecker;
public function __construct(TokenStorageInterface $tokenStorage, AuthorizationChecker $checker)
{
$this->tokenStorage = $tokenStorage;
$this->authorizationChecker = $checker;
}
/**
* {@inheritdoc}
*/
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
{
$this->addWhere($queryBuilder, $resourceClass);
}
/**
* {@inheritdoc}
*/
public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = [])
{
$this->addWhere($queryBuilder, $resourceClass);
}
/**
*
* @param QueryBuilder $queryBuilder
* @param string $resourceClass
*/
private function addWhere(QueryBuilder $queryBuilder, string $resourceClass)
{
$user = $this->tokenStorage->getToken()->getUser();
if ($user instanceof User && Voyage::class === $resourceClass && !$this->authorizationChecker->isGranted('ROLE_USER')) {
$rootAlias = $queryBuilder->getRootAliases()[0];
$queryBuilder->andWhere(sprintf('%s.user = :current_user', $rootAlias));
$queryBuilder->setParameter('current_user', $user->getId());
}
}
}
config.yml :
[...]
doctrine:
dbal:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
server_version: '5.7'
charset: UTF8
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
fos_user:
db_driver: orm
firewall_name: main
user_class: AppBundle\Entity\User
from_email:
address: "%mailer_user%"
sender_name: Guilhem
api_platform:
title: API - Le Monde en Sac
description: The core API for the website Le Monde en Sac
enable_fos_user: true
http_cache:
invalidation:
enabled: false
varnish_urls: []
eager_loading:
max_joins: 1
你知道问题出在哪里吗?
问题已解决:
service.yml :
services:
'AppBundle\Doctrine\ORM\Extension\CurrentUserExtension':
arguments:
- '@security.token_storage'
- '@security.authorization_checker'
tags:
- { name: api_platform.doctrine.orm.query_extension.collection, priority: 9 }
- { name: api_platform.doctrine.orm.query_extension.item }
我想在我的 API 中创建一个自定义扩展,创建感谢 api-platform. I tried to follow the documentation about extensions 做完全相同的事情:获取当前用户拥有的数据。但是我有以下错误:
(1/1) FatalThrowableError Type error: Argument 1 passed to AppBundle\Doctrine\ORM\Extension\CurrentUserExtension::__construct() must implement interface Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface, none given, called in /var/www/api/var/cache/dev/appDevDebugProjectContainer.php on line 579
我试图删除缓存文件,但我遇到了同样的问题。
我的配置:
symfony/symfony v3.3.10
api-platform/schema-generator : v1.2.0
api_filters.yml :
services:
...
'AppBundle\Doctrine\ORM\Extension\CurrentUserExtension':
tags:
- { name: api_platform.doctrine.orm.query_extension.collection, priority: 9 }
- { name: api_platform.doctrine.orm.query_extension.item }
CurrentUserExtension.php :
<?php
namespace AppBundle\Doctrine\ORM\Extension;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use AppBundle\Entity\User;
use AppBundle\Entity\Voyage;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
final class CurrentUserExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface
{
private $tokenStorage;
private $authorizationChecker;
public function __construct(TokenStorageInterface $tokenStorage, AuthorizationChecker $checker)
{
$this->tokenStorage = $tokenStorage;
$this->authorizationChecker = $checker;
}
/**
* {@inheritdoc}
*/
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
{
$this->addWhere($queryBuilder, $resourceClass);
}
/**
* {@inheritdoc}
*/
public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = [])
{
$this->addWhere($queryBuilder, $resourceClass);
}
/**
*
* @param QueryBuilder $queryBuilder
* @param string $resourceClass
*/
private function addWhere(QueryBuilder $queryBuilder, string $resourceClass)
{
$user = $this->tokenStorage->getToken()->getUser();
if ($user instanceof User && Voyage::class === $resourceClass && !$this->authorizationChecker->isGranted('ROLE_USER')) {
$rootAlias = $queryBuilder->getRootAliases()[0];
$queryBuilder->andWhere(sprintf('%s.user = :current_user', $rootAlias));
$queryBuilder->setParameter('current_user', $user->getId());
}
}
}
config.yml :
[...]
doctrine:
dbal:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
server_version: '5.7'
charset: UTF8
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
fos_user:
db_driver: orm
firewall_name: main
user_class: AppBundle\Entity\User
from_email:
address: "%mailer_user%"
sender_name: Guilhem
api_platform:
title: API - Le Monde en Sac
description: The core API for the website Le Monde en Sac
enable_fos_user: true
http_cache:
invalidation:
enabled: false
varnish_urls: []
eager_loading:
max_joins: 1
你知道问题出在哪里吗?
问题已解决:
service.yml :
services:
'AppBundle\Doctrine\ORM\Extension\CurrentUserExtension':
arguments:
- '@security.token_storage'
- '@security.authorization_checker'
tags:
- { name: api_platform.doctrine.orm.query_extension.collection, priority: 9 }
- { name: api_platform.doctrine.orm.query_extension.item }