【问题标题】:Symfony - Show price based on Authentication RoleSymfony - 根据身份验证角色显示价格
【发布时间】:2012-10-18 19:43:41
【问题描述】:

我在 Symfony 中使用 Security Bundle 设置了不同的身份验证角色。

* Wholesale
* Detailing
* Public

基于用户登录的身份验证,我想显示产品的不同价格。

在我的产品实体中我有

$protected wholesalePrice;
$protected detailingPrice;
$protected publicPrice;

我可以使用一个视图来获取特定身份验证角色的价格,还是应该创建 3 个不同的视图?

【问题讨论】:

    标签: php model-view-controller symfony twig


    【解决方案1】:

    我建议创建一个服务和一个树枝扩展来通过您的模板访问它。

    这样你只需要做类似的事情:

    {{ product | priceByRole }}
    

    这将访问处理安全逻辑的“按角色定价”服务。

    服务:http://symfony.com/doc/current/book/service_container.html 编写 Twig 扩展:http://symfony.com/doc/2.0/cookbook/templating/twig_extension.html

    Twig 扩展示例:

    <?php
    
    namespace Acme\DemoBundle\Twig;
    
    use Symfony\Component\DependencyInjection\ContainerAwareInterface;
    use Symfony\Component\DependencyInjection\ContainerInterface;
    
    class PriceByRoleExtension extends \Twig_Extension implements ContainerAwareInterface
    {
        protected $container;
    
        public function setContainer(ContainerInterface $container = null)
        {
            $this->container = $container;
        }
    
        public function getFilters()
        {
            return array(
                'priceByRole' => new \Twig_Filter_Method($this, 'priceByRoleFilter'),
            );
        }
    
        public function priceByRoleFilter(Item $entity)
        {
            $service = $this->container->get('my.price.service');
    
            return $service->getPriceFromEntity($entity);
        }
    
        public function getName()
        {
            return 'acme_extension';
        }
    }
    

    示例服务:

    <?php
    
    namespace Acme\DemoBundle\Service;
    
    use Symfony\Component\Security\Core\SecurityContextInterface;
    use Acme\DemoBundle\Entity\Product;
    
    class PriceService
    {
        protected $context;
    
        public function setSecurityContext(SecurityContextInterface $context = null)
        {
            $this->context = $context;
        }
    
        public function getPriceFromEntity(Product $product)
        {
            if ($this->context->isGranted('ROLE_A'))
                return $product->getWholesalePrice();
    
            if ($this->context->isGranted('ROLE_B'))
                return $product->getDetailingPrice();
    
            if ($this->context->isGranted('ROLE_C'))
                return $product->getPublicPrice();
    
            throw new \Exception('No valid role for any price.');
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以像这样使用is_granted() 只使用一个视图:

      {% if is_granted('ROLE_A') %} 
          {{ product.wholesalePrice }}
      {% elseif is_granted('ROLE B') %}
          {{ product.detailingPrice }}
      {% elseif is_granted('ROLE C') %}
          {{ product.publicPrice }}
      {% endif %}
      

      希望对你有帮助。

      【讨论】:

      • @Alex Joyce 的回答比我的好。我的解决方案更直接,但如果您需要在多个模板中检查此角色,您应该选择 Service + Twig Extension 解决方案。
      猜你喜欢
      • 2016-09-23
      • 2021-07-13
      • 2019-03-10
      • 2014-11-14
      • 2015-05-31
      • 1970-01-01
      • 2021-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多