Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF2 Authentication

i am developing an application using ZF2. I have done the user authentication with username & password. But, i would like to check an additional column(example: status) in authentication.

I have done the following codes.

public function authenticate()
{       
    $this->authAdapter = new AuthAdapter($this->dbAdapter,
            'usertable',
            'username',
            'password'
    );  

    $this->authAdapter->setIdentity($this->username)
                ->setCredential($this->password)
                ->setCredentialTreatment('MD5(?)');
    $result = $this->authAdapter->authenticate();
    return $result;
}

How can i check the column 'status' in authentication? Note: status value should be 1. Thanks.

like image 746
user2003356 Avatar asked Jan 23 '13 09:01

user2003356


2 Answers

When I was building my authentication using zf2 and doctrine, I have created authorization plugin and customized this adapter for passing extra column for authentication. You probably need to go on similar directions.

$adapter = new AuthAdapter($db,
                           'users',
                           'username',
                           'password',
                           'MD5(?)'
                           );

// get select object (by reference)
$select = $adapter->getDbSelect();
$select->where('active = "TRUE"');

// authenticate, this ensures that users.active = TRUE
$adapter->authenticate();

Reference

After changes your code should look something like this.

public function authenticate()
{       
    $this->authAdapter = new AuthAdapter($this->dbAdapter,
            'usertable',
            'username',
            'password'
    );  

    $select = $this->authAdapter->getDbSelect();
    $select->where('status= "1"');
    $this->authAdapter->setIdentity($this->username)
                ->setCredential($this->password)
                ->setCredentialTreatment('MD5(?)');
    $result = $this->authAdapter->authenticate();
    return $result;
}
like image 148
Developer Avatar answered Sep 26 '22 15:09

Developer


ZF2 provides a another way to handle additional checks using other columns than the ones foreseen for identity and credential thanks to the method getResultRowObject. All columns of usertable in your example are available as properties of the object returned by getResultRowObject(). So you could expand your code with this :

if ($result->isValid()) {
    $identityRowObject = $this->authAdapter->getResultRowObject();
    $status = $identityRowObject->status;
    // do whatever complex checking you need with $status...
}

Regards, Marc

like image 38
marc Avatar answered Sep 22 '22 15:09

marc