CodeIgniter Forums
Model or Library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Model or Library (/showthread.php?tid=15217)



Model or Library - El Forum - 01-29-2009

[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');
            }
    }
    
}



Model or Library - El Forum - 01-29-2009

[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.


Model or Library - El Forum - 01-30-2009

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