Welcome Guest, Not a member yet? Register   Sign In
Autoload problem
#7

[eluser]weedy[/eluser]
Yeah, what I did was made the hook a function (It could've been a class but since I can't have 2 controllers, it would be useless anyway):

Code:
<?
/**
* Auth
*
* Check if the user has been logged in. Uses Session class with DB sessions.
*
*
* @access    public
*
*/

function auth()
{    
    $CI =& get_instance();
    
    $CI->load->library('session');
    
    $uri = $CI->uri->uri_string();
    
    //check versus recurrency
    if($uri != '/user/login')
    {
        //id should be known
        if(!$CI->session->userdata('id'))
        {
            //save current uri to redirect back to it after login
            $CI->session->set_userdata('redirect' , $uri);
             redirect('/user/login');
        }
        else
        {
            $CI->load->model('user_model', 'user');
            $CI->user->log(array('id' => $CI->session->userdata('id')));
        }
    }
}
?>

This is loaded post controller:
Code:
$hook['post_controller'][] = array(
                                'class'    => '',
                                'function' => 'auth',
                                'filename' => 'auth.php',
                                'filepath' => 'hooks',
                                );

Uses user_model:
Code:
<?
class User_model extends Model {

    function User_model()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    function auth($user)
    {
        $this->
            db->
                select('id, name, surname, email, company')->
                from('_users')->
                where('username = "' . $this->db->escape_str($user['username']) . '" AND password = MD5("' . $this->db->escape_str($user['password']) . '")');

        $query = $this->db->get();
        
        $result = $query->row();
        
        if (!empty($result->id))
        {
            $this->log(array('id' => $result->id));
        }    
            
        return $query->row();
    }

    function log($user)
    {
        if(!empty($user['id']))
        {
            //rework to use AR!
            return $this->db->query('UPDATE _users SET `last` = "' . time() . '" WHERE `id` = ' . (int)$user['id']);
        }
        return false;
    }

    function insert($user)
    {
        return $this->db->insert('_users', $user);
    }

    function update($user)
    {
        return $this->db->update('_users', $user, array('id' => $user['id']));
    }

}
?>
(the query in log function does not use active record and therefore is sub-optimal, I've noted that and will rework it to use AR once I have time)

And supplemented by User class:
Code:
<?php

class User extends Controller {

    function User()
    {
        parent::Controller();
    }
    
    function index()
    {
        $this->login();
    }
    
    function login()
    {
        $user = $this->input->post('user');
        $password = $this->input->post('password');
        
        if($user && $password)
        {
            $this->load->model('user_model', 'user');
            if($auth = $this->user->auth(array('username' => $user, 'password' => $password)))
            {
                foreach($auth as $key => $value)
                {
                    $this->session->set_userdata($key, $value);
                }

                if($redirect = $this->session->userdata('redirect'))
                {
                    if ($redirect == '/user/logout' || $redirect == '/user/')
                    {
                        $redirect = '/plan';
                    }
                    //move back to original request url
                    redirect($redirect);
                }
                
                //if no original request url, move to index
                redirect('/plan');
            }
        }
        
        $vars = array('title' => false);
        $this->load->view('common/header',$vars);
        $this->load->view('user/login');
        $this->load->view('common/footer');
    }
    
    function logout()
    {
        $this->session->destroy();
        redirect('');
    }
}
?>


Hope that helps. Smile


Messages In This Thread
Autoload problem - by El Forum - 05-11-2008, 05:31 AM
Autoload problem - by El Forum - 05-11-2008, 07:28 AM
Autoload problem - by El Forum - 05-11-2008, 08:00 AM
Autoload problem - by El Forum - 05-11-2008, 08:15 AM
Autoload problem - by El Forum - 05-11-2008, 08:42 AM
Autoload problem - by El Forum - 10-01-2008, 07:56 PM
Autoload problem - by El Forum - 10-02-2008, 12:33 AM



Theme © iAndrew 2016 - Forum software by © MyBB