我使用标准的symfony安全性作为我的管理员密码:
security:
providers:
in_memory:
memory:
users:
admin:
password: $2y$12$.QD1HlEVmeupiIIM3d601urQxd.WsgJyfZ0nJjVAyEn2qQOGpjIHi
roles: 'ROLE_ADMIN'
encoders:
Symfony\Component\Security\Core\User\User:
algorithm: bcrypt
cost: 12
现在我想实现一个更改密码的方法。 通过一些调整,我设法从this问题得到了答案,以便用于密码检查方面。
这是我的密码类:
我现在被迫使用public
类型,因为我一直得到无法确定属性的访问类型错误。 附带问题,为什么会发生这种情况?我没有取消注册安装者和吸气剂
<?php
namespace AppBundle\Security;
use Symfony\Component\Security\Core\Validator\Constraints as SecurityAssert;
use Symfony\Component\Validator\Constraints as Assert;
class ChangePassword
{
/**
* @SecurityAssert\UserPassword(
* message = "Niepoprawne Haslo"
* )
*/
public $oldPassword;
/**
* @Assert\Length(
* min = 6,
* minMessage = "Hasło powinno mieć min. 6 znaków"
* )
*/
public $newPassword;
// /**
// * Get oldPassword
// *
// * @return string
// */
// public function getOldPassword()
// {
// return $this->oldPassword;
// }
// *
// * Get newPassword
// *
// * @return string
// public function getNewPassword()
// {
// return $this->newPassword;
// }
// public function setNewPassword($newPassword)
// {
// $this->newPassword = $newPassword;
// return $this;
// }
}
我的类型:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use AppBundle\Security\ChangePassword;
class ChangePasswordType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('oldPassword', PasswordType::class, array(
'label' => 'Stare Hasło'))
->add('newPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'invalid_message' => 'Hasła nie pasują do siebie',
'required' => true,
'first_options' => array('label' => 'Nowe Hasło'),
'second_options' => array('label' => 'Wprowadź ponownie'),
))
->add('save', SubmitType::class, array(
'label' => 'Zmień Hasło',
'attr' => array(
'class' => 'btn btn-danger'
)));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Security\ChangePassword',
));
}
public function getName()
{
return 'change_passwd';
}
}
最后是控制器:
public function adminChangePasswdAction(Request $request)
{
$changePasswordModel = new ChangePassword();
$form = $this->createForm(ChangePasswordType::class, $changePasswordModel);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$user = $this->getUser();
$plainPassword = $form->getData()->newPassword;
$encoder = $this->container->get('security.password_encoder');
$encoded = $encoder->encodePassword($user, $plainPassword);
$user->setPassword($encoded);
//return $this->redirect($this->generateUrl('admin-panel'));
}
return $this->render('admin/changepasswd.html.twig', array(
'form' => $form->createView(),
));
}
但是你很容易猜到$user->setPassword($encoded);
部分无效,因为用户类没有setPassword方法。
设置密码的正确方法是什么?它甚至可能吗?