Mosi Isp
Mosi Isp

Reputation: 11

symfony3 : password set to empty after running this method

i try register new user by this code, and i want encode password with security.password_encoder before persist password property of user has value but i see this error

An exception occurred while executing 'INSERT INTO user (name, family, username, email, roles, password, salt) VALUES (?, ?, ?, ?, ?, ?, ?)' with params ["root", "rooti", "root", "[email protected]", "[\"ROLE_ADMIN\"]", null, "$2y$13$70JDWmzFF0fuJyVCaB3/ueISm3FgWRBMLAkSJqcQouNAh3qPnzcg."]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'password' cannot be null

my method is here :

public function newAction(Request $request)
{
    $user = new User();
    $form = $this->createForm('AppBundle\Form\UserType', $user);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $encoder = $this->get("security.password_encoder");
        $encoded = $encoder->encodePassword($user, $user->getPlainPassword());
        $user->setPassword($encoded);

        $user->setRoles(array('ROLE_ADMIN'));
        $em = $this->getDoctrine()->getManager();
        $em->persist($user);
        $em->flush();

        return $this->redirectToRoute('user_show', array('id' => $user->getId()));
    }

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

and this is my User entity

<?php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
 */
class User implements UserInterface
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;

/**
 * @var string
 *
 * @ORM\Column(name="family", type="string", length=255)
 */
private $family;

/**
 * @var string
 *
 * @ORM\Column(name="username", type="string", length=255, unique=true)
 */
private $username;

/**
 * @var string
 *
 * @ORM\Column(name="email", type="string", length=255)
 */
private $email;

/**
 * @var array
 *
 * @ORM\Column(name="roles", type="json_array")
 */
private $roles;

/**
 * @var string
 *
 * @ORM\Column(name="password", type="string", length=255)
 */
private $salt;

/**
 * @var string
 *
 * @ORM\Column(name="salt", type="string", length=10)
 */
private $password;

/**
 * @var string
 * @Assert\NotBlank()
 * @Assert\Length(max=4096)
 */
private $plainPassword;


/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}

/**
 * Set name
 *
 * @param string $name
 *
 * @return User
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string
 */
public function getName()
{
    return $this->name;
}

/**
 * Set family
 *
 * @param string $family
 *
 * @return User
 */
public function setFamily($family)
{
    $this->family = $family;

    return $this;
}

/**
 * Get family
 *
 * @return string
 */
public function getFamily()
{
    return $this->family;
}

/**
 * Set username
 *
 * @param string $username
 *
 * @return User
 */
public function setUsername($username)
{
    $this->username = $username;

    return $this;
}

/**
 * Get username
 *
 * @return string
 */
public function getUsername()
{
    return $this->username;
}

/**
 * Set email
 *
 * @param string $email
 *
 * @return User
 */
public function setEmail($email)
{
    $this->email = $email;

    return $this;
}

/**
 * Get email
 *
 * @return string
 */
public function getEmail()
{
    return $this->email;
}

/**
 * Set roles
 *
 * @param array $roles
 *
 * @return User
 */
public function setRoles($roles)
{
    $this->roles = $roles;

    return $this;
}

public function getPlainPassword()
{
    return $this->plainPassword;
}

public function setPlainPassword($password)
{
    $this->plainPassword = $password;
}
/**
 * Get roles
 *
 * @return array
 */
public function getRoles()
{
    $roles = $this->roles;

    // guarantees that a user always has at least one role for security
    if (empty($roles)) {
        $roles[] = 'ROLE_USER';
    }

    return array_unique($roles);
}

/**
 * Set password
 *
 * @param string $password
 *
 * @return User
 */
public function setPassword($password)
{
    $this->password = $password;

    return $this;
}

/**
 * Get password
 *
 * @return string
 */
public function getPassword()
{
    return $this->password;
}

/**
 * Set salt
 *
 * @param string $salt
 *
 * @return User
 */
public function setSalt($salt)
{
    $this->salt = $salt;

    return $this;
}

/**
 * Get salt
 *
 * @return string
 */
public function getSalt()
{
    return;
}

public function eraseCredentials()
{
}
}

and i set security algorithm to bcrypt is security.yml is here

security:
encoders:
    AppBundle\Entity\User: bcrypt
# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
    database_users:
        entity: { class: AppBundle:User, property: username}

firewalls:
    secured_area:
        pattern: ^/
        anonymous: true
        form_login:
            login_path: login
            check_path: login
            csrf_token_generator: security.csrf.token_manager
        logout:
            path: security_logout
            target: homepage
access_control:
    - { path: ^/admin, roles: ROLE_ADMIN }

Upvotes: 0

Views: 77

Answers (1)

Paul
Paul

Reputation: 141827

Your column names are mixed up here:

/**
 * @var string
 *
 * @ORM\Column(name="password", type="string", length=255)
 */
private $salt;

/**
 * @var string
 *
 * @ORM\Column(name="salt", type="string", length=10)
 */
private $password;

You're storing salt in a column called password (which must be set in the database to not allow NULL although that rule is not present in the annotation) and password in a column called salt. Just flip them around:

/**
 * @var string
 *
 * @ORM\Column(name="password", type="string", length=255)
 */
private $password;

/**
 * @var string
 *
 * @ORM\Column(name="salt", type="string", length=10)
 */
private $salt;

Upvotes: 1

Related Questions