ZF-Commons / ZfcUser

A generic user registration and authentication module for ZF2. Supports Zend\Db and Doctrine2. (Formerly EdpUser)
BSD 3-Clause "New" or "Revised" License
497 stars 343 forks source link

Session is started by this module even in CLI mode #518

Open ojhaujjwal opened 10 years ago

ojhaujjwal commented 10 years ago

ZfcUser\Authentication\Storage\Db starts session when isEmpty method is called n session is not started. The problem appears when the current request is not HttpRequest (i.e. CliRequest). Is this an intended behaviour?

ojhaujjwal commented 10 years ago

ping @Danielss89

Ocramius commented 9 years ago

@ojhaujjwal what happens in that case? failure? error?

ojhaujjwal commented 9 years ago

Generally, there is no error but I don't see any point in starting session in CLI mode. We may check if request is HttpRequest or CliRequest.

    public function isEmpty()
    {
        $request = $this->getServiceManager()->get('Request');
        if (!$request instanceof HttpRequest) {
            return true;
        }

        // previously the session was started here in CLI
        if ($this->getStorage()->isEmpty()) {
            return true;
        }
        $identity = $this->read();
        if ($identity === null) {
            $this->clear();
            return true;
        }

        return false;
    }
Ocramius commented 9 years ago

@ojhaujjwal yes, but I'm wondering if this shouldn't simply go back to Zend\Session instead...

ojhaujjwal commented 9 years ago

@Ocramius Yes. I don't know how but I think it will more easier if this is handled by Zend\Session out-of-the-box.

adamlundrigan commented 9 years ago

@Danielss89 this is essentially a duplicate of #513

I've run into this issue myself with ZfcUser 1.x and I worked around it by creating a separate bootstrap file to use when running console commands with this addition above $app->run():

use ZfcUser\Authentication\Adapter\AdapterChain;
use ZfcUser\Authentication\Adapter\AdapterChainEvent;
use Zend\Authentication\Result;

$authService = $app->getServiceManager()->get('zfcuser_auth_service');
$authAdapterChain = new AdapterChain();
$authAdapterChain->getEventManager()->attach('authenticate', function(AdapterChainEvent $e) {
    $e->setCode(Result::SUCCESS)
      ->setIdentity(1);  // This forces the auth'd user to our consoleadmin user
});
$authAdapterChain->prepareForAuthentication(new \Zend\Console\Request());
$authService->authenticate($authAdapterChain);