首页 > 解决方案 > 在 Easyadmin 3 UserProfile 视图中调用成员函数 all()

问题描述

我需要创建一个视图,以便用户可以编辑他的个人资料,但它不能是相同的编辑视图,因为有些字段只有用户可以在会话中访问,例如密码,其他用户不能,甚至管理员也不能更改密码等字段。我想利用 easyadmin 的所有力量。这是我的代码:

class DashboardController extends AbstractDashboardController
{

   public function configureUserMenu(UserInterface $user): UserMenu
   {
     // ...

     $profileUrl = $this->get(CrudUrlGenerator::class)
       ->build()
       ->setAction('profile')
       ->setController("App\\Controller\\UserCrudController")
       ->setEntityId($user->getId())
       ->includeReferrer(true)
       ->generateUrl();
     $menus->addMenuItems([
       MenuItem::linkToUrl('Profile', 'fa fa-user', $profileUrl),
     ]);

     return $menus;
   }

}

class UserCrudController extends AbstractCrudController
{
   public function configureFields(string $pageName): iterable
   {
      // ...

      if ($pageName == 'profile'){
       $fields [] = TextField::new('password', 'Current Password')
         ->setFormType(PasswordType::class)
         ->setRequired(true)
         ;
       $fields[] = Field::new('plainPassword', 'New password')->onlyOnForms()
             ->setFormType(RepeatedType::class)
             ->setFormTypeOptions([
               'type' => PasswordType::class,
               'first_options' => ['label' => 'New password'],
               'second_options' => ['label' => 'Repeat password'],
             ]);
      }
   }

//NOTE: At the moment I have copied and pasted the edit code to see the view that it shows and modify it. But I get the error shown below

   public function profile(AdminContext $context)
   {
     $event = new BeforeCrudActionEvent($context);
     $this->get('event_dispatcher')->dispatch($event);
     if ($event->isPropagationStopped()) {
       return $event->getResponse();
     }
 
     if (!$this->isGranted(Permission::EA_EXECUTE_ACTION)) {
       throw new ForbiddenActionException($context);
     }
 
     if (!$context->getEntity()->isAccessible()) {
       throw new InsufficientEntityPermissionException($context);
     }
 
     $context->getEntity()->setInstance($this->createEntity($context->getEntity()->getFqcn()));
     $this->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_NEW)));
     $this->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
 
     $user = $this->getUser();
     $newForm = $this->createForm(User::class, $user);
     $newForm->handleRequest($context->getRequest());
 
     $entityInstance = $newForm->getData();
     $context->getEntity()->setInstance($entityInstance);
 
     if ($newForm->isSubmitted() && $newForm->isValid()) {
       $this->processUploadedFiles($newForm);
 
       $event = new BeforeEntityPersistedEvent($entityInstance);
       $this->get('event_dispatcher')->dispatch($event);
       $entityInstance = $event->getEntityInstance();
 
       $this->persistEntity($this->get('doctrine')->getManagerForClass($context->getEntity()->getFqcn()), $entityInstance);
 
       $this->get('event_dispatcher')->dispatch(new AfterEntityPersistedEvent($entityInstance));
       $context->getEntity()->setInstance($entityInstance);
 
       $submitButtonName = $context->getRequest()->request->all()['ea']['newForm']['btn'];
       if (Action::SAVE_AND_CONTINUE === $submitButtonName) {
         $url = $this->get(AdminUrlGenerator::class)
           ->setAction(Action::EDIT)
           ->setEntityId($context->getEntity()->getPrimaryKeyValue())
           ->generateUrl();
 
         return $this->redirect($url);
       }
 
       if (Action::SAVE_AND_RETURN === $submitButtonName) {
         $url = $context->getReferrer()
           ?? $this->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl();
 
         return $this->redirect($url);
       }
 
       if (Action::SAVE_AND_ADD_ANOTHER === $submitButtonName) {
         $url = $this->get(AdminUrlGenerator::class)->setAction(Action::NEW)->generateUrl();
 
         return $this->redirect($url);
       }
 
       return $this->redirectToRoute($context->getDashboardRouteName());
     }
 
     $responseParameters = $this->configureResponseParameters(KeyValueStore::new([
       'pageName' => 'profile',
       'templateName' => 'crud/edit',
       'entity' => $context->getEntity(),
       'new_form' => $newForm,
     ]));
 
     $event = new AfterCrudActionEvent($context, $responseParameters);
     $this->get('event_dispatcher')->dispatch($event);
     if ($event->isPropagationStopped()) {
       return $event->getResponse();
     }
 
     return $responseParameters;
   }

}


但是我收到以下错误:“调用数组上的成员函数 all ()”

在此处输入图像描述

正如这里所说,为自定义操作添加自定义页面我可以创建一个全新的视图,但正如我之前所说,我希望能够使用 easyadmin 的所有灵活性和强大功能。

标签: symfony5easyadmin3

解决方案


像这样设置字段权限会更容易:

// users must have this permission/role to see this field
IntegerField::new('sales')->setPermission('ROLE_ADMIN'),
FloatField::new('commission')->setPermission('ROLE_FINANCE'),

因此,只有某些角色可以访问某些字段。

Se 文档https://symfony.com/doc/current/bundles/EasyAdminBundle/security.html#restrict-access-to-fields


推荐阅读