This-> getUser () returns null after validating the form

I am creating a form with a custom option based on who is logged in. The goal is to send an invitation.

The type of invitation you can send is determined by the getIsRefFrom () method (which is the method available for User objects).

Here is my controller

<?php

namespace AppBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use AppBundle\Entity\Invitation;
use AppBundle\Form\InvitationType;
use AppBundle\Entity\User;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;

/**
 * Invitation controller.
 *
 * @Route("/private/invitation")
 */
class InvitationController extends Controller 
{
...
    public function newAction(Request $request)
    {

        $invitation = new Invitation();
        $user = $this->getUser();
        $userStruct = $user->getIsRefFrom();
        $form = $this->createForm('AppBundle\Form\InvitationType', $invitation, array('userStruct' => $userStruct));         

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {     

            return new Response("<body>Hi</body>");

        }

        return $this->render('invitation/new.html.twig', array(
            'invitation' => $invitation,
            'form' => $form->createView(),
        ));
    }

...
}

And here is my type of form

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;

class InvitationType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {       

        $builder
             //showing content of userStruct in a select field
            ->add('structure', EntityType::class,  array(
                    'label' => 'Structure:',
                    'class' => 'AppBundle:Structure',
                    'choices' => $options['userStruct'],
                    'placeholder' => 'Faites un choix',
                    'property' => 'libelle',
                    'mapped' => false
            ))


        ->add('destinataire');
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Invitation',
            'userStruct' => 'AppBundle\Entity\Structure',
        ));
    }
}

The form displays OK, $ this-> getUser () returns my user, and the contents of $ user-> getIsRefFrom () correctly displays on the form.

In addition, the debug toolbar indicates that I am correctly registered as I am filling out a form.

validate , ": - getIsRefFrom() null", $this- > getUser(); null.

n/a ( , ...)

, .

:

  • be_simple_sso_auth (cas auth bundle).
  • (derriere_CAS), , , ACL (. security.yml).
  • .

security.yml

security:
  encoders:
    AppBundle\Entity\User: 
      algorithm: bcrypt

  role_hierarchy:
        ROLE_ADMIN: [ROLE_USER]

  providers:
    users:
      entity: {class: AppBundle\Entity\User, property: username } 



  firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        derriere_CAS:
            methods: GET
            pattern: ^/
            anonymous: ~
            trusted_sso:
              manager: connect_sso 
              login_path: /private/login/
              check_path: /private/login/check
              login_action: false
              logout_action: false
              create_users: true
              created_users_roles: [ 'ROLE_USER' ]
              failure_path: /erreur_identification
            logout:
              path: /logout_sso
              target: /

  access_control:
      - { path: ^/private/login, role: IS_AUTHENTICATED_ANONYMOUSLY } 
      - { path: ^/, roles: IS_AUTHENTICATED_FULLY }  

, - . . .

+4

All Articles