Blog Archive

Friday 4 May 2012

Add site-wide authentication to FUEL CMS

FUEL CMS has a simple method of forcing a site-wide password within fuel.php, where you are able to set a "dev_password". However, if you want to add a 3rd party site wide authentication system, you can do so much like you can with a regular Codeigniter install, by editing MY_Controller.

For this example, I'll use Ben Edmunds' ion_auth. It’s very easy to add – unzip the files into the application folder, and setup the SQL in your database.

Open up application/core/MY_Controller.php. In FUEL v0.93, this has no class definition yet, so add the basic class and ion auth requirements:
 
   1:  class MY_Controller extends CI_Controller 
   2:  {
   3:      function __construct()
   4:      {
   5:          parent::__construct();
   6:          $this->load->library('ion_auth');
   7:          $this->load->helper('url');
   8:          if(!$this->ion_auth->logged_in())
   9:          {
  10:                  //redirect them to the login page
  11:                  redirect('auth/login', 'refresh');
  12:          }
  13:      }
  14:  }



Once saved, that should trigger a redirect to ion auth’s login page with any controller inheriting from MY_Controller. If you need to remove that functionality, return MY_Controller.php back to its original state!

Now you need to edit every controller that extended CI_Controller, and change that class name to “MY_Controller” (which itself extends CI_Controller).

That accounts for custom controllers, but not for some core FUEL CMS controllers. The class name swap will also need to occur in page_router.php, Fuel_base_controller.php and Blog_base_controller.php to if you are using that module. I’m prepared to be wrong, but I think that covers all the important controllers, at least for triggering ion auth login.

No comments:

Post a Comment

My top artists