2013-02-28 95 views
2

我正在嘗試使用codeigniter構建Web應用程序。我已將Ion Auth安裝爲我的身份驗證模型。在另一個控制器中對codeigniter使用離子認證認證

默認Auth.php控制器對用戶進行認證,並建立會話。

<?php defined('BASEPATH') OR exit('No direct script access allowed'); 

class Auth extends CI_Controller { 



    function __construct() 
    { 
     parent::__construct(); 
     $this->load->library('ion_auth'); 
     $this->load->library('session'); 
     $this->load->library('form_validation'); 
     $this->load->helper('url'); 

     $data['title']="Login Page"; 
     $this->load->view("view_site_header",$data); 

     // Load MongoDB library instead of native db driver if required 
     $this->config->item('use_mongodb', 'ion_auth') ? 
     $this->load->library('mongo_db') : 

     $this->load->database();  

     $this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth')); 
    } 

    //redirect if needed, otherwise display the user list 
    function index() 
    { 
     // if not logged in - go to home page 
     if (!$this->ion_auth->logged_in()) 
     { 
      //redirect them to the login page 
      redirect('auth/login', 'refresh'); 
     } 
     // if user is an admin go to this page 
     elseif ($this->ion_auth->is_admin()) 
     { 
      // if an admin, go to admin area 

      //set the flash data error message if there is one 
      $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message'); 

      //list the users 
      $this->data['users'] = $this->ion_auth->users()->result(); 
      foreach ($this->data['users'] as $k => $user) 
      { 
       $this->data['users'][$k]->groups = $this->ion_auth->get_users_groups($user->id)->result(); 
      } 

      $this->_render_page('auth/view_users', $this->data);     
     } else 
    { 
     //redirect them to the default home page 
     $data['title']="IMS Home Page"; 
     $this->load->view("generic/view_site_header",$data); 
     $this->load->view("generic/view_generic_nav"); 
     $this->load->view("generic/view_content_generic"); 
     $this->load->view("view_site_footer"); 
    } 
} 

我想要做的是爲我的應用程序邏輯創建一個新的控制器,並讓身份驗證控制器進行身份驗證。

我如何可以使用身份驗證控制,以確保我的用戶的訪問我的新控制器時,在登錄?此外,我需要將激情信息提供給新控制器。

我的新的控制器,master_data具有下面的代碼:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Masterdata extends CI_Controller{ 

    function index() 
    { 
      $data['title']="Master Data Home Page"; 
      $this->load->view("master_data/view_master_data_header",$data); 
      $this->load->view("master_data/view_master_data_nav"); 
      $this->load->view("master_data/view_content_master_data_home"); 
      $this->load->view("master_data/view_master_data_footer"); 

      echo $this->session->userdata('username'); 



    } 
} 

顯然echo $this->session->userdata('username');不工作作爲新的控制器沒有在auth控制器會話的知識。

任何幫助表示讚賞一如既往。

此致

回答

9

首先自動載入ion_auth庫。 如果妳只是要檢查,如果用戶登錄,就檢查它在每一個控制器的構造函數ü加載

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

    if (!$this->ion_auth->logged_in()) { 
     // redirect to login view 
    } 
} 

如果妳碰巧有多個組,U可以建立內部應用程序/內核的新型控制器/ MY_controller.This控制器將檢查用戶是否已登錄。您可以擴展此基本控制器以創建新控制器。有關此問題的非常好的解釋由David john提供。請檢查此link

+1

對於簡單的安全控制器來說,這是一個比通過MY_Controller進行擴展更簡單的方法。 – jaredstenquist 2013-03-21 03:08:25

2

明顯回波$這 - >會話級>用戶數據( '用戶名');不起作用,因爲新控制器不知道授權控制器會話。

呃......如果會話庫被加載,那麼是......控制器調用它將能夠訪問會話變量$ username。

我們處理這個問題的方法是在應用程序/核心目錄下創建像MY_Controller一個新的控制器父類。這個類加載公共庫/包(如session和ion_auth)。您也可以自動加載庫和幫助程序。

由於ion_auth存儲所有會話中的變種的用戶配置文件數據的,所有你需要(在後續的,非認證)的網頁是會話LIB檢索有關登錄用戶的會話數據。

你真的應該檢查他們的身份驗證狀態,雖然,優雅地失敗:

if (!$this->ion_auth->logged_in()) { 
    // echo a login link 
} else { 
    // echo session var for username 
} 

類似的東西...

+0

謝謝jcorry,應用程序/核心目錄中的MY_Controller,是否自動處理這段代碼而不自動加載?所以如果我將我的離子認證控制器語法移到MY_Controller,這會自動在所有將來的控制器中使用嗎?這是一種標準做法嗎?感謝您的知識和時間。 – Smudger 2013-02-28 20:00:02

+1

你對MY_Controller的處理是用你自己的方式重載CI控制器類。因此,'class MY_Controller extends CI_Controller'可以讓你繼承CI控制器類的所有功能,但是可以對從MY_Controller繼承的單個控制器進行更改。你可以在這裏閱讀這個原理:[鏈接](http://ellislab.com/codeigniter/user-guide/general/core_classes.html) – jcorry 2013-03-04 15:09:30

0

jcorrys辦法應該工作。另一種方法(這將使您的整個應用程序具有更大的靈活性)使用模塊化佈局 - https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

您必須做一些小小的調整才能使其與ion認證一起正常工作,但遵循說明在這個問題上爲我工作:Using Ion Auth as a separate module in the HMVC structure(看看git hub上的離子認證的分支 - 我認爲有人可能已經爲你做了)

這種方法將允許你訪問任何控制器中的任何方法在應用程序中的任何地方使用這種語法(即使從視圖如果需要):modules::run('module/controller/method', $params);

這將ESSENTIA允許您將現有的離子認證控制器開發成用戶管理控制器,您可以從您創建的任何其他控制器(好和幹)訪問該用戶管理控制器。