CodeIgniter Forums

Full Version: Change the execution flow of controller methods
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]sajjad26[/eluser]
I am building role based access control system.
I am doing something like this

Code:
if($userHasAccess === true){
    // execute the normal code
}else{
    $ci =& get_instance();
    $ci->accessDenied(); // this method is defined in main controller
}

now the problem is if user does not have the access then it will execute the accessDenied function just fine but will also execute the method from the query string which i would like to stop from happening. accessDenied is just used to output a view.

El Forum

[eluser]TheFuzzy0ne[/eluser]
I use this method in my base controller:

Code:
/**
* Displays a 403 error (Forbidden) and halts further execution.
*
* This is used exclusively for members who are logged in, but are denied
* access to a certain page.
*
* @access public
* @return void
*/
function _show_403()
{
    $_error =& load_class('Exceptions', 'core');
    echo $_error->show_error(
            'Access Denied!',
            'You do not have permission to view this page.',
            'error_403',
            403
        );

    $CI =& get_instance();
    $CI->session->sess_write(TRUE);
    exit;
}

El Forum

[eluser]sajjad26[/eluser]
Thanks TheFuzzy0ne it works but i have found another solution by examining the core files.
we can do this
Code:
if($userHasAccess === true){
    // execute the normal code
}else{
    $ci =& get_instance();
    $ci->accessDenied(); // this method is defined in main controller
    $ci->output->_display();
    die();
}

El Forum

[eluser]TheFuzzy0ne[/eluser]
You should not be editing system files. You may break something in the process.

In this instance, the underscore didn't need to be removed. The _display method is a public method, so you should be able to call it directly if you have to.

Code:
$this->output->_display();

El Forum

[eluser]sajjad26[/eluser]
yes you are right i don't have to edit anything it is public indeed.