A while ago I wrote a small add-on for CodeIgniter which provides Roles based URL level Authorization. I named it Kibishii Security, cos “kibishii” means “strict” in Japanese.
It uses CI hooks to provide authorization at the URL-level, but it provides no authentication mechanism. You need to use it in conjunction with some kind of authentication system (at the time I was using Tank Auth) which I liked back then.
But Kibishii was designed to be roles-based, that is it enforces “only people with a certain role can access this URL”. However Tank Auth provides no concept of groups or roles, so to use it with Kibishii I had to invent this user-role relationship by implementing it myself, or by hard-coding it into my config (both less than ideal).
So today I will run you through another popular authentication system for CI: Ion Auth. It provides “groups” which we can use in Kibishii as our “roles”.
This guide will start a new CI project from scratch, but theres no reason it wouldnt work on an existing system. If you already have Ion Auth you can skip the next set up steps.
Set up Ion Auth
Follow these steps (should be pretty straight forward)
- First up, download CI from here: http://codeigniter.com/,
- then download Ion Auth from here: https://github.com/benedmunds/CodeIgniter-Ion-Auth
- Copy the Ion Auth files into the appropriate folders in your new CI installation.
- Then run the SQL script that comes with Ion Auth.
- edit your /application/config/database.php to point to your database.
- open up /application/config/config.php and change $config['encryption_key'] to be not empty, something like: $config['encryption_key'] = ‘mySecretKey’;
Now goto http://localhost/index.php/auth/login
If you get a login page, then you have installed Ion Auth.
Using Kibishii
- Enable hooks by editing your application/config/config.php and set $config['enable_hooks'] = TRUE;
- Add kibishii as a hook in application/config/hooks.php by adding this:
$hook['post_controller_constructor'] = array( 'class' => 'kibishii_hook', 'function' => 'check_permissions', 'filename' => 'kibishii_hook.php', 'filepath' => 'hooks' );
- download kibishii
- copy the following files into the corresponding folders in your CI installation.
- hooks/kibishii_hook.php – the main kibishii code
- config/kibishii.php – config for kibishii
- libraries/kibishii_ion.php – an adapter required for Ion Auth groups (it seems ion auth doesnt store its groups in the session anymore, so i wrote this to get the groups from model)
- open up application/config/kibishii.php and set the acl rules like this:
$config['kibishii_acl'] = array( '/^auth$/' => 'admin', '/^auth\/index/' => 'admin', '/^auth\/change_password/' => 'members' );
Basically it protects the admin list for only admins, and the change password for only logged in users.
- I’ll upload a pre-made config to github soon, but you need to change it to look like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /* * This is the access control list. * It should be an associative array of '{uri}' => '{role required}' * */ $config['kibishii_acl'] = array( '/^auth$/' => 'admin', '/^auth\/change_password/' => 'members', '/^auth\/index/' => 'admin', ); /* * Set this to TRUE to enter test mode. * * In test mode, the decision process will be display on the screen. * And the result page will not be display (whether permission is grant or not) */ $config['kibishii_test_mode'] = FALSE; // turn off everything $config['kibishii_disabled'] = FALSE; // The url of the login page // This overrides any rules in the ACL and make this url always unprotected. $config['kibishii_login_url'] = 'auth/login'; // set to true if you want a 404 instead of error when access is denied. $config['kibishii_denied_show_404'] = FALSE; // The view to load if access is denied $config['kibishii_denied_view'] = 'denied'; /************************************************** * Authentication Config **************************************************/ $config['kibishii_get_id_from_config'] = FALSE; $config['kibishii_get_id_from_session'] = TRUE; $config['kibishii_get_id_from_class'] = FALSE; // mock user id (used when you set $config['kibishii_get_id_from_config'] = TRUE;) // $config['kibishii_mock_user_id'] = 'user33'; /* * The key name for the userid in session userdata * This should be the * * Some common ones are: * Tank_auth: 'user_id' * Ion_auth: 'email' * */ $config['kibishii_user_id_session_field'] = 'email'; /* * ## Configuration of your Authentication ## * * These things tell kibishii how to find your user's id * * kibishii_authentiation_class = the name of your authentication class * kibishii_authentiation_method = the method to call on your class which returns the * user id of the currently logged in user. * kibishii_authentiation_filename = the file which contains your class * (this is not needed if you autoload your class or * load your class in the controller's constructor) */ // $config['kibishii_authentication_class'] = 'tank_auth'; // $config['kibishii_authentication_method'] = 'get_username'; // $config['kibishii_authentication_filename'] = 'libraries/Tank_auth.php'; /************************************************** * Authorization Config **************************************************/ $config['kibishii_roles_from_config'] = FALSE; $config['kibishii_roles_from_session'] = FALSE; $config['kibishii_roles_from_class'] = TRUE; $config['kibishii_roles_session_field'] = ''; $config['kibishii_roles_class'] = 'kibishii_ion'; $config['kibishii_roles_method'] = 'get_roles'; $config['kibishii_roles_filename'] = 'libraries/kibishii_ion.php'; $config['kibishii_roles_default_role'] = 'EVERYONE'; // mock user roles used if you set $config['kibishii_roles_from_config'] = TRUE; /* $config['kibishii_mock_roles'] = array( 'user11' => array('ROLE_USER', 'ROLE_ADMIN'), 'user22' => array('ROLE_USER', ), ); */ ?>
Testing It Out
- log out if you logged in, then goto /index.php/auth/ you should see an error like “You dont have permission to view this page.”. This means kibishii is working, normally Ion Auth will send you back to the login page, but that is done with an if statement in the controller.
- now goto /index.php/auth/login and login with “admin@admin.com” / “password”, these are ion auth defaults.
- now try again to goto /index.php/auth/ and you should get the user admin page (successful authorization).
- create a new user, then go /index.php/auth/logout , you will see the no permission message again (this is because ion auth redirects to /auth on logout, ill discuss this more later)
- goto /index.php/auth/login and login as the user you just created (i.e. not an admin)
- goto /index.php/auth/ and you should get the no permission message, since you are not an admin.
- goto /index.php/auth/change_password and you should get the no permission message. (this page appears to break in the current ion auth.. ill investigate)