1

I have a Drupal site and a Zend application. The main thing is the Drupal site, where the users are stored & everything.

I want my users to be automatically logged in to the Zend app when they log in on Drupal. The problem is that Drupal changes the session cookie to SESS* where * is some random (EDIT: not random, but based on protocol and domain) string.

Is there any way I can tell Zend to use this cookie as a session identifier and to log the user automatically?

Eduard Luca
  • 6,514
  • 16
  • 85
  • 137
  • Do the Drupal site and the Zend app share the same URL (including subdomain and port)? – Florent Aug 07 '12 at 14:42
  • Same port yes, same domain yes, different subdomain tho. The cookie is set on the entire domain though, so it's accessible from both sites. – Eduard Luca Aug 07 '12 at 14:43
  • You can write your own authentication adapter (extending `Zend_Auth_Adapter_Abstract`). Read the cookie and process the authentication. – Florent Aug 07 '12 at 14:46
  • I don't see any class by this name. Looked in Zend/Auth/Adapter/Abstract.php but there's no such file. Note: using Zend 1.11 – Eduard Luca Aug 07 '12 at 14:49
  • My bad. You have to implement `Zend_Auth_Adapter_Interface`. – Florent Aug 07 '12 at 14:51
  • I see. Very hard process for me, but I guess that would work. Thanks. If you post an answer, I'd be happy to accept it. – Eduard Luca Aug 07 '12 at 15:35

1 Answers1

1

You have to write your own authentication adapter:

class YourApp_Auth_Adapter_DrupalBridge implements Zend_Auth_Adapter_Interface
{
    /**
     * @return Zend_Auth_Result
     */
    public function authenticate()
    {
        // Check if the Drupal session is set by reading the cookie.
        // ...

        // Read the current user's login into $username.
        // ...

        // Create the authentication result object.

        // Failure
        if (null === $username) {
            return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, null);
        }

        // Success
        return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $username);
    }
}

Then process your authentication:

$adapter = new YourApp_Auth_Adapter_DrupalBridge();
$result = Zend_Auth::getInstance()->authenticate($adapter);

if ($result->isValid()) {
    // User is logged in
}
Florent
  • 12,310
  • 10
  • 49
  • 58