duanhuang1699 2016-04-21 12:05
浏览 28
已采纳

Symfony Twig Extension打破其他服务 - 在安全之前进行模板化吗?

I am working on a Symfony 2.7 WebApp. One of the bundles I created includes a service that offer some user related stuff, e.g. userHasPurchases().

Problem is, that including a Twig Extesion breaks another service:

AppShopService

namespace AppShopBundle\Service;

use AppBundle\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
...

class AppShopService {
    protected $user;

    public function __construct(TokenStorageInterface $tokenStorage, ...) {
        $this->user = $tokenStorage->getToken() ? $tokenStorage->getToken()->getUser() : null;
        ...
    }

    public function userHasPurchases(User $user) {
        $user = $user ? $user : $this->user;
        $result = $user...
        return result;
    }
}

AppShopBundle\Resources\config\services.yml

services:
    app_shop.service:
        class: AppShopBundle\Service\AppShopService
        arguments: 
            - "@security.token_storage"
            - ...

So far everything works fine: The AppShopServices is created with the current user and userHasPurchases() work as expected.

Now I have add a Twig Extension to be able to use userHasPurchases() within my templates:

Twig Extension

namespace AppShopBundle\Twig;

use AppShopBundle\Service\AppShopService;

class AppShopExtension extends \Twig_Extension {
    private $shopService;

    public function __construct(AppShopService $shopService) {
        $this->shopService = $shopService;
    }

    public function getName() {
        return 'app_shop_bundle_extension';
    }

    public function getFunctions() {
        $functions = array();

        $functions[] = new \Twig_SimpleFunction('userHasPurchases', array(
                $this,
                'userHasPurchases'
            ));

        return $functions;
    }

    public function userHasPurchases($user) {
        return $this->shopService->userHasPurchases($user);
    }
}

Including Extension in AppShopBundle\Resources\config\services.yml

services:
    app_shop.service:
        class: AppShopBundle\Service\AppShopService
        arguments: 
            - "@security.token_storage"
            - ...

    app_shop.twig_extension:
        class: AppShopBundle\Twig\AppShopExtension
        arguments: 
          - "@app_shop.service"
        tags:
          - { name: twig.extension }

After icluding the Twig Extension, AppShopService and its method userHasPurchases does not work any more. Problem is, that the constructor of AppShopService does not set user anymore since $tokenStorage->getToken() now returns null.

How is this possible? I have changed nothing except including the Twig Extension. As soon as I remove the Twig Extension from services.yml everything works correctly again.

My only guess is, that the creation fo the Twig Extension is done before any security. But why?

Any idea what might be wrong here?

  • 写回答

1条回答 默认 最新

  • dongru3726 2016-04-21 13:00
    关注

    don't interact with the tokenStorage in the constructor but only in the userHasPurchases method.

    namespace AppShopBundle\Service;
    
    use AppBundle\Entity\User;
    use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
    ...
    
    class AppShopService {
        protected $tokenStorage;
    
        public function __construct(TokenStorageInterface $tokenStorage, ...) {
            $this->tokenStorage = $tokenStorage;
        }
    
        public function userHasPurchases(User $user) {
            $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
            $result = $user...
            return result;
        }
    }
    

    Hope this help

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度