src/Model/Behavior/UserBehavior.php
public function handleNewPasswordByOldPassword(EntityInterface $user) {
		// Don't do it for new users or the password will end up empty
		if ($user->isNew()) {
			return;
		}

		$oldPassword = $user->get($this->_field('oldPassword'));

		if (empty($oldPassword) || $user->getErrors()) {
			$user->unsetProperty($this->_field('password'));
			$user->unsetProperty($this->_field('passwordCheck'));

			return;
		}

		$newPassword = $this->hashPassword($user->get($this->_field('password')));
		$user->set($this->_field('password'), $newPassword, ['guard' => false]);
	}
public function handleNewPasswordByOldPassword(EntityInterface $user) {
		// Don't do it for new users or the password will end up empty
		if ($user->isNew()) {
			return;
		}

		$oldPassword = $user->get($this->_field('oldPassword'));

		if (empty($oldPassword) || $user->getErrors()) {
			$user->unsetProperty($this->_field('password'));
			$user->unsetProperty($this->_field('passwordCheck'));

			return;
		}

		$newPassword = $this->hashPassword($user->get($this->_field('password')));
		$user->set($this->_field('password'), $newPassword, ['guard' => false]);
	}
public function handleNewPasswordByOldPassword ( EntityInterface $ user ) { // Don't do it for new users or the password will end up empty if ( $ user -> isNew ( ) ) { return ; } $ oldPassword = $ user -> get ( $ this -> _field ( 'oldPassword' ) ) ; if ( empty ( $ oldPassword ) || $ user -> getErrors ( ) ) { $ user -> unsetProperty ( $ this -> _field ( 'password' ) ) ; $ user -> unsetProperty ( $ this -> _field ( 'passwordCheck' ) ) ; return ; } $ newPassword = $ this -> hashPassword ( $ user -> get ( $ this -> _field ( 'password' ) ) ) ; $ user -> set ( $ this -> _field ( 'password' ) , $ newPassword , [ 'guard' => false ] ) ; }
Handles the hashing of the password after the old password of the user
was verified and the new password was validated as well.

Call this in your models beforeSave() or wherever else you want.

This method will unset by default the `password` and `confirm_password`
fields if no `old_password` was provided or the entity has validation
errors.

@param \Cake\Datasource\EntityInterface $user User Entity
@return void
Handles the hashing of the password after the old password of the user was verified and the new password was validated as well .