Empty Password Field, disable writing to database
Empty Password Field, disable writing to database
I have a password field that when a new user is created I want to write the password to the database. That works fine and encrypts the password using the following.
$out=Editor::inst( $db, 'users' )
->field(
Field::inst( 'password' )
->get( false ) // never read from the db
->setFormatter ( function ( $val, $data ) {
return crypt( $val, '$somerandomsalthere$' );
}),
...
However if you edit an existing users information and do not specify a password a null is encrypted and the user's password is changed. I don't want to encrypt a blank/null password only a change such as a password reset.
I have been trying to figure this out and only have gotten this far unsuccessfully...
$out=Editor::inst( $db, 'users' )
->field(
Field::inst( 'password' )
->get( false ) // never read from the db
->validator( function ( $editor, $action, $data, $val ) {
if ( $action === Editor::ACTION_CREATE ) {
->setFormatter ( function ( $val, $data ) {
return crypt( $val, '$somerandomsalthere$' );
})
} else if ( $action === Editor::ACTION_EDIT ) {
Field( 'users' ).set(false);
return;
}
} ),
...
It doesn't like the ->setFormatter. Evidently it doesn't belong there. How else can I accomplish this?
This question has an accepted answers - jump to answer
Answers
Tried this also but just not sure how to read the value of the field and check it for null and then disable the setting of the value in the database for this password field.
Use the
preEdit
server-side event for this.There is an example in this blog post.
Allan
Thank you Allen. I didn't think of using preEdit although I am using preCreate to check password length and if the password and confirm password matches. I'll give it a shot and get back to you.
Ok. I'm finally back to this again. So I am using the following now...
and I get the following error,
Notice: Undefined index: users.password in /development/FCMS/users-con.php on line 124
{"data":[{"DT_RowId":"row_4","users":{"id":"4","user_id":"654322","first_name":"RO","last_name":"User","full_name":"","location":"134","user_email":"ro_user@example.com","password":"","user_telephone":"5551200","permissions":"2","status":"1","last_verified":"2020-02-25"},"lk_locations":{"location":"Academy"},"lk_user_permissions":{"permissions":"ro_user"},"lk_user_status":{"status":"Active"}}]}
Any help would be greatly appreciated.
Should be:
Allan
Thank you, thank you, thank you Allan. It is working now and the encryption is working as well. Now I just need to go find that code that tells it not to read the password value from the database and populate the text field.
Found it,