0

I am having trouble configuring the permissions of a user with the 'ROLE_USER' role so that they can only edit, view, or delete their own articles with EasyAdmin 3. I have tried using the 'setEntityPermission' method, but it returns all articles, even those not created by the user. I have also tried to use the 'setEntityPermissions' method, but I receive the error 'Undefined method'.

Here is my code:

<?php

namespace App\Controller\Admin;

use App\Entity\Users;
use App\Entity\Article;
use \Symfony\Bundle\SecurityBundle\Security;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use EasyCorp\Bundle\EasyAdminBundle\Field\SlugField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use EasyCorp\Bundle\EasyAdminBundle\Filter\EntityFilter;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use Doctrine\ORM\EntityManagerInterface;



class ArticleCrudController extends AbstractCrudController
{
    private $security;
    private $entityManager;

    public function __construct(Security $security, EntityManagerInterface $entityManager)
    {
        $this->security = $security;
        $this->entityManager = $entityManager;
    }

    public static function getEntityFqcn(): string
    {
        return Article::class;
    }
    

    public function configureFields(string $pageName): iterable
    {
        // Define the fields to be displayed in the form for creating/editing an article
        yield TextField::new('title');
        yield SlugField::new('slug')
            ->setTargetFieldName('title');
        yield TextEditorField::new('content');
        yield TextareaField::new('featuredText', 'Texte mis en avant');
        yield DateTimeField::new('createdAt')->hideOnForm();
        yield DateTimeField::new('updatedAt')->hideOnForm();
        // yield TextEditorField::new('author')->hideOnForm();
    }

    public function configureFilters(Filters $filters): Filters
    {
        // Define the filters for the article list page
        $filters->add(EntityFilter::new('author'));
        return $filters;
    }

    private function getArticlesByUser(Users $user): array
    {
        $user = $this->security->getUser();
        if (!$user) {
            return [];
        }
        $repository = $this->entityManager->getRepository(Article::class);

        return $repository->findBy(['author' => $user]);
    }


    public function configureCrud(Crud $crud): Crud
    {
        $user = $this->security->getUser();
    
        // If the user is an admin, display all the articles
        if ($this->isGranted('ROLE_ADMIN')) {
            return $crud;
        }
        
        // If the user is not an admin, display only the articles authored by the user
        $articles = $this->getArticlesByUser($user);
        // dd($articles); // With this dd(), I successfully retrieve the articles linked to the logged-in user.
        
        return $crud
        ->setEntityPermission('ROLE_USER', 'EDIT', function (Article $article) use ($user, $articles) {
            return $this->isGranted('ROLE_USER') && $article->getAuthor() === $user && in_array($article->getId(), $articles);
        })
        ->setEntityPermission('ROLE_USER', 'VIEW', function (Article $article) use ($user, $articles) {
            return $this->isGranted('ROLE_USER') && $article->getAuthor() === $user && in_array($article->getId(), $articles);
        })
        ->setEntityPermission('ROLE_USER', 'DELETE', function (Article $article) use ($user, $articles) {
            return $this->isGranted('ROLE_USER') && $article->getAuthor() === $user && in_array($article->getId(), $articles);
        });

    }


    
}

Thank you for your help.

0 Answers0