CodeIgniter Forums

Full Version: Model or Library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]drale2k[/eluser]
Hi, i wrote a Login Class but i am not sure if it is better for a Model or a Library. In this case it is a Model, should it be ?

Code:
class Login_Model extends Model
{
    function Login_Model()
    {
        parent::Model();
    }
    
    function verifyUser($u, $pw, $bool_escape_str = TRUE)
    {
        $this->db->select('id, username, email, last_login');

        // check if escape or not
        
        if ($bool_escape_str == FALSE)
        {
            $this->db->where('username', $u);
            $this->db->where('password', $pw);
        }
        
        else
        {
            $this->db->where('username', $this->db->escape_str($u));
            $this->db->where('password', $this->db->escape_str($pw));
        }
        
        
        
        $this->db->limit(1);
        
        $query = $this->db->get('users');
        if ($query->num_rows() > 0)
        {
            $user = $query->row_array();
            $_SESSION['active'] = TRUE;
            $_SESSION['userid'] = $user['id'];
            $_SESSION['username'] = $user['username'];
            $_SESSION['email'] = $user['email'];
            $_SESSION['lastlogin'] = $user['last_login'];
            
            // update last_login
            $date = date('Y-m-d h:m:s');
            $data = array('last_login' => $date);
            $this->db->update('users', $data);
            
            return TRUE;
        }
        
        else
        {
            return FALSE;
        }
    }    
    
    function verifyAdmin($uid)
    {
        if ($_SESSION['active'] == TRUE)
        {
            $uid = $_SESSION['userid'];
            
            $this->db->select('user_id');
            $this->db->where('user_id', $uid);
            $this->db->limit(1);
            
            $query = $this->db->get('admins');
            if ($query->num_rows() > 0)
            {
                $_SESSION['admin'] = TRUE;
                return TRUE;
            }
            
            else
            {
                return FALSE;
            }
        }
        
        else
        {
            return FALSE;
        }
    }

    function verifySession()
    {
        if ( ! isset($_SESSION['admin']) || ! isset($_SESSION['active']) )
            {
                redirect('welcome');
            }
    }
    
}

El Forum

[eluser]gullah[/eluser]
What I've seen from a lot of the auth systems out there is they are 1 part library and 1 part model. The DB stuff is kept strictly in the model where the session handling, management, etc is kept in the library. It seems to work pretty well and keeps you code separated.

El Forum

[eluser]drale2k[/eluser]
Ok thanks, i will see how to do it best.