9

I've installed SonataAdminBundle, SonataUserBundle and FOSUserBundle as well as CoopTilleulsAclSonataAdminExtensionBundle while using ACL in the SonataAdminBundle.

The listings are filtered by owners and all is fine. Customer A can see just his items, customer b also just his ones. But if i'm going to create a new object i can see items of other customers too.

Lets say a customer can create groups that will be used to assign products to. This is done while creating a product as a dropdown list (many-to-one relation as of products view). But i also can see groups that have been created by another customer.

How can i filter this? I think i have to do any filtering in the ProductsAdmin.php. Or does it have to happen in the ProductsRepository.php? I can't find any hints in the docs so far and would appriciate any kind of hint or link where i can find informations for this.

3

1 Answer 1

0

You need to either overwrite the edit twig template or create a custom select box. See this question about about overriding. I don`t know your entity structure but the process should go like

{% for object in objects %}
 {% if owned by this user flag display %}
  {{object}}
 {% endif %}
{% endfor%}

Custom select query in the admin class like

    $em = $this->getConfigurationPool()->getContainer()->get('doctrine.orm.entity_manager');
    $user = $this->getConfigurationPool()->getContainer()->get('security.token_storage')->getToken()->getUser();
    $query = $em->createQueryBuilder('p')
        ->select('p')
        ->from('MyBundle:Product', 'p')
        ->where('p.user = :user')
        ->setParameter('user', $user);

    $formMapper
            ...
            ->add('categoria', 'sonata_type_model', array(
    'required' => true,
    'query' => $query
))
9
  • I see your approoach. But i can't gain access to em in an adminclass, as it extends from AbstractAdmin, where no $this->get() is possible. Perhaps i can't see the forest for the trees.
    – Michael
    Oct 28, 2016 at 13:20
  • @Michael you can get em in the admin class using the configuration pool. I updated my answer.
    – Filchev
    Oct 28, 2016 at 13:23
  • I wasn't aware of the getConfigurationPool. Thanks for this. But still the question is, how to just get elements, where the current user is the owner of? In your example you reference to a column user in Product. But i'm using the ACL with CoopTilleulsAclSonataAdminExtensionBundle. So i can't reference on a column like user.
    – Michael
    Oct 28, 2016 at 13:50
  • @Michael in your entity you don't have product owner?
    – Filchev
    Oct 28, 2016 at 13:58
  • @Michael so I`m guessing you have a user role then? Get the role from your user object and filter by it.
    – Filchev
    Oct 28, 2016 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.