Welcome Guest, Not a member yet? Register   Sign In
building an efficient admin control panel
#21

[eluser]kakap[/eluser]
hello, I need help here in using ci+hmvc+ion auth. my simple codes :

MY_Controller: (location:application/libraries/MY_Controller.php)
Code:
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class MY_Controller extends Controller{
    
    function __construct() {
        
        parent::Controller();
        
    }
}

Admin_Controller: (location:application/libraries/Admin_Controller.php)
Code:
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Admin_Controller extends MY_Controller{
    
    function __construct() {
        
        parent::__construct();
        
        //check apakah user login
        if (!$this->ion_auth->logged_in()) {
            
            //jika belum login kirim ke halaman login admin
            redirect('admin/login', 'refresh');
        }
        //cek apakah yang login adalah admin
        elseif (!$this->ion_auth->is_admin()) {
            
            //jika yg login bukan admin (user biasa) maka
            //dikembalikan ke halaman profil siswa.
            redirect('siswa/index', 'refresh');
        }
    }
}

admin.php: (location:application/controller/admin.php)
Code:
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Admin extends Admin_Controller{
    
    function __construct() {
        
        parent::__construct();
        $this->load->library('form_validation');
    }
    
    function index() {
        
        //here
        $this->data['title'] = 'Dashboard';
        
        $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
        $this->data['users'] = $this->ion_auth->get_users_array();
        
        $this->load->view('admin/index', $this->data);
    }
    
    function login() {
        
        //here
        $this->data['title'] = 'Login';
        
        //validasi input
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'required');
        
        if ($this->form_validation->run() == TRUE) {
                
            //cek remember me
            if ($this->input->post('remember') == 1) {
                
                $remember = TRUE;
            }
            else {
                
                $remember = FALSE;
            }
            
            if ($this->ion_auth->login($this->input->post('email'), $this->input->post('password'), $remember)) {
                
                $this->session->set_flashdata('message', $this->ion_auth->messages());
                redirect('admin/index', 'refresh');
            }
            else {
                
                $this->session->set_flashdata('message', $this->ion_auth->errors());
                redirect('admin/login', 'refresh');
            }
        }
        else {
            
            //disini adalah antarmuka login
            //$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
            
            $this->data['email']    = array(
                                            'name'  => 'email',
                                            'id'    => 'email',
                                            'type'  => 'text',
                                            'value' => $this->form_validation->set_value('email')
                                            );
                                            
            $this->data['password'] = array(
                                            'name'  => 'password',
                                            'id'    => 'password',
                                            'type'  => 'password'
                                            );
            
            $this->load->view('admin/login', $this->data);
        }
    }
    
    function logout() {
        
        //here
        $this->data['title'] = 'Logout';
        
        $logout = $this->ion_auth->logout();
        redirect('admin/login', 'refresh');
    }
}

?>

when I try to access http://localhost/web/admin/login, the page refresh on and on (I mean always refreshed) I think this is because the condition from Admin_Controller :
Code:
//check apakah user login
        if (!$this->ion_auth->logged_in()) {
            
            //jika belum login kirim ke halaman login admin
            redirect('admin/login', 'refresh');
        }

I want to ask, how to pass it from the condition ?. I try to see from PyroCMS Admin_Controller, there is stuff like :
Code:
// These pages get past permission checks
        $ignored_pages = array('admin/login', 'admin/logout');
            
        // Check if the current page is to be ignored
        $current_page = $this->uri->segment(1, '') . '/' . $this->uri->segment(2, 'index');
        $is_ignored_page = in_array($current_page, $ignored_pages);

but I don't know how to make my own code like that.

help me please, thank yo so much.




Theme © iAndrew 2016 - Forum software by © MyBB