0
我試圖通過這個代碼註冊新用戶後清空,我想用編碼密碼security.password_encoder堅持之前的用戶密碼財產具有價值,但我看到這個錯誤symfony3:密碼設置運行此方法
使用參數[「root」,「rooti」執行'INSERT INTO用戶(姓名,家庭,用戶名,電子郵件,角色,密碼,鹽)VALUES(?,?,?,?,?,?,?)' 」, 「根」, 「[email protected]」, 「[\」 ROLE_ADMIN \ 「]」,NULL, 「$ $ 2Y $ 13 70JDWmzFF0fuJyVCaB3/ueISm3FgWRBMLAkSJqcQouNAh3qPnzcg」]:
SQLSTATE [23000]:完整性約束中提琴重刑:1048列「密碼」不能爲空
我的方法是在這裏:
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(),
));
}
,這是我的用戶實體
<?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()
{
}
}
和我設置的安全算法bcrypt是security.yml 在這裏
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 }