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
Comments
Hello, @razeta |
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;
}
} |
This is interesting, I'm trying to do the same kind of thing. |
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 |
@Glideh Thanks for sharing, Pierre! |
Can we close this @razeta ? |
Added missing translations
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.
The text was updated successfully, but these errors were encountered: