Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-18-2007, 09:29 AM
quannm0410l's Avatar
Junior Member
 
Join Date: Mar 2007
Posts: 11
Red face How can i set timeout for user when login using zend_auth?

This is my MyAuthAdapter class
PHP Code:

<?php
require_once 'Zend/Auth/Adapter/Interface.php';
require_once 
'Zend/Auth/Result.php';

class 
MyAuthAdapter implements Zend_Auth_Adapter_Interface
{
    protected 
$_username;
    protected 
$_password;
    
/**
     * Sets username and password for authentication
     *
     * @return void
     */
    
public function __construct($username$password)
    {
        
$this->_username  $username;
        
$this->_password $password;
    }

    
/**
     * Performs an authentication attempt
     *
     * @throws Zend_Auth_Adapter_Exception If authentication cannot be performed
     * @return Zend_Auth_Result
     */
    
public function authenticate()
    {
        
$DB Zend_Registry::get('dbAdapter');
        
        
$select $DB->select();
        
$select->from('tbl_users''username''password');
        
$select->where('Username = ?'$this->_username);
        
$select->where('Password = ?'$this->_password);
        
$sql $select->__toString();

        
$rowsFound $DB->fetchAll($select);
        if (isset(
$rowsFound[0]['username']))
        {
            
$msg=array();
            
$msg[1]="Authenticated";
            
$result = new Zend_Auth_Result(true$this->_username$msg);
            return 
$result;
        }
        else
        {
            
$msg=array();
            
$msg[1]="Sorry, you are not authenticate to access this website. Please check your username and password";
            
$result = new Zend_Auth_Result(false$this->_username$msg);
            return 
$result;
        }
    }
}
?>

And this is authenticateAction()
PHP Code:

public function authenticateAction()
    {        
        if(
$this->_request->isPost())
        {
            
Zend_Loader::loadClass('Zend_Filter_StripTags');
            
$f = new Zend_Filter_StripTags();
            
$username $f->filter($this->_request->getPost('username'));
            
$password $f->filter($this->_request->getPost('password'));
            
            
$authAdapter = new MyAuthAdapter($username$password);
            
            
$auth Zend_Auth::getInstance();
            
$result $auth->authenticate($authAdapter);
            
            if (!
$result->isValid()) {
            
// Authentication failed; print the reasons why
                
foreach ($result->getMessages() as $message) {
                    echo 
"$message\n. Click <a href='".$this->view->baseUrl."/'>here</a> to try again";
                }
            } else {
                
                
$dbAdapter Zend_Registry::get('dbAdapter');
                
$select $dbAdapter->select()
                    ->
from(array('r' => 'tbl_roles'),
                        array(
'roleid''rolename'))
                    ->
join(array('u' => 'tbl_users'),
                        
'r.roleid = u.roleid');
                
$select->where('u.username = ?'$auth->getIdentity());
                
$rs $dbAdapter->fetchAll($select);
                
                if (isset(
$rs[0]['rolename']))
                {
                    
$role $rs[0]['rolename'];
                    
                    
$_SESSION['role'] = $role;
                }

                
$result->getIdentity() === $auth->getIdentity();
                
                
$this->_redirect($this->baseUrl.'/');
            }
        }
        else
        {
            
$this->_redirect($this->baseUrl.'/');
        }
    } 
I want to set timeout after 20minutes from the last interaction. Someone help me

Last edited by SpotSec : 05-02-2007 at 05:54 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-20-2007, 07:24 PM
Junior Member
 
Join Date: Mar 2007
Posts: 1
Default

This assumes that you aren't changing the default Zend_Auth namespace. If you are then update my code below to reflect your defined namespace.

PHP Code:

$authSession 
= new Zend_Session_Namespace('Zend_Auth');
$authSession->setExpirationSeconds(3600); 
You will want to insert that into your authenticateAction() function right after the user is determined to be valid. Below is your code showing you where the snippet above should be roughly.

PHP Code:

if (!$result->isValid()) {
  
// Authentication failed; print the reasons why
  
foreach ($result->getMessages() as $message) {
    echo 
"$message\n. Click <a href='".$this->view->baseUrl."/'>here</a> to try again";
  }
} else {
  
$authSession = new Zend_Session_Namespace('Zend_Auth');
  
$authSession->setExpirationSeconds(3600);
  
$dbAdapter Zend_Registry::get('dbAdapter');

... 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 06:03 PM.