1

To put in context there is an editor who can modify his information according to his id and his information must be validated by the admin before persisting in the database.

I have in mind to create a temporary table to store the information that the editor will modify and the admin can access his information via the interface of EasyAdmin then as soon as the admin validates the information it will persist in the original table.

I did not find in the documentation the way to recover the information of the form of the edit page.

I join the crud controller that I want to target:

class OperateurCrudController extends AbstractCrudController {

private $crudUrlGenerator;
private $adminContextProvider;

public function __construct(AdminUrlGenerator $adminUrlGenerator, AdminContextProvider $adminContextProvider)
{
    $this->adminUrlGenerator = $adminUrlGenerator;
    $this->adminContextProvider = $adminContextProvider;

}

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

public function configureFields(string $pageName): iterable
{
        return [
            IdField::new('id')->hideOnForm(),
            TextField::new('name', 'Nom'),
            TextEditorField::new('description', 'Description')->hideOnIndex(),
            TextareaField::new('address', 'Adresse'),
            TextField::new('city', 'Ville')->hideOnIndex(),
            TextField::new('postal_code', 'Code Postal')->hideOnIndex(),
            TextField::new('email', 'Email'),
            TelephoneField::new('phone', 'Téléphone'),
            UrlField::new('website', 'Site Web')->hideOnIndex(),
            TextEditorField::new('opening_hours', 'Horaires d\'Ouverture'),
            NumberField::new('latitude', 'Latitude')->hideOnIndex()->setNumDecimals(15),
            NumberField::new('longitude', 'Longitude')->hideOnIndex()->setNumDecimals(15),
            TextField::new('slug', 'Slug')->hideOnIndex()->setPermission('ROLE_ADMIN'),
            DateTimeField::new('created_at', 'Date Création')->onlyOnIndex(),
            DateTimeField::new('updated_at', 'Date Modification')->onlyOnIndex(),
            AssociationField::new('thematiques', 'Thématiques')

        ];

}

public function configureActions(Actions $actions): Actions
{
    $batchAction = Action::new('approve', 'Approuver', 'fa fa-user-check')
        ->linkToUrl('approveOperators');

    $updateOperator = Action::new('update', 'Enregistrer les modifications', 'fa fa-save')
        ->linkToCrudAction('saveOperator');


    if($this->isGranted('ROLE_ADMIN')){
        return $actions
            ->add(Crud::PAGE_INDEX, $batchAction)
            ->add(Crud:: PAGE_INDEX, Action::DETAIL)
            ->setPermission(Action::DELETE, 'ROLE_ADMIN')
            ->setPermission(Action::NEW, 'ROLE_ADMIN')
            ->setPermission(Action::EDIT, 'ROLE_ADMIN')
            ->setPermission($batchAction, 'ROLE_ADMIN');
    }

    if($this->isGranted('ROLE_EDITOR')){
        return $actions
            ->add(Crud:: PAGE_INDEX, Action::DETAIL)
            ->add(Crud::PAGE_EDIT, $updateOperator)
            ->setPermission(Action::DELETE, 'ROLE_ADMIN')
            ->setPermission(Action::NEW, 'ROLE_ADMIN')
            ->setPermission($updateOperator, 'ROLE_EDITOR')
        ->disable( Action::SAVE_AND_RETURN, Action::SAVE_AND_CONTINUE);
    }


}

public function approveOperators(): Response{

    $this->addFlash('notice', '<span style="color: green"><i class="fa fa-check"></i>Modification effecuté </span>');

    $url = $this->adminUrlGenerator
        ->setAction(Action::INDEX)
        ->generateUrl();


    return $this->redirect($url);


}



public function saveOperator(){
    //$this->addFlash('notice', '<span style="color: green"><i class="fa fa-check"></i>Modification pris en compte ! </span>');

    //Create my own save button in page edit

}


public function configureCrud(Crud $crud): Crud
{
    return $crud
        ->setEntityPermission('ROLE_EDITOR');
}



public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
{

    $response = parent::createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters);
    if(!$this->isGranted('ROLE_ADMIN')){
        $response->where('entity.id = :id');
        $response->setParameter('id', $this->getUser()->getOperateur());
    }


    return $response;

}

public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void
{
    parent::updateEntity($entityManager, $entityInstance); // TODO: Change the autogenerated stub
}

Thank you for reading

Maxime05
  • 11
  • 1
  • And what prevents you to copy the data over once the admin presses the "accept" button (or however you want to design the accept mechanism)? Or set an "accepted" field to true? – hakre Jul 16 '21 at 13:45
  • I had also considered this solution of adding a field if the information is accepted, but I find myself with the same problem of recovering the values of the form. And how to store the information (if for example the pc crashes at the same time) – Maxime05 Jul 16 '21 at 13:54
  • Make two entities. One is the one you have. The other relates to it by a reference and an accept field. only the admin can edit the later entity. and if your pc crashes, use a database that can handle crashes. – hakre Jul 16 '21 at 13:57
  • Okay thanks for this solution! But what is the method to get the information from the form? Or a way to override the save and return button – Maxime05 Jul 16 '21 at 14:10
  • Well perhaps its then not a solution, I don't know. EasyAdmin3 might have a way to to allow/disallow record based role access? (e.g. once accepted, one role can't edit the record any longer). – hakre Jul 16 '21 at 14:14
  • Ok, I'll see if depending on the user's role we can modify the behavior of the save button already set up by EasyAdmin3 – Maxime05 Jul 16 '21 at 14:26

1 Answers1

-1

you should use BeforeEntityPersistedEvent::class

It will autotrigger just before persisting an element in database

class EasyAdminSubscriber extends AbstractController implements EventSubscriberInterface
{

  public function __construct( )
      {

      }

  public static function getSubscribedEvents(){

    return [
      BeforeEntityPersistedEvent::class => ['functionName']
    ];
  }

  public function functionName(BeforeEntityPersistedEvent $event){

    $entity=$event->getEntityInstance();

    $user = $this->getUser();
    $userRole = $user->getRole();
    $check = $this->checkRole($userRole);

    if ($entity instanceof yourEntity && $check== true) {

    
           //Do your magic with changing the value you want in the table you
       }
    }

}

Sorry to no comment, i dont have enough reputation for it so i answer there.

Cephalopodius
  • 117
  • 1
  • 10
  • Please share more details. How does this ensure that the data can be presented to the admin after being edited by a common user? – Nico Haase Jul 21 '21 at 11:47
  • He want a validation system by admin on prepersist data, so user try to persist data, trigger this event, and then he can choose his way. Sending these information with a check field on null on table, sending an alerte "waiting for validation for the admin, or sending a link to an admin with a form full of preset with these informations. – Cephalopodius Jul 22 '21 at 15:03