Zend Framework

Sample code incorrect in authentication tutorial

Details

  • Type: Docs:  Problem Docs: Problem
  • Status: Resolved Resolved
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.10.0
  • Fix Version/s: 1.11.6
  • Component/s: Zend_Auth
  • Labels:
    None
  • Language:
    English

Description

Page: http://framework.zend.com/manual/en/learning.multiuser.authentication.html

In the script for authController, lines 8-10 are currently:

08 $loginForm = new Default_Form_Auth_Login($_POST);
09
10 if ($loginForm->isValid()) {

It should probably say

08 $loginForm = new Default_Form_Auth_Login();
09
10 if ($loginForm->isValid($_POST)) {

Also, the auth/login.phtml refers to $this->form when it should probably refer to $this->loginForm

Activity

Hide
fisharebest added a comment -

Also, line 23 uses $auth, which is not defined anywhere.

Also, (possibly not a bug, just me being a bumbling novice!), $this->_getParam('db') returns null, rather than a DbAdapter object.

I'm attaching this controller/action to the quickstart tutorial code (which seems to use the db adapter without doing any initialisation).

Show
fisharebest added a comment - Also, line 23 uses $auth, which is not defined anywhere. Also, (possibly not a bug, just me being a bumbling novice!), $this->_getParam('db') returns null, rather than a DbAdapter object. I'm attaching this controller/action to the quickstart tutorial code (which seems to use the db adapter without doing any initialisation).
Hide
PVWebGuy added a comment -

Has anyone figured out the issue with the sample code? If so, please post!

Show
PVWebGuy added a comment - Has anyone figured out the issue with the sample code? If so, please post!
Hide
Menencia added a comment -

Well, I'm new too and I try to fix the source code as next:

$db	= Zend_Db_Table_Abstract::getDefaultAdapter();
 
        $loginForm = new Default_Form_Auth_Login($_POST);
 
        if ($loginForm->isValid($_POST)) {
 
            $adapter = new Zend_Auth_Adapter_DbTable(
                $db,
                'utilisateurs',
                'login',
                'mdp',
                'MD5(?)'
                );
 
            $adapter->setCredential($loginForm->getValue('mdp'))
            		->setIdentity($loginForm->getValue('login'));
 
            $result = $adapter->authenticate($adapter);
 
            if ($result->isValid()) {
                //$this->_helper->FlashMessenger('Successful Login');
                echo "Logged!";
                //$this->_helper->redirector('index');
                return;
            } else {
            	echo "Not Logged!";
            }
 
        }
 
        $this->view->loginForm = $loginForm;

Don't forget to initialize the session with a Zend_Session::start();
Also, in the auth/login.phtml, correct $this->form into $this->loginForm

Works here. Hope it helps you

Show
Menencia added a comment - Well, I'm new too and I try to fix the source code as next:
$db	= Zend_Db_Table_Abstract::getDefaultAdapter();
 
        $loginForm = new Default_Form_Auth_Login($_POST);
 
        if ($loginForm->isValid($_POST)) {
 
            $adapter = new Zend_Auth_Adapter_DbTable(
                $db,
                'utilisateurs',
                'login',
                'mdp',
                'MD5(?)'
                );
 
            $adapter->setCredential($loginForm->getValue('mdp'))
            		->setIdentity($loginForm->getValue('login'));
 
            $result = $adapter->authenticate($adapter);
 
            if ($result->isValid()) {
                //$this->_helper->FlashMessenger('Successful Login');
                echo "Logged!";
                //$this->_helper->redirector('index');
                return;
            } else {
            	echo "Not Logged!";
            }
 
        }
 
        $this->view->loginForm = $loginForm;
Don't forget to initialize the session with a Zend_Session::start(); Also, in the auth/login.phtml, correct $this->form into $this->loginForm Works here. Hope it helps you
Hide
Thomas Weidner added a comment -

Assigned correct component

Show
Thomas Weidner added a comment - Assigned correct component
Hide
Remy Damour added a comment -

I personally added a submit-page check so that 'Value is required and can't be empty' does not get displayed when we first land on the page.

I replaced:

if ($loginForm->isValid($_POST)) {

with

if (isset($_POST['submit']) && $loginForm->isValid($_POST)) {
Show
Remy Damour added a comment - I personally added a submit-page check so that 'Value is required and can't be empty' does not get displayed when we first land on the page. I replaced:
if ($loginForm->isValid($_POST)) {
with
if (isset($_POST['submit']) && $loginForm->isValid($_POST)) {
Hide
Remy Damour added a comment -

as fisharebest said, $auth is not defined!

I had to replace:

$result = $auth->authenticate(adapter);

with:

$result = $adapter->authenticate();
Show
Remy Damour added a comment - as fisharebest said, $auth is not defined! I had to replace:
$result = $auth->authenticate(adapter);
with:
$result = $adapter->authenticate();
Hide
Remy Damour added a comment -

on line 27, $this->redirect('/') generated an error (unknown method 'redirect')

I replaced:

$this->redirect('/');

with:

$this->_redirect('/');
Show
Remy Damour added a comment - on line 27, $this->redirect('/') generated an error (unknown method 'redirect') I replaced:
$this->redirect('/');
with:
$this->_redirect('/');
Hide
Ramon Henrique Ornelas added a comment -

Fix in trunk r23868 and merged to branch release 1.11 r23869 - thanks.

Show
Ramon Henrique Ornelas added a comment - Fix in trunk r23868 and merged to branch release 1.11 r23869 - thanks.

People

Vote (1)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: