How to capture a password and save to users tables

I have a use case I’d to share. I originally wanted to post here seeking assistance. But I figured out the solution. I hope this helps you. If you have an alternate solution, please share.

As a system admin of my web app, I want the ability to reset users passwords. My goal is to allow for a new password on the user Edit Form. No need for the Add Form.
If a password is entered, then before update, encrypt. If no password is entered in the field then the password should NOT be updated.

I had this working in the GC-CE (see bellow).

The following his how I configured this in GC-EE.

I’ve added these config statements in my new GC-EE controller:

		$crud->callbackEditField('password', function ($fieldValue, $primaryKeyValue, $rowData) {
			return '<input class="form-control" name="password" value=""  />';
		$crud->callbackEditForm(function ($data) {
			$data['password'] = '';
			return $data;
		$crud->displayAs('password', 'New Password');

The first command sets up a new field that has a value of “”
The next command adds it to the EditForm
The third changes the label.
The result is the EditForm displays a blank field labeled “New Password”.

This works. I’m capturing a new password.

Then I have this.

		$crud->callbackBeforeUpdate(function ($stateParameters) use ($data) {
			if ($stateParameters->data['password'] <> '') {
				$stateParameters->data['password'] = password_hash($stateParameters->data['password'], PASSWORD_DEFAULT);

			$stateParameters->data['update_dt'] = date('Y-m-d H:i:s');
			return $stateParameters;

If the user enters a password then encrypt it and move on. If the value is “” then the variable is unset.

This works as expected. If a new password is entered on the EditForm then it gets encrypted and saved to the DB. All good.

If the user does NOT enter in a password, the value of password is “” and then the variable is unset. This prevents any value from updating in the DB.

The way I did this in GC-CE is use a callback_before_update and then check for the post array:

function encrypt_password_callback($post_array, $primary_key) {
	//Encrypt password only if is not empty. Else unset 
			$post_array['password'] = sha1($post_array['password']);

	return $post_array;

You need to addthis config, as well (GC-CE):


function set_password_input_to_empty()
	return "<input type='password' name='password' value='' />";

If you have an alternate solution, I’d like you to post it.

Good luck and all the thanks go to Johnny. He does respond to email inquiries and, as you see here, does read and respond to forum posts.