Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible bug in authenticateAction within User Controller #468

Closed
teseo opened this issue Jun 17, 2014 · 2 comments
Closed

Possible bug in authenticateAction within User Controller #468

teseo opened this issue Jun 17, 2014 · 2 comments

Comments

@teseo
Copy link

teseo commented Jun 17, 2014

I am trying to check whether a user is logged ir not by using $this->zfcUserAuthentication()->hasIdentity() which works fine.

Once I verify the user is not logged in, following this suggestion in Stack overflow http://stackoverflow.com/a/14033746, I have this piece of code:

if (!$this->zfcUserAuthentication()->hasIdentity()) {

    // Build the redirect URL using the route to which we want
    // the user returned.
    $redirect = $this->url()->fromRoute('yourRoute', array(
        'param' => 1234
    ));

    // Set the redirect URL in the request so that ZfcUser can
    // pick it up. This is the key.
    $this->getRequest()->getQuery()->set('redirect', $redirect);

    // Use ZfcUser's login action rather than its authentication
    // action.
    return $this->forward()->dispatch('zfcuser', array(
        'action' => 'login'
    ));
}

That will allow you to access redirect url generated in the login form to be used in a hidden variable with name "redirect" and value like this /yourRoute/param/1234/. Once form is submitted, and you are into the authenticateAction method in UserController, the value will be collected as string in the $redirect variable. See the method :

public function authenticateAction()
    {
        if ($this->zfcUserAuthentication()->hasIdentity()) {
            return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute());
        }

        $adapter = $this->zfcUserAuthentication()->getAuthAdapter();
        $redirect = $this->params()->fromPost('redirect', $this->params()->fromQuery('redirect', false));

        $result = $adapter->prepareForAuthentication($this->getRequest());

        // Return early if an adapter returned a response
        if ($result instanceof Response) {
            return $result;
        }

        $auth = $this->zfcUserAuthentication()->getAuthService()->authenticate($adapter);

        if (!$auth->isValid()) {
            $this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage($this->failedLoginMessage);
            $adapter->resetAdapters();
            return $this->redirect()->toUrl(
                $this->url()->fromRoute(static::ROUTE_LOGIN) .
                ($redirect ? '?redirect='. rawurlencode($redirect) : '')
            );
        }

        if ($this->getOptions()->getUseRedirectParameterIfPresent() && $redirect) {
            return $this->redirect()->toUrl($redirect);
        }

        return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute());
    }

Even though toRoute accepts params, options, etc. Those are not available at this point. $redirect is a string that is dynamically generated.

        if ($this->getOptions()->getUseRedirectParameterIfPresent() && $redirect) {
            return $this->redirect()->toRoute($redirect);
        }

Is there any reason this code is using

 return $this->redirect()->toRoute($redirect);

Instead of using toURL which works for a dynamic generated and get properly redirected?

 return $this->redirect()->toURL($redirect);

@teseo
Copy link
Author

teseo commented Jul 2, 2014

Please take a look at #480

@Danielss89
Copy link
Member

Version 1.2.0 allows you to fix this. See #480

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants