public function beforeSave(Event $event, EntityInterface $entity): bool
    {
        if (empty($this->getConfig('oldPasswordCount')) || !is_numeric($this->getConfig('oldPasswordCount'))) {
            return true;
        }

        if (!is_array($entity->last_passwords)) {
            $entity->last_passwords = [];
        }

        $lastPasswords = $entity->last_passwords;
        if (count($lastPasswords) == $this->getConfig('oldPasswordCount')) {
            array_shift($lastPasswords);
        }
        $lastPasswords[] = $entity->password;
        $entity->setAccess('last_passwords', true);
        $this->_table->patchEntity($entity, [
            'last_passwords' => $lastPasswords,
        ]);

        return true;
    }
public function beforeSave(Event $event, EntityInterface $entity): bool
    {
        if (empty($this->getConfig('oldPasswordCount')) || !is_numeric($this->getConfig('oldPasswordCount'))) {
            return true;
        }

        if (!is_array($entity->last_passwords)) {
            $entity->last_passwords = [];
        }

        $lastPasswords = $entity->last_passwords;
        if (count($lastPasswords) == $this->getConfig('oldPasswordCount')) {
            array_shift($lastPasswords);
        }
        $lastPasswords[] = $entity->password;
        $entity->setAccess('last_passwords', true);
        $this->_table->patchEntity($entity, [
            'last_passwords' => $lastPasswords,
        ]);

        return true;
    }
public function beforeSave ( Event $ event , EntityInterface $ entity ) : bool { if ( empty ( $ this -> getConfig ( 'oldPasswordCount' ) ) || ! is_numeric ( $ this -> getConfig ( 'oldPasswordCount' ) ) ) { return true ; } if ( ! is_array ( $ entity -> last_passwords ) ) { $ entity -> last_passwords = [ ] ; } $ lastPasswords = $ entity -> last_passwords ; if ( count ( $ lastPasswords ) == $ this -> getConfig ( 'oldPasswordCount' ) ) { array_shift ( $ lastPasswords ) ; } $ lastPasswords [ ] = $ entity -> password ; $ entity -> setAccess ( 'last_passwords' , true ) ; $ this -> _table -> patchEntity ( $ entity , [ 'last_passwords' => $ lastPasswords , ] ) ; return true ; }
BeforeSave Event to update last passwords

@param \Cake\Event\Event $event Event
@param \Cake\Datasource\EntityInterface $entity  Entity
@return bool
BeforeSave Event to update last passwords