2017-02-08 10 views
1

私は管理者とユーザーのページがあるウェブサイトを構築しています。私はユーザーとしてログインしていてもURLを介して管理ページにアクセスできるという問題があります。ログインページで妥当性チェックを行っていますが、すでにユーザーまたは管理者としてログインしている場合は、すべてのページにアクセスできます。ページを自分の役割だけに制限したい。ここに私のコードです。Codeigniter管理ページをURLで制限する

MY_Controller:

function __construct() 
{ 
    parent::__construct(); 
    $this->is_logged_in(); 

    $session_admin = $this->session->userdata('isAdmin'); 
    $method = $this->router->fetch_method(); 

    if(($session_admin == FALSE) && $method != 'login') 
    { 
     $this->session->set_flashdata('message', 'You need to login to access this location'); 
     redirect('user_home'); 
    } 
    else 
    { 
     redirect('admin_ticketing/new_tickets'); 
    } 
} 

function is_logged_in() 
{ 
    $is_logged_in = $this->session->userdata('is_logged_in'); 

    if(!isset($is_logged_in) || $is_logged_in != true) { 
     redirect('login'); 
     die(); 
    } 
} 

モデル:

function validate() 
{ 
    $this->db->where('username', $this->input->post('username')); 
    $this->db->where('password', $this->input->post('password')); 
    $query = $this->db->get('accounts'); 
    $result = $query->row(); 

    if($query->num_rows() == 1) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

function check_role() 
{ 
    $this->db->where('username', $this->input->post('username')); 
    $this->db->where('password', $this->input->post('password')); 
    $this->db->where('role', 1); 
    $query = $this->db->get('accounts'); 
    $result = $query->row(); 

    if($query->num_rows() == 1) 
    { 
     $data = array(
      'userid' => $result->userid, 
      'username' => $result->username, 
      'password' => $result->password, 
      'firstname' => $result->firstname, 
      'lastname' => $result->lastname, 
      'email' => $result->email, 
      'address' => $result->address, 
      'monthly_dues' => $result->monthly_dues, 
      'arrears' => $result->arrears, 
      'isAdmin' => true, 
      'contactnum' => $result->contactnum, 
      'role' => $result->role, 
      'is_logged_in' => true 
     ); 
     $this->session->set_userdata($data); 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

function check_user() 
{ 
    $this->db->where('username', $this->input->post('username')); 
    $this->db->where('password', $this->input->post('password')); 
    $this->db->where('role', 0); 
    $query = $this->db->get('accounts'); 
    $result = $query->row(); 

    if($query->num_rows() == 1) 
    { 
     $data = array(
      'userid' => $result->userid, 
      'username' => $result->username, 
      'password' => $result->password, 
      'firstname' => $result->firstname, 
      'lastname' => $result->lastname, 
      'email' => $result->email, 
      'address' => $result->address, 
      'monthly_dues' => $result->monthly_dues, 
      'arrears' => $result->arrears, 
      'isAdmin' => false, 
      'contactnum' => $result->contactnum, 
      'role' => $result->role, 
      'is_logged_in' => true 
     ); 
     $this->session->set_userdata($data); 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

function check_active() 
{ 
    $this->db->where('username', $this->input->post('username')); 
    $this->db->where('password', $this->input->post('password')); 
    $this->db->where('isActive', 1); 
    $query = $this->db->get('accounts'); 
    $result = $query->row(); 

    if($query->num_rows() == 1) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

コントローラー:

function validate_login() 
{ 
    $this->load->model('model_accounts'); 
    $valid = $this->model_accounts->validate(); 
    $isAdmin = $this->model_accounts->check_role(); 
    $isUser = $this->model_accounts->check_user(); 
    $isActive = $this->model_accounts->check_active(); 

    if($valid && $isAdmin && $isActive) // Active Admin 
    { 
     redirect('admin_ticketing/new_tickets'); 
    } 
    else if($valid && $isActive && $isUser) // Active User 
    { 
     redirect('user_home'); 
    } 
    else if(($valid && $isAdmin) && $isActive == false) //Deactivated Admin 
    { 
     redirect('login/admindeact'); 
    } 
    else if($valid && ($isActive && $isAdmin) == false) //Deactivated User 
    { 
     redirect('login/userdeact'); 
    } 
    else if($valid == false) //Invalid Account 
    { 
     $data['message'] = "Sorry, the username and password you entered did not match our records. Please double-check and try again. "; 
     $this->template->load('template', 'view_login', $data); 
    } 
} 
+0

http://stackoverflow.com/questions/31261007/not-giving-access-to-certain-method-in-controller-、このコードを参照してください、あなたのコントローラでこれを確認することができますセッション時に設定されていないとき/ 31316000#31316000 – shafiq

答えて

2

あなたは

function __construct() 
{ 
    parent::__construct(); 

    $session_admin = $this->session->userdata('admin'); //getting admin session 
    $method = $this->router->fetch_method(); // get the current method 

    if(empty($session_admin) && $method != 'login'){ // check for admin session and methos is login 
     $this->session->set_flashdata('message', 'You need to login to access this location'); 
     redirect('admin/users/login'); 
    } 
} 
+0

これをMY_Controllerに置くことができますので、これをすべてのコントローラーで繰り返す必要はありませんか? – coderszx

+0

これはどのように実装しますか?これをMY_Controllerに入れても、ページが何度もリダイレクトされ、エラーが返されます。 – coderszx

+0

上記コードを – coderszx

0

あなたは単に、ログイン時に管理者とフロントユーザーのロールを設定したい場合、セッション値 'is_admin'を設定する

それから($ is_admin)がそういうことを確認できます。

+0

だから私はすべてのコントローラとその機能についてチェックする必要がありますか? – coderszx

+0

すべてのコントローラのコンストラクタにチェックを追加するだけです。 –

関連する問題