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


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-15-2007, 08:18 PM
jap jap is offline
Junior Member
 
Join Date: Apr 2007
Posts: 23
Default Add extra data to an existing auth instance

Hello,

I've just made an Auth system, and when a user is logged in I can use all the variables stored in an stdClass. But, I want more... I want to have a "login history" so I can track from wich IP a user logged in last and at what date and time, actually all the other user related info.

So I store all that data in a table called users_auth_history and every record is a login. Getting the data in the database is nothing. But getting it out and being able to add it to the already existing user information collected by these two functions:

PHP Code:
//Storing the user information (without the password)
$data $authAdapter->getResultRowObject(null,'password');
$auth->getStorage()->write($data);

//Retrieving the information and send it to the view
$this->view->user Zend_Auth::getInstance()->getIdentity(); 
This might be a stupid question, but I really have no idea where to start.

Thanks,
Jasper
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-15-2007, 09:48 PM
SpotSec's Avatar
Senior Member
 
Join Date: Feb 2007
Location: United States
Posts: 116
Default

well getIdentity() can return a class with all the data in it.

so you would access it like getIdentity()->getLastLogin() etc.....
sorry about the vagueness, I'm in a hurry
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-16-2007, 02:09 AM
Junior Member
 
Join Date: May 2007
Posts: 25
Default one idea

i use this code for authorization. it needs to be refactored and documented for the production version, but may give you some ideas.

one key here is that it allows you to store multiple roled per session. i have users who are also members. this allows them to be logged into admin and their member account at the same time, without ever granting a member access to admin.

one other key is the utility role, registeredUser. this is helpfull for when you want to save some information about a site visitor for a few.


PHP Code:
<?php
class MyAuth
{
    private 
$userNamespace;
    private 
$user;
    private 
$member;
    private 
$registered_user;
    private 
$username;
    private 
$password;
    
    function 
__construct(){
        
$this->userNamespace = new Zend_Session_Namespace('User');
        if(isset(
$this->userNamespace->user)){
            
$this->user $this->userNamespace->user;
            
//log the users request
            
$log = new AccessLog();
            
$log->log($this->user->id);
        }
        if(isset(
$this->userNamespace->member)){
            
$this->member $this->userNamespace->member;
        }
        if(isset(
$this->userNamespace->registered_user)){
            
$this->registered_user $this->userNamespace->registered_user;
        }

    }
    
    function 
authenticate($username$password,$type false){
        if(
$type == 'person'){
            
$e = new libEncrypt();
            
$password $e->encryptData(trim($password));
            
$where "username = '$username' AND password = \"{$password}\"";
            
$user = new People();
            
$currUser $user->fetchRow($where);
            if(!
$currUser->id == ''){
                
$userClass = new stdClass();
                
$userClass->id $currUser->id;
                
$userClass->first_name $currUser->first_name;
                
$userClass->last_name $currUser->last_name;
                
$userClass->email $currUser->email;

                
//get user role
                
$r = new PeopleRoles();
                echo 
$currUser->role;
                
$ur $r->find($currUser->role)->current();
                
//Zend_Debug::dump($ur);
                
$role $ur->role;
                
$userClass->role $role;
                return 
$this->userNamespace->$role $userClass;
            }else{
                
$e = new libErrors();
                
$e->add("The username or password you entered is not correct");
            }
        }else{
            
$e = new libEncrypt();
            
$password $e->encryptData($password);
            
$where "email = '$username' AND password = \"{$password}\"";
            
$user = new Users();
            
$currUser $user->fetchRow($where);
            if(!
$currUser->id == ''){
                
$userClass = new stdClass();
                
$userClass->id $currUser->id;
                
$userClass->first_name $currUser->first_name;
                
$userClass->last_name $currUser->last_name;
                
$userClass->role $currUser->role;
                
$userClass->email $currUser->email;
                
$userClass->editor $currUser->editor;
                
                
//load the users usergroup
                
$g = new UserGroups();
                
$group =  $g->find($currUser->user_group_id)->current();
                
                
$userGroup = new stdClass();
                
$userGroup->id intval($group->id);
                
$userGroup->name = (string)$group->name;
                
                
$userClass->group $userGroup;

                
$this->user $userClass;
                return 
$this->userNamespace->user $this->user;
            }
        }
    }
    
    
/**
     * the registered user is like a utility class to store temp data
     * no need to validate it here
     *
     * @return unknown
     */
    
function hasIdentity()
    {
        if(
$this->user || $this->member){
            return 
true;
        }
    }
    
    function 
getUser()
    {
        return 
$this->user;
    }
    
    function 
getMember()
    {
       return 
$this->member
    }
    
    function 
getRegisteredUser()
    {
       return 
$this->registered_user
    }
    
    function 
destroy()
    {
        foreach (
$this->userNamespace as $k=>$v)
        {
            
$this->userNamespace->$k false;
            unset(
$this->userNamespace->$k );
        }
    }
    
}
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 12:22 AM.