[eluser]mscahill[/eluser]
I'm developing an authenticated intranet application for our University Relations division. It is absolutely essential that the entire application be secure.
I've already created an authentication scheme that checks credentials against the database, but I'm having trouble creating a hook function that forces authentication. My hook so far is as follows:
Code:
<?php
class Auth {
function check()
{
$CI =& get_instance();
if (strpos($_SERVER['REQUEST_URI'],'account')===FALSE) {
switch (get_cookie('user_pass')) {
case TRUE:
//Double check that the cookie has the right user name and password
$username = $CI->encrypt->decode(get_cookie('user_name'));
$password = $CI->encrypt->decode(get_cookie('user_pass'));
$query = $CI->db->query("SELECT user_pass FROM ci_users WHERE user_name='$username';");
//Username and password incorrect, so force login
$row = $query->row();
if ($query->num_rows()==0) {
delete_cookie("user_name","urweb.stetson.edu");
delete_cookie("user_pass","urweb.stetson.edu");
header("Location: " . site_url('account/login'));
}
if ($password!=$row->user_pass) {
delete_cookie("user_name","urweb.stetson.edu");
delete_cookie("user_pass","urweb.stetson.edu");
header("Location: " . site_url('account/login'));
}
break;
case FALSE:
//No cookies found, so force them to log in
header("Location: " . site_url('account/login'));
break;
}
}
}
?>
The problem that I'm having is that if I make it a pre_system hook then I don't have access to the CI instance, and as such cannot use CI helpers and libraries. However, if I make it a post_controller_construct hook, then authentication is not required on cached pages. Some of these pages take up to a minute to load, so caching is necessary. The caching process is done on a nightly basis automatically for pages that have taken longer than 10 seconds to load in the previous week (as logged by another hook function).
Is there any way I can force authentication on cached pages and still use CI helpers?