Changeset 56

Show
Ignore:
Timestamp:
04/12/07 04:57:54
Author:
grahack
Message:

FAL: adding convenient helper functions to get user infos (the one connected or others) + adding some more user info in db_session + renaming the 'username' key in this db_session to 'user_name'

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • FreakAuth/trunk/www/system/application/controllers/example.php

    r2 r56  
    1616                  
    1717                        echo '<h1>You can view this message because you logged in and are at least an user!</h1>'; 
    18                   }      
    19     
     18                        echo 'You are <b>'.getUserName().'</b> (role:<b>'.getUserProperty('role').'</b>),'; 
     19                        echo ' your id is <b>'.getUserProperty('id').'</b>.<br />'; 
     20                        echo 'Your superadmin is <b>'.getUserPropertyFromId(1,'user_name').'</b>.'; 
     21                  } 
    2022} 
    2123?> 
  • FreakAuth/trunk/www/system/application/helpers/freakauth_light_helper.php

    r7 r56  
    3535    $obj =& get_instance(); 
    3636    return $obj->freakauth_light->getUserName(); 
     37} 
     38 
     39// ------------------------------------------------------------------------ 
     40// 
     41// Returns the currently logged on user's prop. 
     42// Returns an empty string if no user is logged in. 
     43// 
     44function getUserProperty($prop) 
     45{ 
     46    $obj =& get_instance(); 
     47    return $obj->freakauth_light->getUserProperty($prop); 
     48} 
     49 
     50// ------------------------------------------------------------------------ 
     51// 
     52// Returns the prop of the user identified by $id 
     53// Returns a message if the user does not exist 
     54// Returns an empty string if $prop is unknown. 
     55// 
     56function getUserPropertyFromId($id, $prop) 
     57{ 
     58        $obj =& get_instance(); 
     59    return $obj->freakauth_light->getUserPropertyFromId($id, $prop); 
    3760} 
    3861 
  • FreakAuth/trunk/www/system/application/libraries/Freakauth_light.php

    r20 r56  
    194194        if ($this->CI->db_session AND $this->CI->config->item('FAL')) 
    195195        { 
    196             $_username = $this->CI->db_session->userdata('username'); 
     196            $_username = $this->CI->db_session->userdata('user_name'); 
    197197            $_role = $this->CI->db_session->userdata('role'); 
    198198             
     
    222222        if ($this->CI->db_session AND $this->CI->config->item('FAL')) 
    223223        { 
    224             $_username = $this->CI->db_session->userdata('username'); 
     224            $_username = $this->CI->db_session->userdata('user_name'); 
    225225            $_role = $this->CI->db_session->userdata('role'); 
    226226             
     
    246246        if ($this->CI->db_session AND $this->CI->config->item('FAL')) 
    247247        { 
    248             $_username = $this->CI->db_session->userdata('username'); 
     248            $_username = $this->CI->db_session->userdata('user_name'); 
    249249            $_role = $this->CI->db_session->userdata('role'); 
    250250             
     
    281281        if ($this->CI->db_session AND $this->CI->config->item('FAL')) 
    282282        { 
    283             $_username = $this->CI->db_session->userdata('username'); 
     283            $_username = $this->CI->db_session->userdata('user_name'); 
    284284            $_who_is = $this->CI->db_session->userdata('role'); 
    285285             
     
    369369                { 
    370370                    $row = $query->row(); 
    371                     $userdata['id'] = $row->{'id'}; 
    372                     $userdata['username'] = $row->{'user_name'};                     
    373                     $userdata['role'] = $row->{'role'}; 
    374                     $banned = $row->{'banned'}; 
     371                    $fields = array('id', 'user_name', 'country_id', 'email', 
     372                                  'role', 'last_visit', 'created', 'modified'); 
     373                    foreach($fields as $field) $userdata[$field] = $row->{$field}; 
     374 
    375375                    //verifies if an user has not been banned from the site (i.e. user table, banned=1) 
    376                     if ($banned == 0) 
     376                    if ($row->{'banned'} == 0) 
    377377                    { 
    378378                        $this->_set_logindata($userdata); 
     
    408408        if ($this->CI->db_session) 
    409409        { 
    410             $_username = $this->CI->db_session->userdata('username'); 
     410            $_username = $this->CI->db_session->userdata('user_name'); 
    411411 
    412412            if ($_username != false) 
     
    705705    function _unset_user($_username) 
    706706    { 
    707         $users = $this->CI->db_session->userdata('username'); 
     707        $users = $this->CI->db_session->userdata('user_name'); 
    708708         
    709709        if (isset($users)) 
     
    712712            //is better to do a 1 call to unset_userdata passing an array? 
    713713            $this->CI->db_session->unset_userdata('id'); 
    714             $this->CI->db_session->unset_userdata('username'); 
     714            $this->CI->db_session->unset_userdata('user_name'); 
    715715            $this->CI->db_session->unset_userdata('role'); 
    716716        } 
     
    764764             
    765765                // returns username string of currently logged in user 
    766                 return $this->CI->db_session->userdata('username'); 
     766                return $this->CI->db_session->userdata('user_name'); 
    767767         
    768768        // returns empty string if user not logged in 
    769769        return ''; 
     770    } 
     771     
     772        // -------------------------------------------------------------------- 
     773     
     774    /** 
     775     * Returns the currently logged in user's prop 
     776     * Returns an empty string if no user is logged in 
     777     * uses function isValidUser() 
     778     * and Class db_session method "userdata". 
     779     *  
     780     * @return prop string of currently logged in user 
     781     * @return empty string if user not logged in 
     782     */ 
     783    function getUserProperty($prop) 
     784    { 
     785        if ($this->CI->config->item('FAL') && $this->CI->db_session && ($this->isValidUser() OR $this->isAdmin())) 
     786             
     787                // returns property string of currently logged in user 
     788                return $this->CI->db_session->userdata($prop); 
     789         
     790        // returns empty string if user not logged in 
     791        return ''; 
     792    } 
     793     
     794 
     795    /** 
     796     * Returns the prop of the user identified by $id 
     797     * Returns an empty string if no user is logged in 
     798     * uses function isValidUser() 
     799     * and Class db_session method "userdata". 
     800     *  
     801     * @return prop string of currently logged in user 
     802     * @return empty string if user not logged in 
     803     */ 
     804    function getUserPropertyFromId($id, $prop) 
     805    { 
     806                $query = $this->CI->usermodel->getUserById($id); 
     807                if ($query->num_rows() == 1) 
     808                { 
     809                        $row = $query->row(); 
     810                        if (isset($row->{$prop})) return $row->{$prop}; 
     811                        else return ''; 
     812                } 
     813                else 
     814                { 
     815                        $this->CI->lang->load('freakauth'); 
     816                        return $this->CI->lang->line('FAL_unknown_user'); 
     817                } 
    770818    } 
    771819     
  • FreakAuth/trunk/www/system/language/english/freakauth_lang.php

    r40 r56  
    3838$lang['FAL_citation_message'] = 'Thank You!'; 
    3939$lang['FAL_already_logged_in_msg'] = 'you have already logged in!'; 
     40 
     41$lang['FAL_unknown_user'] = 'utilisateur inconnu'; 
    4042 
    4143//------------------------------------------------------------------