myworkaccount
myworkaccount

Reputation: 139

PHP Zend Authentication - How can I tell Zend the user is authenticated

I need to tell Zend that a user is authenticated on a very specific occasion, but I don't want to build an Auth Adapter for this purpose.

I want something like this:

if ($user == 'sam') {

    // this user is valid, tell Zend to authenticate
    Zend_Auth::authenticate(true);

}

In other words, I don't want to delegate the task of determining whether or not a user is valid to some Auth Adapter.I want to tell Zend "Hey, this guy is with me, so set the authentication cookie and let him use my site".

Is there a way to do this?

Upvotes: 0

Views: 221

Answers (2)

charles
charles

Reputation: 307

http://framework.zend.com/manual/1.12/en/zend.auth.adapter.dbtable.html

check the last code under Advanced Usage By Example, code is as follows

    $registry       =   Zend_Registry::getInstance();
    $DB             =   $registry['DB'];
    $authAdapter    =   new Zend_Auth_Adapter_DbTable($DB,'usertable','username','password');

    $authAdapter->setIdentity($request->getParam('username'));
    $authAdapter->setCredential($request->getParam('password'));

    $select         =   $authAdapter->getDbSelect();
    $select->where('`active` = 1');
    $result         =   $authAdapter->authenticate();

    if($result->isValid()){
            //set user proper to zend instance
            $this->_redirect('/index');
    }
    else
    {
       //logout or redirect to same page
    }

Upvotes: 0

Michael
Michael

Reputation: 1267

You can do that by writing to to Zend_Auth storage:

$userData = (object)array('username' => 'Johny23');
Zend_Auth::getInstance()->getStorage()->write($userData);

When you do that, Zend automaticly create new Zend_Auth_Storage_Session object which handle the session.

And then, you can check with:

$auth = Zend_Auth::getInstance();
if($auth->hasIdentity()){
    echo 'welcome back, ' . $auth->getIdentity()->username;
}

Upvotes: 1

Related Questions