3

I'm trying to encode the password in symfony3 but i'm facing some problems. this is the error i got.

Catchable Fatal Error: Argument 3 passed to GMAOBundle\Security\LoginFormAuthentificator::__construct() must be an instance of Symfony\Component\Security\Core\Encoder\UserPasswordEncoder, none given, called in C:\xampp\htdocs\ProjetSomadiag\var\cache\dev\appDevDebugProjectContainer.php on line 483 and defined

I'm working with guard in the authentification. this is the authentification class name loginformauthentificator.

namespace GMAOBundle\Security;


use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Encoder\EncoderFactory;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;



class LoginFormAuthentificator extends  AbstractGuardAuthenticator
{
private $em;
private $router;
private $passwordEncoder;


public function __construct($em, $router,UserPasswordEncoder  $PasswordEncoder )
{
    $this->em = $em;
    $this->router = $router;
    //$this->passwordEncoder = new UserPasswordEncoder();
    $this->passwordEncoder = $PasswordEncoder;
}


public function getCredentials(Request $request)
{
    if ($request->getPathInfo() != '/login' || !$request->isMethod('POST')) {
        return;
    }
    $request->getSession()->set(Security::LAST_USERNAME,$request->request->get('_username'));

    return [
        'username' => $request->request->get('_username'),
        'password' => $request->request->get('_password'),
    ];
}


public function getUser($credentials, UserProviderInterface $userProvider)
{
    $username = $credentials['username'];

    return $this->em->getRepository('GMAOBundle:Employe')
        ->findOneBy(['EmailEmploye' => $username]);
}

/**
 * @param mixed $credentials
 * @param UserInterface $user
 * @return bool
 */
public function checkCredentials($credentials, UserInterface $user)
{

    $password=$credentials['password'];
    if ($this->passwordEncoder->isPasswordValid($user, $password)) {
        return true;
    }
    return false;
}

public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
    $request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
    $url = $this->router->generate('login');
    return new RedirectResponse($url);
}

public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
    $url = $this->router->generate('gmao_homepage');
    return new RedirectResponse($url);
}

public function supportsRememberMe()
{
    return true;
}

public function start(Request $request, AuthenticationException $authException = null)
{
    $url = $this->router->generate('login');
    return new RedirectResponse($url);
}


}

