Empty Password Field, disable writing to database

Empty Password Field, disable writing to database

th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1
edited February 2020 in Editor

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

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    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.

    ->setFormatter ( function ( $editor, $action, $val ) {
                        if( $action === Editor::ACTION_CREATE ) {
                            return crypt( $val, '$somerandomsalthere$' );
                        } elseif( ( $action === Editor::ACTION_EDIT ) && ( Field('.password').val() !==null ) ) {
                            return crypt( $val, '$somerandomsalthere$' );
                        } else {
                            Field('.password').set(false);
                            return;
                        }
                    }),
    ...
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    edited February 2020
  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    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.

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    Ok. I'm finally back to this again. So I am using the following now...

    ->on( 'preEdit', function ( $e, $id, $values ) {
                if ( $values['users.password'] === '' ) {
                    $e->field( 'users.password' )->set( false );
                }
            } )
    

    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.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    $values['users.password']

    Should be:

    $values['users']['password']
    

    Allan

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1
    edited August 2020

    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.

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    Found it,

    ->get( false )
    
This discussion has been closed.