user2003356
user2003356

Reputation: 455

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.

Upvotes: 6

Views: 3458

Answers (2)

marc
marc

Reputation: 11

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

Upvotes: 1

Developer
Developer

Reputation: 26173

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;
}

Upvotes: 7

Related Questions