[eluser]simshaun[/eluser]
What I'm building is a Users lib that spans across both the frontend and backend.
That class was in no way, shape, or form, complete by any means. It was merely an example of what I'm doing that allows myself the use of my models in combination with sessions to set vars in the config.
In the end I plan on doing something like what you posted above xwero, except a bit different.
The get_user_level method must be called on each page load for what I'm building.
If a user is demoted in any way, it must be nearly instantaneous, otherwise the user can still do things freely until the next time he logs in.
Here's the general concept of what I'm gonna do.
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Class is auto-loaded by CI.
*/
class Auth {
var $CI;
function Auth()
{
$this->init();
}
/**
* Called by construct.
*
*/
function init()
{
$this->_get_ci();
$this->CI->load->model('UsersModel');
if ($this->is_logged_in()){
$user_level = $this->CI->UsersModel->get_user_level($this->CI->session->userdata('user_id'));
$this->CI->config->set_item('cur_user_level', $user_level);
}
}
/**
* Returns TRUE/FALSE if user is logged in.
*
*/
function is_logged_in()
{
if ($this->CI->session->userdata('user_id') !== FALSE && ctype_digit($this->CI->session->userdata('user_id'))){
return TRUE;
}
## TODO: Implement cookie "remember-me" support via a token.
return FALSE;
}
/**
* Called upon login.
*
*/
function do_login()
{
// sample data for now
$sess_data = array(
'user_id' => 1
,'username' => 'jdoe'
,'user_fname' => 'John'
,'user_tz' => 'UM5'
);
$this->CI->session->set_userdata($sess_data);
}
function _get_ci()
{
$this->CI =& get_instance();
}
}
// end of file