I'm learning Symfony at the moment and have a problem with authentication of users that are stored in a database. I have followed the Tutorials from here but I always receive an "invalid credentials" error. Here comes my code so far (sorry for the names but this is what the "customer" wants).
security.yml:
security:
encoders:
AppBundle\Entity\Benutzer:
algorithm: bcrypt
providers:
mysql_provider:
entity:
class: AppBundle:Benutzer
property: bNID
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
default:
anonymous: ~
form_login:
login_path: login
check_path: login
require_previous_session: false
default_target_path: /auth/bnme
provider: mysql_provider
logout:
path: /logout
target: /
This is how I load my user into the database
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\Benutzer;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class UserController extends Controller
{
/**
* @Route("/user", name="user")
*/
public function createAction(){
//Get Entity Object
$user = new Benutzer();
//Encrypt password
$plainpw = 'Initial-PW-55218';
$encoder = $this->container->get('security.password_encoder');
$pwd = $encoder->encodePassword($user, $plainpw);
//Set Entity fields
$user->setANREDE("Herr");
$user->setBNID("[email protected]");
$user->setINIPW($pwd);
$user->setBNTYP("SA");
$user->setKOEPID(1);
$user->setNACHNAME("KomBau-System");
$user->setVORNAME("UL");
//get Entity Manager
$em = $this->getDoctrine()->getManager();
//write Entity in database
$em->persist($user);
$em->flush();
return new Response('Created user with id: '.$user->getBNID());
}
}
and this is my loginAction:
public function loginAction(Request $request){
$authenticationUtils = $this->get('security.authentication_utils');
$error = $authenticationUtils->getLastAuthenticationError();
$system = $this->getDoctrine()->getRepository('AppBundle:System')->find(1);
return $this->render('default/sstm.html.twig', array('error' => $error, 'system' => $system));
}
The login with users that are hard coded into security.yml works. But this is not what is wanted by the customer. Can somebody help me with this?
This is the entity class for the user. I left the getters and setters away due to the length of the code:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
*
* @ORM\Table(name="Benutzer_TAB")
* @ORM\Entity
*
* This class represents database table Benutzer_TAB which holds user informations
*/
class Benutzer implements UserInterface, \Serializable
{
/**
* @var int
*
* @ORM\Column(name="KOEPID", type="integer", length=8)
*/
private $kOEPID;
/**
* @var string
*
* @ORM\Column(name="BNID", type="string", length=255, unique=true)
* @ORM\id
*
* User-ID as email adress
*/
private $bNID;
/**
* @var string
*
* @ORM\Column(name="BNTYP", type="string", length=2)
*
* Usertype (Systemadmin or normal user)
*/
private $bNTYP;
/**
* @var string
*
* @ORM\Column(name="ANREDE", type="string", length=4)
*/
private $aNREDE;
/**
* @var string
*
* @ORM\Column(name="VORNAME", type="string", length=255)
*/
private $vORNAME;
/**
* @var string
*
* @ORM\Column(name="NACHNAME", type="string", length=255)
*/
private $nACHNAME;
/**
* @var string
*
* @ORM\Column(name="INIPW", type="string", length=255)
*/
private $iNIPW;
/**
* @var string
*
* @ORM\Column(name="BNPW", type="string", length=255, nullable=true)
*/
private $bNPW;
/**
* {@inheritDoc}
* @see \Symfony\Component\Security\Core\User\UserInterface::getRoles()
*/
public function getRoles() {
return array('ROLE_USER');
}
/**
* {@inheritDoc}
* @see \Symfony\Component\Security\Core\User\UserInterface::getPassword()
*/
public function getPassword() {
if(!$this->getBNPW()){
$this->getINIPW();
} else {
$this->getBNPW();
}
}
/**
* {@inheritDoc}
* @see \Symfony\Component\Security\Core\User\UserInterface::getSalt()
*/
public function getSalt() {
return null;
}
/**
* {@inheritDoc}
* @see \Symfony\Component\Security\Core\User\UserInterface::getUsername()
*/
public function getUsername() {
$this->getBNID();
}
/**
* {@inheritDoc}
* @see \Symfony\Component\Security\Core\User\UserInterface::eraseCredentials()
*/
public function eraseCredentials() {
// TODO: Auto-generated method stub
}
/**
* {@inheritDoc}
* @see Serializable::serialize()
*/
public function serialize() {
return serialize(array(
$this->kOEPID,
$this->bNID,
$this->iNIPW,
));
}
/**
*
* {@inheritDoc}
*
* @see Serializable::unserialize()
*/
public function unserialize($serialized) {
list(
$this->kOEPID,
$this->bNID,
$this->iNIPW,) = unserialize($serialized);
}
}