views:

219

answers:

2

I am using Zend_Auth to validate user credentials and ran into an issue. I need to have a dual column identity. The the two columns are user name and customer identifier. The identityColumn setting and setIdentity() methods don't allow for this scenario. I tried to accomplish this by using the credentialTreatment setting, but when I have a duplicate user name for two or more customers, it merely calculates the zend_auth_credential_match as false for the other customers rather than filtering those users out.

Here's a sanitized example of the resulting query executed by Zend Auth:

SELECT `users`.*,
    (CASE
        WHEN `password` = 'password'
            AND active = 1
            AND customer_id = 1
            THEN 1 
        ELSE 0
        END) AS `zend_auth_credential_match`
FROM `users`
WHERE (`username` = 'username')

Is it necessary to extend the Zend_Auth module to do this? Has anyone else done it and can provide an example?

Thanks!

+2  A: 

Best way to do this would be to write your own Auth adapter. Perhaps even extending Zend_Auth_Adapter_DbTable directly.

prodigitalson
That's the road I am going down, but I wanted to make sure I wasn't missing something obvious.
Sonny
+3  A: 

I would imagine you will need to write your own subclass of Zend_Auth_Adapter_DbTable to handle this.

Something like:

class My_Auth_Adapter extends Zend_Auth_Adapter_DbTable {
  protected $_customerColumn = 'customer_id';
  protected $_customerId = false;

  public function setCustomerId($id) {
    $this->_customerId = $id;
    return $this;
  }

  public function getCustomerId() {
    return $this->_customerId!==false?$this->_customerId:'';
  }

  public function _authenticateCreateSelect() {
    $select = parent::_authenticateCreateSelect();
    $select->where($this->_zendDb->quoteIdentifier($this->_customerColumn, true)." = ?", $this->getCustomerId());
    return $select;
  }
}
gnarf
That's much simpler than what was my idea to extend the `Zend_Auth_Adapter_DbTable` to accommodate multiple identity columns. Testing now...works!
Sonny