Using Editor with Password fields and Salt
Using Editor with Password fields and Salt
I am using Editor to add new users to a website. I have a hidden salt field that holds a random string. I obviously also have a password field. The password field is a required field, of course.
I have everything working properly when a new user is added. The salt is added to the db and the password is encrypted using a function in setFormatter().
If a user row is edited, I only want the password to use setFormatter() if the password field is changed. I also want the salt to be added if the password is updated, otherwise, don't do anything to the password and salt fields in the db.
The salt is a hidden field in the editor. Right now my salt default value is created by using this code:
$salt = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 20);
Here's what I have now:
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'users', 'id' )
->fields(
Field::inst( 'first_name' )
->validator( 'Validate::notEmpty' ),
Field::inst( 'middle_name' ),
Field::inst( 'last_name' )
->validator( 'Validate::notEmpty' ),
Field::inst( 'username' )
->validator( 'Validate::notEmpty' )
->validator( 'Validate::unique' ),
Field::inst( 'salt' )
->validator( 'Validate::notEmpty' ),
Field::inst( 'password' )
->validator( 'Validate::notEmpty' )
->setFormatter( function ( $val ) {
return hash('sha256', $val . $_POST['data'][0]['salt']);
})
)
->where( $key = "company_id", $value = $company_id, $op = "=" )
->process( $_POST )
->json();
One separate issue I have is on this line:
->setFormatter( function ( $val ) {
return hash('sha256', $val . $_POST['data'][0]['salt']);
})
I am trying to use the hidden salt value to encrypt the password. This code works on inserting a new user, but when I try to edit it, I get a system error:
<b>Notice</b>: Undefined offset: 0
This question has an accepted answers - jump to answer
Answers
Have a look at Editor's server-side events. Specifically, this blog post introducing them describes exactly what you are looking for.
Allan
Thanks, I am making some progress.
Can you answer a question for me?
How can I make my password field required ONLY if a new user is being created? Because I'm returning a blank password field when editing, I don't want that field required. If it is, then the user will have a different password every time something else is edited.
I did it! I think this is right:
That looks good. You could also use server-side validation for it (which personally I would recommend since you can't trust data from the client). The
preCreate
andpreEdit
server-side events can be used to alter how the data is set or not.Allan
Thanks!
Thanks guys, this post helped me a lot!
I had a little trouble figuring it out but eventually I got it working. It would be great to have a more detailed description of the structure of the parameters that are passed into
"->on('preCreate', function ( $editor, $values )" though.
In this example I hash the answers to security questions. The answers cannot be edited. You can only create or delete a question-and-answer pairs which probably makes the solution a bit easier. Here it is with "salt" (saved in db) and "PEPPER" (constant)" plus the constant but individual user id: