Zend session and zend auth
Asked Answered
I

6

5

I have made a login system through zend auth here is the code

// userAuthentication
   public function authAction(){
       $request     = $this->getRequest();
       $registry    = Zend_Registry::getInstance();
       $auth        = Zend_Auth::getInstance(); 
       $DB = $registry['DB'];
           $authAdapter = new Zend_Auth_Adapter_DbTable($DB);
               $authAdapter->setTableName('user')
                            ->setIdentityColumn('user_name')
                            ->setCredentialColumn('user_password');

      $username = $request->getParam('username');
      $password = $request->getParam('password');
      $authAdapter->setIdentity($username);
      $authAdapter->setCredential($password);
      $result = $auth->authenticate($authAdapter);

      if($result->isValid()){
           $data = $authAdapter->getResultRowObject(null,'password');
           $auth->getStorage()->write($data);
           $this->_redirect('/login/controlpannel');
       }else{
           $this->_redirect('/login/login');
        }
  }

This work fine now. There is user_id (column) in user (table) where there are username and password too. I need to get that specific user_id from this table which just login and put it in session through

$user_session = new Zend_Session_Namespace('user_session');
$user_session->username = $username;
$user_id->user_id       = $user_id;

so that I can query some info against this $user_id and pass the result into view (name) controlpanel

Ist answered 25/1, 2012 at 11:21 Comment(3)
why you want to put in session manually? It will create session once login and you can get that from storage.Standish
@Teez how can i get user_id from storage ???Ist
$data = Zend_Auth::getInstance()->getStorage()->read(); $this->view->username = $data->user_name; $this->view->id = $data->user_id;Ist
S
8

Get user id from storage :

$userInfo = Zend_Auth::getInstance()->getStorage()->read();

echo $userInfo->user_id;
Standish answered 25/1, 2012 at 11:41 Comment(0)
N
8

While this was already answered, I tend to use the getIdentity() function more frequently than the getStorage()->read() chain. Examples below.

// to check if authenticated
Zend_Auth::getInstance()->hasIdentity();

// to actually get the details from storage
Zend_Auth::getInstance()->getIdentity()->user_id;

// if I need to use the identity over and over
$identity = Zend_Auth::getInstance()->getIdentity();
$userId = $identity->user_id;
Neustria answered 9/5, 2012 at 18:41 Comment(0)
K
3

You can access the data the way Teez suggest or just pull it from Zend_Session_Namespace.

15.1.3.1. Default Persistence in the PHP Session
By default, Zend_Auth provides persistent storage of the identity from a successful authentication attempt using the PHP session. Upon a successful authentication attempt, Zend_Auth::authenticate() stores the identity from the authentication result into persistent storage. Unless configured otherwise, Zend_Auth uses a storage class named Zend_Auth_Storage_Session, which, in turn, uses Zend_Session. A custom class may instead be used by providing an object that implements Zend_Auth_Storage_Interface to Zend_Auth::setStorage().

Zend_Auth_Storage_Session uses a session namespace of 'Zend_Auth'. This namespace may be overridden by passing a different value to the constructor of Zend_Auth_Storage_Session, and this value is internally passed along to the constructor of Zend_Session_Namespace. This should occur before authentication is attempted, since Zend_Auth::authenticate() performs the automatic storage of the identity.

Kentigera answered 25/1, 2012 at 11:43 Comment(0)
S
2

assigning an array to a session, you must provide a name to the session you area creating, i.e. you must do setStorage before you do getStorage.

you must write your code like this:

   // userAuthentication
   public function authAction(){
       $request     = $this->getRequest();
       $registry    = Zend_Registry::getInstance();
       $auth        = Zend_Auth::getInstance(); 
       $DB = $registry['DB'];
           $authAdapter = new Zend_Auth_Adapter_DbTable($DB);
               $authAdapter->setTableName('user')
                            ->setIdentityColumn('user_name')
                            ->setCredentialColumn('user_password');


      $username = $request->getParam('username');
      $password = $request->getParam('password');
      $authAdapter->setIdentity($username);
      $authAdapter->setCredential($password);
      $authAdapter->setStorage(new Zend_Auth_Storage_Session('User_Auth'));
      $result = $auth->authenticate($authAdapter);
      if($result->isValid()){
      $data = $authAdapter->getResultRowObject(null,'password');
       $auth->getStorage()->write($data);
       $this->_redirect('/login/controlpannel');
       }else{
           $this->_redirect('/login/login');
        }
  }

and then to get your storage value, you must use this:

$x = new Zend_Auth_Storage_Session('User_Auth');
$y = $x->read();

and you get everything in $y as an object.

Enjoy!

Sidoon answered 31/7, 2013 at 10:32 Comment(0)
L
1

This is my approach and it s working nice: 1-i start by defining an init function in the bootstrap

protected function _initSession()
{

    $UserSession = new Zend_Session_Namespace('UserSession');
    $UserSession->setExpirationSeconds(/* you may fix a limit */);
    Zend_Registry::set('UserSession', $UserSession);
}

/* in the Login action,after correct username & pwd */

 // Create session
    $UserSession = Zend_Registry::get('UserSession');
 // Get the user from database 
 $db = Zend_Db_Table::getDefaultAdapter();
 $user = $db->fetchRow("SELECT * FROM user_table WHERE user_email = '".$user_email."'");

 //then you assign to $user to $UserSession variable : 
 $UserSession->user = $user;

 //finaly don't forget to unset session variable in the Logout action ...
Latona answered 5/6, 2013 at 11:6 Comment(1)
remember to sanitize/parametrize the user email before such query ;)Walking
F
0
user = Zend_Auth::getInstance()->getIdentity(); if(!@$this->user){ $objSession->errorMsg = " Please Login First .. ! "; $this->_redirect('/user/login'); } ?>
Furious answered 8/10, 2014 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.