and this is the user class.

  namespace GMAOBundle\Entity;
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Security\Core\User\UserInterface;


  /**
  * @ORM\Entity
  * @ORM\Table(name="employe")
  */
  class Employe implements UserInterface
  {

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */private $idEmploye;
/**
 * @ORM\Column(type="string")
 */private $NomEmploye;
/**
 * @ORM\Column(type="string")
 */private $PrenomEmploye;
/**
 * @ORM\Column(type="date", nullable=true)
 */private $DateNaissanceEmploye;
/**
 * @ORM\Column(type="string")
 */private $cinEmploye;
/**
 * @ORM\Column(type="string", nullable=true)
 */private $AdresseEmploye;
/**
 * @ORM\Column(type="string")
 */private $MatriculeEmploye;
/**
 * @ORM\Column(type="string", nullable=true)
 */private $SituationFamiliale;
/**
 * @ORM\Column(type="string", nullable=true)
 */private $SexeEmploye;
/**
 * @ORM\Column(type="string", nullable=true)
 */private $CnssEmploye;
/**
 * @ORM\Column(type="string", nullable=true)
 */private $FonctionEmploye;
/**
 * @ORM\Column(type="string", nullable=true)
 */private $NumeroMutuelle;
/**
 * @ORM\Column(type="date", nullable=true)
 */private $DateDebutEmploye;
/**
 * @ORM\Column(type="date", nullable=true)
 */private $DateTitularisationEmploye;
/**
 * @ORM\Column(type="string", nullable=true)
 */private $NumeroTelEmploye;
/**
 * @ORM\Column(type="string", nullable=true)
 */private $NumeroLigneEmploye;
/**
 * @ORM\Column(type="string")
 */private $EmailEmploye;

/**
 * A non-persisted field that's used to create the encoded password.
 * @var string
 */private $plainPassword;

/**
 * @ORM\Column(type="string")
 */private $MdpEmploye;

/**
 * @ORM\Column(type="string")
 */private $active;



/**
 * @ORM\Column(type="json_array")
 */private $roles = [];

/**
 * @ORM\ManyToOne(targetEntity="Departement")
 * @ORM\JoinColumn(name="Departement", 
   referencedColumnName="id_departement")
 */private $Departement;

/**
 * @ORM\ManyToOne(targetEntity="Equipe", cascade={"persist"})
 * @ORM\JoinColumn(name="Equipe", referencedColumnName="id" ,nullable=true)
 */private $equipe;

/**
 * @ORM\Column(type="string", nullable=true)
 */private $imgsrc;
 /**
 * @ORM\Column(type="string")
 *private $responsable_projet;
/**
 * @ORM\Column(type="string")
 *private $m_GMAO_Projet;
/**
 * @ORM\Column(type="string")
 *public $m_PMP_Objectif;
/**
 * @ORM\Column(type="string")
 *public $m_Services;*/



/**
 * User Interface Methode Override
 */

function __construct()
{
}

function __destruct()
{
}

public function getUsername()
{
    return $this->EmailEmploye;
}

public function getRoles()
{
    $roles = $this->roles;
        if(!in_array('ROLE_USER',$roles))
        {
            $roles[]='ROLE_USER';
        }

    return $roles;
}

public function getPassword()
{
    return$this->MdpEmploye;
}

public function getSalt()
{
}

public function eraseCredentials()
{
    $this->plainPassword = null;
}




/**
 * GETTERS AND SETTERS
 **/


/**
 * @return mixed
 */
public function getIdEmploye()
{
    return $this->idEmploye;
}

/**
 * @param mixed $idEmploye
 */
public function setIdEmploye($idEmploye)
{
    $this->idEmploye = $idEmploye;
}

/**
 * @return mixed
 */
public function getNomEmploye()
{
    return $this->NomEmploye;
}

/**
 * @param mixed $NomEmploye
 */
public function setNomEmploye($NomEmploye)
{
    $this->NomEmploye = $NomEmploye;
}

/**
 * @return mixed
 */
public function getPrenomEmploye()
{
    return $this->PrenomEmploye;
}

/**
 * @param mixed $PrenomEmploye
 */
public function setPrenomEmploye($PrenomEmploye)
{
    $this->PrenomEmploye = $PrenomEmploye;
}

/**
 * @return mixed
 */
public function getDateNaissanceEmploye()
{
    return $this->DateNaissanceEmploye;
}

/**
 * @param mixed $DateNaissanceEmploye
 */
public function setDateNaissanceEmploye($DateNaissanceEmploye)
{
    $this->DateNaissanceEmploye = $DateNaissanceEmploye;
}

/**
 * @return mixed
 */
public function getcinEmploye()
{
    return $this->cinEmploye;
}

/**
 * @param mixed $cinemploye
 */
public function setcinEmploye($Cinemploye)
{
    $this->cinEmploye = $Cinemploye;
}

/**
 * @return mixed
 */
public function getAdresseEmploye()
{
    return $this->AdresseEmploye;
}

/**
 * @param mixed $AdresseEmploye
 */
public function setAdresseEmploye($AdresseEmploye)
{
    $this->AdresseEmploye = $AdresseEmploye;
}

/**
 * @return mixed
 */
public function getMatriculeEmploye()
{
    return $this->MatriculeEmploye;
}

/**
 * @param mixed $MatriculeEmploye
 */
public function setMatriculeEmploye($MatriculeEmploye)
{
    $this->MatriculeEmploye = $MatriculeEmploye;
}

/**
 * @return mixed
 */
public function getSituationFamiliale()
{
    return $this->SituationFamiliale;
}

/**
 * @param mixed $SituationFamiliale
 */
public function setSituationFamiliale($SituationFamiliale)
{
    $this->SituationFamiliale = $SituationFamiliale;
}

/**
 * @return mixed
 */
public function getSexeEmploye()
{
    return $this->SexeEmploye;
}

/**
 * @param mixed $SexeEmploye
 */
public function setSexeEmploye($SexeEmploye)
{
    $this->SexeEmploye = $SexeEmploye;
}

/**
 * @return mixed
 */
public function getCnssEmploye()
{
    return $this->CnssEmploye;
}

/**
 * @param mixed $CnssEmploye
 */
public function setCnssEmploye($CnssEmploye)
{
    $this->CnssEmploye = $CnssEmploye;
}

/**
 * @return mixed
 */
public function getFonctionEmploye()
{
    return $this->FonctionEmploye;
}

/**
 * @param mixed $FonctionEmploye
 */
public function setFonctionEmploye($FonctionEmploye)
{
    $this->FonctionEmploye = $FonctionEmploye;
}

/**
 * @return mixed
 */
public function getNumeroMutuelle()
{
    return $this->NumeroMutuelle;
}

/**
 * @param mixed $NumeroMutuelle
 */
public function setNumeroMutuelle($NumeroMutuelle)
{
    $this->NumeroMutuelle = $NumeroMutuelle;
}

/**
 * @return mixed
 */
public function getDateDebutEmploye()
{
    return $this->DateDebutEmploye;
}

/**
 * @param mixed $DateDebutEmploye
 */
public function setDateDebutEmploye($DateDebutEmploye)
{
    $this->DateDebutEmploye = $DateDebutEmploye;
}

/**
 * @return mixed
 */
public function getDateTitularisationEmploye()
{
    return $this->DateTitularisationEmploye;
}

/**
 * @param mixed $DateTitularisationEmploye
 */
public function setDateTitularisationEmploye($DateTitularisationEmploye)
{
    $this->DateTitularisationEmploye = $DateTitularisationEmploye;
}

/**
 * @return mixed
 */
public function getNumeroTelEmploye()
{
    return $this->NumeroTelEmploye;
}

/**
 * @param mixed $NumeroTelTmploye
 */
public function setNumeroTelEmploye($NumeroTelEmploye)
{
    $this->NumeroTelEmploye = $NumeroTelEmploye;
}

/**
 * @return mixed
 */
public function getNumeroLigneEmploye()
{
    return $this->NumeroLigneEmploye;
}

/**
 * @param mixed $NumeroLigneEmploye
 */
public function setNumeroLigneEmploye($NumeroLigneEmploye)
{
    $this->NumeroLigneEmploye = $NumeroLigneEmploye;
}

/**
 * @return mixed
 */
public function getEmailEmploye()
{
    return $this->EmailEmploye;
}

/**
 * @param mixed $EmailEmploye
 */
public function setEmailEmploye($EmailEmploye)
{
    $this->EmailEmploye = $EmailEmploye;
}

/**
 * @return mixed
 */
public function getMdpEmploye()
{
    return $this->MdpEmploye;
}

/**
 * @param mixed $MdpEmploye
 */
public function setMdpEmploye($MdpEmploye)
{
    $this->MdpEmploye = $MdpEmploye;
}

/**
 * @return mixed
 */
public function getActive()
{
    return $this->active;
}

/**
 * @param mixed $active
 */
public function setActive($active)
{
    $this->active = $active;
}

/**
 * @return mixed
 */
public function getDepartement()
{
    return $this->Departement;
}

/**
 * @param mixed $Departement
 */
public function setDepartement($Departement)
{
    $this->Departement = $Departement;
}

/**
 * @return mixed
 */
public function getImgsrc()
{
    return $this->imgsrc;
}

/**
 * @param mixed $imgsrc
 */
public function setImgsrc($imgsrc)
{
    $this->imgsrc = $imgsrc;
}


public  function setRoles($roles)
{
    $this->roles =$roles;
}

/**
 * @return mixed
 */
public function getEquipe()
{
    return $this->equipe;
}

/**
 * @param mixed $Equipe
 */
public function setEquipe($Equipe)
{
    $this->equipe = $Equipe;
}

/**
 * @return string
 */
public function getPlainPassword()
{
    return $this->plainPassword;
}

/**
 * @param string $plainPassword
 */
public function setPlainPassword($plainPassword)
{
    $this->plainPassword = $plainPassword;
    $this->password = null;
}
}

