MD5 encrypted password cannot be saved
MD5 encrypted password cannot be saved
->fields(
Field::inst( 'id' )->set(false) //ID是自新增的,加上set(false才能新增后自动刷新)
->validator( Validate::notEmpty( ValidateOptions::inst()
->message( 'id is required' )
) ),
Field::inst( 'UserName' )
->validator( Validate::notEmpty( ValidateOptions::inst()
->message( '用户名不能为空' )
) )
->validator( Validate::unique( ValidateOptions::inst()
->message( '用户名重复' )
) ),
Field::inst( 'PassWord' ),
Field::inst( 'removed_date' )
->setFormatter( Format::ifEmpty( null ) )
)
->on( 'preCreate', function ( $editor, &$values ) {
$editor
->field('PassWord')
->setValue(md5($values['PassWord']));
} )
->on( 'preEdit', function ( $editor, $id, &$values ) {
if ($values["PassWord"]==="")
{
$editor->field('PassWord')->set(false);
}
} )
->where( 'removed_date', null )
->on( 'preRemove', function () {
// Disallow all delete actions since data cannot be removed completely
return false;
} )
->debug(true)
->process( $_POST )
->json();
I pre submit the MD5 password field according to the example. If the password is not modified, it can not be saved to the database.It's ok, However, If I modify the password, it cannot be MD5 encrypted when saving, and the saved value is still unencrypted, How does this need to be modified? Thaks
This question has an accepted answers - jump to answer
Answers
It looks like you probably need to add
->get(false)
to yourPassWord
field, since you wouldn't want to read the hashes into your DataTable. You should also useField->setValue()
in anelse
statement for yourpreEdit
so if a value is submitted then it will get hashed.As a slight aside, md5 is a hashing algorithm, not encryption. It is not sure as md5 hashes can be easily brute forced on modern hardware. I would very strongly suggest you use PHP's
password_hash()
function rather thanmd5()
. md5 is not safe for password storage.Allan
As above, however, it will cause the value of MD5 to be repeated by MD5
Did you add
->get(false)
as well so the hash is not read? Otherwise, as you say, the hash would then be hashed again!Allan
After I add it now, it works ,Now it's OK, thank allan,