Skip to content

Instantly share code, notes, and snippets.

@simplyniceweb
Created December 27, 2022 01:59
Show Gist options
  • Save simplyniceweb/53d8f4ad7dd5c97823298e8e92e41694 to your computer and use it in GitHub Desktop.
Save simplyniceweb/53d8f4ad7dd5c97823298e8e92e41694 to your computer and use it in GitHub Desktop.
<?php
namespace App\Controller\Admin;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\QueryBuilder;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
use EasyCorp\Bundle\EasyAdminBundle\Orm\EntityRepository;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\SlugField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
class UserCrudController extends AbstractCrudController
{
var $passwordHasher;
private $tokenStorage;
public function __construct(UserPasswordHasherInterface $passwordHasher, TokenStorageInterface $tokenStorage)
{
$this->passwordHasher = $passwordHasher;
$this->tokenStorage = $tokenStorage->getToken()->getUser();
}
public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
{
$user = $this->tokenStorage;
$qb = $this->container->get(EntityRepository::class)->createQueryBuilder($searchDto, $entityDto, $fields, $filters);
if ($this->isGranted('ROLE_CONTRACTOR') && !$this->isGranted('ROLE_ADMIN')) {
$qb->andWhere('entity.id = :user')
->setParameter('user', $user->getId());
}
return $qb;
}
public static function getEntityFqcn(): string
{
return User::class;
}
public function configureActions(Actions $actions): Actions
{
return $actions
->setPermission(Action::NEW, 'ROLE_ADMIN')
->setPermission(Action::DELETE, 'ROLE_ADMIN')
;
}
public function configureFields(string $pageName): iterable
{
$fields = [
IdField::new('id')
->hideOnForm(),
ImageField::new('photo')
->setRequired(false)
->setBasePath('uploads/')
->setUploadDir('public/uploads')
->hideOnIndex(),
TextField::new('contractor_name')
->hideOnIndex(),
SlugField::new('slug')
->setTargetFieldName('contractor_name')
->hideOnIndex(),
TextField::new('email'),
TextField::new('plainPassword', 'Password')
->setRequired(false)
->setFormType(PasswordType::class)
->hideOnIndex(),
TextField::new('social_media')
->hideOnIndex(),
TextField::new('contact_nr')
->hideOnIndex(),
TextareaField::new('address')
];
if ($this->isGranted('ROLE_ADMIN')) {
$fields[] = ChoiceField::new('roles')
->autocomplete()
->setChoices([
'Admin' => 'ROLE_ADMIN',
'Contractor' => 'ROLE_CONTRACTOR',
'User' => 'ROLE_USER',
])
->allowMultipleChoices();
}
return $fields;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment