Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8701807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:29:28+00:00 2026-06-13T02:29:28+00:00

I have different Authentication Roles set up in Symfony using the Security Bundle. *

  • 0

I have different Authentication Roles set up in Symfony using the Security Bundle.

* Wholesale
* Detailing
* Public

Based on the authentication that the user is logged in with I want to display different prices for products.

In my Product entity I have

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

Can I use one view to get the price for that specific Authentication Role or should I create 3 different views?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:29:30+00:00Added an answer on June 13, 2026 at 2:29 am

    I’d suggest creating a service and a twig extension to access it via your templates.

    That way you’d only have to do something like:

    {{ product | priceByRole }}
    

    This would access your “price by role” service that handles the security logic.

    Services: http://symfony.com/doc/current/book/service_container.html
    Writing a Twig Extension: http://symfony.com/doc/2.0/cookbook/templating/twig_extension.html

    Example Twig Extension:

    <?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';
        }
    }
    

    Example Service:

    <?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.');
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I’m writing an CI application that has to have authentication and preferably different roles
I have an ASP.Net application that is using Windows Authentication and I have specified
I am using BlazeDS and Spring Security and have set up my application using
I have different php output in jQuery-based tabs. This output is formed from database
I have different urls that points to the same code www.url1.com www.url2.com I need
We are developing a web app that will have a pretty complex user and
I'm using the ASP.NET/C# Login control and that entire authentication and authorization system. I
I'm using windows authentication in my ASP.net MVC 3 application. I have a few
I'm using spring-security web authentication with spring-mvc, all is well so far, but now
I'm using MVC3 with .net membership. I want to redirect the user to different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.