类型错误:传递给Symfony \ Component \ Security \ Core \ Role \ RoleHierarchy :: getReachableRoles()的参数1必须为数组类型,给定为null

时间:2019-01-13 19:17:41

标签: symfony heroku symfony-3.4

我在security.yml中定义了3个角色,如下所示:

role_hierarchy:
    ROLE_ADMIN: [ROLE_MANAGER]
    ROLE_MANAGER: [ROLE_EMPLOYEE]
    ROLE_EMPLOYEE: [ROLE_USER]

有时会出现以下错误:

  

2019-01-13T19:07:19 + 00:00 [紧急]类型错误:传递给Symfony \ Component \ Security \ Core \ Role \ RoleHierarchy :: getReachableRoles()的参数1必须为       类型数组的名称,给定null,在/ app / vendor / sensio / framework-extra-中调用       第90行的bundle / EventListener / SecurityListener.php

     

2019-01-13T19:07:19.205536 + 00:00 app [web.1]:2019-01-13T19:07:19 + 00:00       [critical]未捕获的PHP异常       Symfony \ Component \ Debug \ Exception \ FatalThrowableError:“类型错误:参数1       传递给       Symfony \ Component \ Security \ Core \ Role \ RoleHierarchy :: getReachableRoles()必须为       类型数组的名称,给定null,在/ app / vendor / sensio / framework-extra-中调用       在第90行上的bundle / EventListener / SecurityListener.php,位于/app/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Role/RoleHierarchy.php第37行

     

2019-01-13T19:07:19.208667 + 00:00 app [web.1]:2019-01-13T19:07:19 + 00:00       [警告]警告:array_map():预期参数2为数组,给定为空

你有什么主意吗?

更新 SendSurveyFormBuilder.php

class SendSurveyFormBuilder extends FormBuilder
{
    private $teamManager;
    private $officeManager;
    private $projectManager;
    private $formFactory;
    private $surveyTypeManager;

    public function __construct
    (
      # TeamManager $teamManager,
   # OfficeManager $officeManager,
   # ProjectManager $projectManager,
    FormFactoryInterface $formFactory#,
   # SurveyTypeManager $surveyTypeManager
)
{
   # $this->teamManager = $teamManager;
   # $this->officeManager = $officeManager;
   # $this->projectManager = $projectManager;
    $this->formFactory = $formFactory;
   # $this->surveyTypeManager = $surveyTypeManager;
}

public function buildForm(Company $company)
{
    #$company = $user->getCompany();

    $teams = [];
    $projects = [];
    $offices = [];
    $surveyTypeChoices = [];

    $targetChoices = [];

    $options = [];

    #        if ($user->hasRole("ROLE_ADMIN")) {

    /*

    $teams = $this->teamManager->getTeamsByCompany($company);
    $projects = $this->projectManager->getByCompany($company);
    $offices = $this->officeManager->getOfficesByCompany($company);


    */

    $targetChoices["Groups"] = [
        sprintf("%s (%s)", 'All Employees', $company->getEmployees()->count()) => new Group(Team::GROUP_ALL),
        // 'All Managers' => Team::GROUP_MANAGERS,
        //  'All Employees without Managers' => Team::GROUP_NO_MANAGERS
    ];

   /* } elseif ($user->hasRole("ROLE_MANAGER")) {

        $teams = $this->teamManager->getTeamsByManager($user);
        $projects = $this->projectManager->getByManager($user);

    }*/


    $targetChoices["Teams"] = $this->transformTargetChoices($teams);
    $targetChoices["Projects"] = $this->transformTargetChoices($projects);
    $targetChoices["Offices"] = $this->transformTargetChoices($offices);

   /*
    * $standardSurveyTypes = $this->surveyTypeManager->getStandardSurveyTypes();
        $customSurveyTypes = $this->surveyTypeManager->getCustomSurveyTypesByCompany($user->getCompany());
    *
    *

    $surveyTypeChoices = [
        "Standard" => $this->transformSurveyTypeChoices($standardSurveyTypes),
        "Custom" => $this->transformSurveyTypeChoices($customSurveyTypes)
    ];*/

    $options["targetChoices"] = $targetChoices;
    $options["surveyTypeChoices"] = $surveyTypeChoices;

    $this->form = $this->formFactory->create(NewSurveyType::class, null, $options);

    return $this;
}

private function transformTargetChoices($objects)
{
    $choices = [];
    for ($i=0; $i<count($objects); $i++) {
        $name = sprintf("%s (%s)", $objects[$i]->getName(), $objects[$i]->getEmployees()->count());
        $choices[$name] = $objects[$i];
    }

    return $choices;
}

private function transformSurveyTypeChoices($objects)
{
    $choices = [];
    for ($i=0; $i<count($objects); $i++) {
        $name = $objects[$i]->getName();
        $choices[$name] = $objects[$i];
    }

    return $choices;
}
}

实际形式:

 class NewSurveyType extends AbstractType
 {

    public function buildForm(FormBuilderInterface $builder, array $options)
{
    $targetChoices = $options["targetChoices"];
    $surveyTypeChoices = $options["surveyTypeChoices"];

    $builder
        ->add("surveyType", ChoiceType::class, [
            "choices" => $surveyTypeChoices
        ])
        ->add("target", ChoiceType::class, [
            'choices' => $targetChoices,
            "mapped" => false
        ])
        ->add("deadline", ChoiceType::class, [
            "choices" => [
                "Same Day" => QuestionnaireInterval::RANGE_SAME_DAY,
                "7 Days" => QuestionnaireInterval::RANGE_7_DAYS,
                "30 Days" => QuestionnaireInterval::RANGE_30_DAYS
            ]
        ])
    ;
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setRequired("targetChoices");
    $resolver->setRequired("surveyTypeChoices");
}
}

要测试的控制器方法:

/**
 * @Route("/test-survey", name="survey_test")
 */
public function testAction()
{
    $user = $this->getUser();
    $company = $user->getCompany();

    $form = $this->sendSurveyFormBuilder->buildForm($company)->getForm();

    return new Response();
}

当我去这条路线,然后再去另一条路线(即使我在浏览器中手动输入它,也会发生错误。我整天都在处理该错误。

1 个答案:

答案 0 :(得分:1)

看来,问题在于我尚未实现用于(取消)序列化包含角色的用户对象的方法。当我添加这些时,一切再次正常。仍然想知道为什么它以前可行。