_zendDb = $zendDb; if (null !== $tableName) { $this->setTableName($tableName); } if (null !== $identityColumn) { $this->setIdentityColumn($identityColumn); } if (null !== $credentialColumn) { $this->setCredentialColumn($credentialColumn); } if (null !== $credentialTreatment) { $this->setCredentialTreatment($credentialTreatment); } } /** * setTableName() - set the table name to be used in the select query * * @param string $tableName * @return Zend_Auth_Adapter_DbTable */ public function setTableName($tableName) { $this->_tableName = $tableName; return $this; } /** * setIdentityColumn() - set the column name to be used as the identity column * * @param string $identityColumn * @return Zend_Auth_Adapter_DbTable */ public function setIdentityColumn($identityColumn) { $this->_identityColumn = $identityColumn; return $this; } /** * setCredentialColumn() - set the column name to be used as the credential column * * @param string $credentialColumn * @return Zend_Auth_Adapter_DbTable */ public function setCredentialColumn($credentialColumn) { $this->_credentialColumn = $credentialColumn; return $this; } /** * setCredentialTreatment() - allows the developer to pass a parameterized string that is * used to transform or treat the input credential data * * In many cases, passwords and other sensitive data are encrypted, hashed, encoded, * obscured, or otherwise treated through some function or algorithm. By specifying a * parameterized treatment string with this method, a developer may apply arbitrary SQL * upon input credential data. * * Examples: * * 'PASSWORD(?)' * 'MD5(?)' * * @param string $treatment * @return Zend_Auth_Adapter_DbTable */ public function setCredentialTreatment($treatment) { $this->_credentialTreatment = $treatment; return $this; } /** * setIdentity() - set the value to be used as the identity * * @param string $value * @return Zend_Auth_Adapter_DbTable */ public function setIdentity($value) { $this->_identity = $value; return $this; } /** * setCredential() - set the credential value to be used, optionally can specify a treatment * to be used, should be supplied in parameterized form, such as 'MD5(?)' or 'PASSWORD(?)' * * @param string $credential * @return Zend_Auth_Adapter_DbTable */ public function setCredential($credential) { $this->_credential = $credential; return $this; } /** * getResultRowObject() - Returns the result row as a stdClass object * * @param string|array $returnColumns * @param string|array $omitColumns * @return stdClass */ public function getResultRowObject($returnColumns = null, $omitColumns = null) { $returnObject = new stdClass(); if (null !== $returnColumns) { $availableColumns = array_keys($this->_resultRow); foreach ( (array) $returnColumns as $returnColumn) { if (in_array($returnColumn, $availableColumns)) { $returnObject->{$returnColumn} = $this->_resultRow[$returnColumn]; } } return $returnObject; } elseif (null !== $omitColumns) { $omitColumns = (array) $omitColumns; foreach ($this->_resultRow as $resultColumn => $resultValue) { if (!in_array($resultColumn, $omitColumns)) { $returnObject->{$resultColumn} = $resultValue; } } return $returnObject; } else { foreach ($this->_resultRow as $resultColumn => $resultValue) { $returnObject->{$resultColumn} = $resultValue; } return $returnObject; } } /** * authenticate() - defined by Zend_Auth_Adapter_Interface. * * @throws Zend_Auth_Adapter_Exception if answering the authentication query is impossible * @return Zend_Auth_Result */ public function authenticate() { $exception = null; if ($this->_tableName == '') { $exception = 'A table must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.'; } elseif ($this->_identityColumn == '') { $exception = 'An identity column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.'; } elseif ($this->_credentialColumn == '') { $exception = 'A credential column must be supplied for the Zend_Auth_Adapter_DbTable authentication adapter.'; } elseif ($this->_identity == '') { $exception = 'A value for the identity was not provided prior to authentication with Zend_Auth_Adapter_DbTable.'; } elseif ($this->_credential === null) { $exception = 'A credential value was not provided prior to authentication with Zend_Auth_Adapter_DbTable.'; } if (null !== $exception) { /** * @see Zend_Auth_Adapter_Exception */ require_once 'Zend/Auth/Adapter/Exception.php'; throw new Zend_Auth_Adapter_Exception($exception); } // create result array $authResult = array( 'code' => Zend_Auth_Result::FAILURE, 'identity' => $this->_identity, 'messages' => array() ); // build credential expression if (empty($this->_credentialTreatment) || (strpos($this->_credentialTreatment, "?") === false)) { $this->_credentialTreatment = '?'; } $credentialExpression = new Zend_Db_Expr( $this->_zendDb->quoteInto( $this->_zendDb->quoteIdentifier($this->_credentialColumn) . ' = ' . $this->_credentialTreatment, $this->_credential ) . ' AS zend_auth_credential_match' ); // get select $dbSelect = $this->_zendDb->select(); $dbSelect->from($this->_tableName, array('*', $credentialExpression)) ->where($this->_zendDb->quoteIdentifier($this->_identityColumn) . ' = ?', $this->_identity); // query for the identity try { $resultIdentities = $this->_zendDb->fetchAll($dbSelect->__toString()); } catch (Exception $e) { /** * @see Zend_Auth_Adapter_Exception */ require_once 'Zend/Auth/Adapter/Exception.php'; throw new Zend_Auth_Adapter_Exception('The supplied parameters to Zend_Auth_Adapter_DbTable failed to ' . 'produce a valid sql statement, please check table and column names ' . 'for validity.'); } if (count($resultIdentities) < 1) { $authResult['code'] = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND; $authResult['messages'][] = 'A record with the supplied identity could not be found.'; return new Zend_Auth_Result($authResult['code'], $authResult['identity'], $authResult['messages']); } elseif (count($resultIdentities) > 1) { $authResult['code'] = Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS; $authResult['messages'][] = 'More than one record matches the supplied identity.'; return new Zend_Auth_Result($authResult['code'], $authResult['identity'], $authResult['messages']); } $resultIdentity = $resultIdentities[0]; if ($resultIdentity['zend_auth_credential_match'] != '1') { $authResult['code'] = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID; $authResult['messages'][] = 'Supplied credential is invalid.'; return new Zend_Auth_Result($authResult['code'], $authResult['identity'], $authResult['messages']); } unset($resultIdentity['zend_auth_credential_match']); $this->_resultRow = $resultIdentity; $authResult['code'] = Zend_Auth_Result::SUCCESS; $authResult['messages'][] = 'Authentication successful.'; return new Zend_Auth_Result($authResult['code'], $authResult['identity'], $authResult['messages']); } }