I don't see a reason to extend the class into the auth object, just to do
the same thing the original object does. But, I'm yet to have a complete cup
of "wake-up juice" so I could just be not reading it right.

Here is a dirty-fast auth test I made when I was trying to figure out how
the auth worked for my stuff. I'm guessing you should be able to change the
db connection and the table details for the auth adapter and it will work.

Maybe it will help some. 

Terre

--------------------

<?
/*
 * 
 * -------------------------------------------
 * id   username        password
 * -------------------------------------------
 * 1    tester1         098f6bcd4621d373cade4e832627b4f6
 * 
 * password is md5 of sting test
 */

// zend path is in include path

// launch auto loader
// quicker for testing
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();

// create a db connection 
$db = (create db connection) Zend_Db::factory 

// get form values after validation and blah blah
$username = 'tester1';
$password = 'tests';
 
// create/get auth object
$auth = Zend_Auth::getInstance();

// create auth adapter
$authAdapter = new Zend_Auth_Adapter_DbTable($db);
                                
// set up auth adapter details
$authAdapter->setTableName('cms_users');
$authAdapter->setIdentityColumn('username');
$authAdapter->setCredentialColumn('password');
$authAdapter->setCredentialTreatment('md5(?)');

// Set the input credential values to authenticate against
$authAdapter->setIdentity($username);
$authAdapter->setCredential($password);

// try and authenticate the user
$result = $auth->authenticate($authAdapter); 

if ($result->isValid()) {                       
        echo "-- success <hr>"; 
} else {
        echo "-- failed <hr>";
        // why failed?
        foreach ($result->getMessages() as $message) {
        echo "$message<br>";
    }
}

// what files is loadeder including
echo '<pre>';
print_r(get_included_files());
echo '</pre>';

?>

--------------------


-----Original Message-----
From: Matthew Ishii [mailto:[EMAIL PROTECTED] 
Sent: Thursday, July 24, 2008 11:46 PM
To: fw-general@lists.zend.com
Subject: [fw-general] Combining Zend_Auth with Zend_Auth_Adapter

I am at wits end with this, where most of the examples could be instructive,
they use phrases such as:

"Other details, such as how the authentication service is queried, have been
omitted for brevity"

Which these details are precisely what is ambiguous (at least to me).

I have a login form which takes username and password and passes them to the
login action called processAction.  Within processAction, the request is
validated for POST and variables etc etc.  Then comes the actual
authentication to check the database and verify the 'identity'
of the user.

The following code is giving me 'fatal error Call to a member function
isValid() on a non-object':

      // Get authentication adapter to check input variables
      $adapter = $this->getAuthAdapter($form->getValues());
      $auth = Zend_Auth::getInstance();
      $result = $auth->authenticate($adapter);

Then later isValid is called on $result like so: $result->isValid() and
blamo I get the error.

Now .. getAuthAdapter instantiates the following class:

class MyAuthAdapter implements Zend_Auth_Adapter_Interface {
    public function __construct($username, $password)
    {
        $this->username = $username;
        $this->password = $password;
    }

    public function authenticate()
    {
      $db_config = array('host' => '127.0.0.1', 'username' =>
'ioforgec_zend', 'password' => 'Pa55w0rD', 'dbname' => 'ioforgec_iofdb');

      try
      {
        $db = Zend_Db::factory('Pdo_Mysql', $db_config);
        $db->getConnection();
      } catch (Zend_Db_Adapter_Exception $e) {
        print "// perhaps a failed login credential, or perhaps the RDBMS is
not running\n";
      } catch (Zend_Exception $e) {
        print "// perhaps factory() failed to load the specified Adapter
class\n";
      }

      // Attempt to use fetchAll() - The first argument to this method is a
string containing a SELECT statement
      // The second argument to fetchAll() is an array of values to
substitute for parameter placeholders in the SQL statement
      $sql = "SELECT * FROM users WHERE username = ? AND password = ?";
      $result = $db->fetchAll($sql, array($this->username,
$this->password));

      // Return Zend_Auth_Result Object to determine success or failure of
authenticate attempt
      if ($result[0]['auth']) { return Zend_Auth_Result::SUCCESS; }

      return Zend_Auth_Result::FAILURE;

    }


Ok .. so I used the Zend_Auth_Result because as I read it turns out thats
what the isValid() function is expecting.  Other places I read I need to
call the Zend_Auth method Authenticate without any changes, while others say
I need to instantiate a Zend_Auth_Adapter and over write the authenticate
call with my own DB selects, etc.

What is going on here?  How can I get so confused?  Im following directions
(Or so I thought) from a tutorial on the zendframework
site:

http://weierophinney.net/matthew/archives/165-Login-and-Authentication-with-
Zend-Framework.html

Could I get a little re-alignment?  Thank you ;0)

Reply via email to