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

  • SEARCH
  • Home
  • 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 7062119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:33:21+00:00 2026-05-28T04:33:21+00:00

We are using Symfony2’s roles feature to restrict users’ access to certain parts of

  • 0

We are using Symfony2’s roles feature to restrict users’ access to certain parts of our app. Users can purchase yearly subscriptions and each of our User entities has many Subscription entities that have a start date and an end.

Now, is there a way to dynamically add a role to a user based on whether they have an ‘active’ subscription? In rails i would simply let the model handle whether it has the necessary rights but I know that by design symfony2 entities are not supposed to have access to Doctrine.

I know that you can access an entity’s associations from within an entity instance but that would go through all the user’s subscription objects and that seems unnecessaryly cumbersome to me.

  • 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-05-28T04:33:22+00:00Added an answer on May 28, 2026 at 4:33 am

    I think you would do better setting up a custom voter and attribute.

    /**
     * @Route("/whatever/")
     * @Template
     * @Secure("SUBSCRIPTION_X")
     */
    public function viewAction()
    {
        // etc...
    }
    

    The SUBSCRIPTION_X role (aka attribute) would need to be handled by a custom voter class.

    class SubscriptionVoter implements VoterInterface
    {
        private $em;
    
        public function __construct($em)
        {
            $this->em = $em;
        }
    
        public function supportsAttribute($attribute)
        {
            return 0 === strpos($attribute, 'SUBSCRIPTION_');
        }
    
        public function supportsClass($class)
        {
            return true;
        }
    
        public function vote(TokenInterface $token, $object, array $attributes)
        {
            // run your query and return either...
            //  * VoterInterface::ACCESS_GRANTED
            //  * VoterInterface::ACCESS_ABSTAIN
            //  * VoterInterface::ACCESS_DENIED
        }
    }
    

    You would need to configure and tag your voter:

    services:
        subscription_voter:
            class: SubscriptionVoter
            public: false
            arguments: [ @doctrine.orm.entity_manager ]
            tags:
                - { name: security.voter }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Doctrine as Auth provider in my symfony2 app. How can I
I'm developing a little application using Symfony2. I can access all routes with no
Using Symfony2 and Doctrine ODM with MongoDB, I need to access the parent document
Using Symfony2 and orm.yml files to map the entities into the database how can
I'm using Symfony2 to authenticate users. And I have two databases to store user
I can't seem to find any documentation on Symfony2 forms when using Propel ORM.
I am re-writing an old project using Symfony2 so I can try out the
I'm using Symfony2 Framework, and I want to update an entity with data from
I'm using Symfony2 and CraueFormFlowBundle to create a multi-step form. Everything is going well
We are using Symfony2 to create an API. When updating a record, we expect

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.