0

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);
}

}

3 Answers 3

1

Thanks for the entity,

Why in your firewall your provider is comment ? You should have:

 default:
    anonymous: ~
    form_login:
      provider: mysql_provider
      login_path: login
      check_path: login
      require_previous_session: false
      default_target_path: /auth/bnme
Sign up to request clarification or add additional context in comments.

11 Comments

Yes it was a little mistake. See the comment above
the provider should be under form_login to work no ?
Well, that actually changed the error message. I take this as a good sign. Now it says "Authentication request could not be processed due to a system problem."
Yes :) You can check the logs to see what sort of system problem is it ?
Your database is up to date ? doctrine:schema:update
|
1

I finally solved it! And the mistake I made is so severe, that I lightheartedly will give all my reputationpoints away. For I don't deserve them. This is the code of my getPassword() function in the user entity as it was:

public function getPassword() {
if(!$this->getBNPW()){
    $this->getINIPW();
} else {
    $this->getBNPW();
}
}

and this is how it should look like:

public function getPassword() {
if(!$this->getBNPW()){
    return $this->getINIPW();
} else {
    return $this->getBNPW();
}
}

I will throw it all away and work with wood or something like that.....

Comments

0

Try changing mysql_provider to default_provider; also check that your User (Benutzer) entity implements the UserInterface. Using a custom repository for loading the user doesn't hurt, but it should not be a solution to your problem.

Also, in my code, I am encoding passwords with EncoderFactory::getEncoder(User) from the service 'security.encoder_factory'. That might be where your problem lies.

1 Comment

Thank you for the answer, but it didn't work. I still have the same result.

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.