Search code examples
cakephpcakephp-2.4reset-password

Submit Fails in cakephp requested address not found, since original url had 2 params


I am currently doing a reset password of which is sending the following link to a user in an email

 http://localhost/realestateagencyadministration/users/reset/859be257b603ce278c42dca470be642a/48/

Then the user goes to the form and the controller does the following

public function reset($resetkey = null, $id = null) {

        if (!$this->User->exists($id)) {
            throw new NotFoundException(__('Invalid user'));
        }

        $result =$this->User->find('first', array('conditions' => array('User.id' => $id, 'User.resetKey' => "'" . $resetkey . "'")));

        if ($result) {
            $message = __('<b>Please check your reset link</b>');
            $this->Session->setFlash($message);
        }

        if ($this->request->is('post')) {
            if ($this->User->save()) {
                $message = __('Your password has been reset');
                $this->Session->setFlash($message);
            } else {
                $message = __('Something has gone wrong. Please try later or <b>sign up again</b>');
                $this->Session->setFlash($message);
            }
        } 
        else {
            $this->request->data = $this->User->findByIdAndResetkey( $id,  $resetkey );
            $log = $this->User->getDataSource()->getLog(false, false);
            debug($log);
        }
    }

The problem is when I submit it gives an error like so

Error: The requested address '/realestateagencyadministration/users/reset/48' was not found on this server.

How should I handle this?


Solution

  • The answer that I have found be it correct or not and if anyone has suggestions on how it should be altered do comment.

    I took skywalker's suggestion and switched around the params so that id is first.

    public function reset($id = null,$resetkey = null) {

    and changed my save code

        if ($this->request->is(array('post', 'put'))) {                    
            $data = $this->User->findById($id);
            $data['User']['password'] = $this->request->data['User']['password'];
            $data['User']['resetkey'] = '';
            if ($this->User->save($data)) {
                $message = __('Your password has been changed, try logging in with your new password.');
                $this->Session->setFlash($message);
            } else {
                $message = __('Something has gone wrong. Please try again later.');
                $this->Session->setFlash($message);
            }
        } 
    

    I know it isn't clean and if anyone knows a better way it would be gratefully appreciated.