Zend 1.12: auth adapter using doctrine 2

739 views Asked by At

how to getting auth adapter using doctrine 2 instead of zend_db:

private function getAuthAdapter() {

 $authAdapter= new Zend_Auth_Adapter_DbTable(
   Zend_Db_Table::getDefaultAdapter()
 );

 $authAdapter->setTableName('users')
   ->setIdentityColumn('username')
   ->setCredentialColumn('password')
   ->setCredentialTreatment('MD5(?)');

 return $authAdapter;                              
}

My test following a tutorial:

$dbAdapter = Doctrine::getConnectionByTableName('users');
$authAdapter = new Zend_Auth_Adapter_Doctrine_Table( $dbAdapter); 
//downloaded from here link //http://framework.zend.com/wiki/download/attachments/3866950/Table.php 

$authAdapter->setTableName('users')
 ->setIdentityColumn('username')
 ->setCredentialColumn('password')
 ->setCredentialTreatment('MD5(?)');

return $authAdapter;

i got

Warning: include_once(Doctrine.php): failed to open stream: No such file or directory in C:\wamp\www\MyProject\library\Zend\Loader.php on line 134

I don't have any class contain getConnectionByTableName!!

-----
C:\wamp\www\MyProject\library\Doctrine>tree
C:.
├───Common
│   ├───Annotations
│   ├───Cache
│   ├───Collections
│   └───Util
├───DBAL
│   ├───Driver
│   │   ├───IBMDB2
│   │   ├───OCI8
│   │   ├───PDOIbm
│   │   ├───PDOMySql
│   │   ├───PDOOracle
│   │   ├───PDOPgSql
│   │   ├───PDOSqlite
│   │   └───PDOSqlsrv
│   ├───Event
│   │   └───Listeners
│   ├───Logging
│   ├───Platforms
│   ├───Schema
│   │   └───Visitor
│   ├───Tools
│   │   └───Console
│   │       ├───Command
│   │       └───Helper
│   └───Types
├───ORM
│   ├───Event
│   ├───Id
│   ├───Internal
│   │   └───Hydration
│   ├───Mapping
│   │   └───Driver
│   ├───Persisters
│   ├───Proxy
│   ├───Query
│       │   ├───AST
│   │   │   └───Functions
│   │   ├───Exec
│   │   └───Expr
│   └───Tools
│       ├───Console
│       │   ├───Command
│       │   │   ├───ClearCache
│       │   │   └───SchemaTool
│       │   └───Helper
│       ├───Event
│       └───Export
│           └───Driver
└───Symfony
    └───Component
        ├───Console
        │   ├───Command
        │   ├───Helper
        │   ├───Input
        │   ├───Output
        │   └───Tester
        └───Yaml
-----------

Thanks in advance :)

1

There are 1 answers

0
bsmedhabib On BEST ANSWER

The Solution:

class AuthenticationController extends Zend_Controller_Action
{

public function init()
{
    /* Initialize action controller here */
}

public function indexAction()
{
    // action body
    }

public function loginAction()
{
    $this->view->title='Login';
    if(Zend_Auth::getInstance()->hasIdentity())
  {$this->_redirect();}  

  $request=$this->getRequest();
  $form=new Form_LoginForm();

  if($request->isPost())
  {if($form->isValid($this->_request->getPost())){
     $authAdapter =$this->getAuthAdapter();    

  $username=$form->getValue('username');
  $password=$form->getValue('password');
  $authAdapter->setIdentity($username)
              ->setCredential(md5($password));

  $auth=Zend_Auth::getInstance();

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

  if($result->isValid())
  {  $doctrine = $this->getDoctrineContainer();
     $em = $doctrine->getEntityManager(); 
     $identity = $em->getRepository('\ZC\Entity\Users')
                ->find($authAdapter->getIdentity());
  $authStorage=$auth->getStorage();
  $authStorage->write($identity);
    $this->redirect($_SERVER['HTTP_REFERER']);         
  }else{

      $this->view->errorMessage='Username or password is wrong';
  }


  }   
  }

  $this->view->form=$form;


}  


public function logoutAction()
{
   Zend_auth::getInstance()->clearIdentity();

   $this->_redirect();
}

public function getDoctrineContainer()
{
    return $this->getInvokeArg('bootstrap')->getResource('doctrine');
}
private function getAuthAdapter() {
       $doctrine = $this->getDoctrineContainer();
    $em = $doctrine->getEntityManager();
      $authAdapter = new Doctrine\Auth\doctrine2($em);
      $authAdapter->getIdentity();

         $authAdapter->setEntityClassName('ZC\Entity\users')
               ->setIdentityField('username')
               ->setCredentialField('password');
                       return $authAdapter;


}
    }