i don't know what this error stand for.

1
  • When you create LoginFormAuthentificator instance, you have to pass 3 parameters and the third one must be instance of Symfony\Component\Security\Core\Encoder\UserPasswordEncoder. Commented Jul 17, 2017 at 12:18

1 Answer 1

3

If you are using dependency injection, you must send your password encoder in as your third argument.

Example: services.yml

services:
     app.loginform.authenticator:
     class: GMAOBundle\Security\LoginFormAuthentificator
       arguments: 
           - "@doctrine.orm.entity_manager"
           - "@router"
           - "@passwordencoder"

Alternatively you can "autowire".

services:
     app.loginform.authenticator:
     class: GMAOBundle\Security\LoginFormAuthentificator
     autowire: true

Or however you're using your services.yml to inject dependencies.

If you're creating the loginFormAuthenticator as an object you have to instantiate it as:

$loginAuthenticator = new  LoginFormAuthentificator($entitymanager, $router, $passwordEncoder);

You're missing the password encoder in some argument when you call the class.

Sign up to request clarification or add additional context in comments.

4 Comments

i tried to add this in the services.yaml: but style have this problem services: app.loginform.authenticator: class: AppBundle\Etc\Etc\LoginFormAuthentificator autowire: true
Right. You have to replace the "AppBundle/Etc/Etc/LoginFormAuthentificator" with your own class. The "Etc/Etc/" is just my placeholder because I don't know where your class is.
In your yml file if you start typing: class: LoginFormAuth Your yml autocomplete should find your class file.
There, I edited my answer so it looks more like your yml file should look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.