Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about pre filtering list of admin class. #276

Closed
razeta opened this issue Sep 8, 2011 · 6 comments
Closed

Question about pre filtering list of admin class. #276

razeta opened this issue Sep 8, 2011 · 6 comments

Comments

@razeta
Copy link

razeta commented Sep 8, 2011

Hi,

I am trying to know if there is a way to use the same entity as a multiple admin clases, for example:

I have a entity class call user, with 4 roles and I am trying to make each role a admin class in the dashboard.

All I want to do is when I enter the list of one of the roles, show only the users with that role with out having to manually filter.

Any ideas if this can be done?

More info: I have created two admin classes refering to the same entity.

Admin classes User and Student.
Entity User

I have in the dashboard two categories, User and studen, but when I hit studen or user I get the same list.

I want to filter when I hit student option, the ones with that role in the entity. Without having to manually do it.

thanks.

@Entea
Copy link

Entea commented Jan 7, 2012

Hello, @razeta
Did you solve this issue?
I'm trying to do the same now, intersted how this can be done

@Entea
Copy link

Entea commented Jan 7, 2012

Filtering lists by owner, and in fact, by any field can be done like this:

I created an OwnerFilter class:

class OwnerFilter
{
    /**
     * @var SecurityContext
     */
    private $securityContext;

    private $ownerFieldName = 'owner';

    public function __construct(SecurityContext $securityContext, $ownerFieldName = null)
    {
        $this->securityContext = $securityContext;
        if ($ownerFieldName)
            $this->ownerFieldName = $ownerFieldName;
    }

    public function apply(ProxyQueryInterface $query)
    {
        /* @var \Symfony\Component\Security\Core\TokenInterface $token */
        $token = $this->securityContext->getToken();
        $user = $token->getUser();

        if ($user) {
            if (!($user instanceof \Maggots\LeadGeneratorBundle\Entity\User)) {
                throw new \InvalidArgumentException("User %s is not of type Maggots\\LeadGeneratorBundle\\Entity\\User", $user);
            }

            // Admins are allowed to view all
            if ($this->securityContext->isGranted('ROLE_ADMIN'))
                return;

            $r = rand(100000, 999999);
            $alias = $query->getRootAlias();

            $paramName = $this->ownerFieldName . $r;
            $query->andWhere($query->expr()->eq(sprintf('%s.%s', $alias, $this->ownerFieldName ), ':'.$paramName));
            $query->setParameter($paramName, $user->getId());
        }
    }
}

And injected it into my admin class, overriding the getQuery() method:

abstract class OwnerAwareAdmin extends \Sonata\AdminBundle\Admin\Admin
{
    /**
     * @var \Maggots\LeadGeneratorBundle\Admin\Filter\OwnerFilter
     */
    private $ownerFilter;

    public function setOwnerFilter(OwnerFilter $filter){
        $this->ownerFilter = $filter;
    }

    public function createQuery($context = 'list')
    {
        $query = parent::createQuery($context);
        $this->ownerFilter->apply($query);
        return $query;
    }
}

@Glideh
Copy link
Contributor

Glideh commented Jun 10, 2013

This is interesting, I'm trying to do the same kind of thing.
I'd like to inject the securityContext into my admin class (that extends SonataUser's one).
As I don't have any services configuration file in my extending bundle, I don't know where I could inject the securityContext argument.
I'm quite a beginner with Symfony and confused with all these classes, objects, entities, interfaces ...
(Coming from Python/Django world :)

@Glideh
Copy link
Contributor

Glideh commented Jul 8, 2013

After weeks of searching an no reply at all on my numerous questions, here is what I ended up with:

services:
    sonata.admin.user:
        class: Application\Sonata\UserBundle\Admin\Entity\UserAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: user, label: users, label_translator_strategy: sonata.admin.label.strategy.underscore }
        arguments:
            - null
            - Application\Sonata\UserBundle\Entity\User
            - SonataAdminBundle:CRUD
        calls:
            - [setTranslationDomain, [SonataUserBundle]]
            - [setUserManager, [@fos_user.user_manager]]
            - [setSecurityContext, [@security.context]] # <- key part here

The security context gets injected in the admin class with the custom call setSecurityContext() which needs to be implemented of course.

@ureimers
Copy link

@Glideh Thanks for sharing, Pierre!

@webdevilopers
Copy link
Contributor

Can we close this @razeta ?

ft-1408 pushed a commit to intexsys/SonataAdminBundle that referenced this issue Sep 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants