• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Security interceptor" and interceptors in general

#1
[eluser]eddieconnecti[/eluser]
Tried the search, but couldn´t find anything helpful about his:

In some other frameworks i worked before, i used interceptors to check whether the user is logged in or not. I used them to handle pre-requests, like calling models for the navigation tree before the view is called and any other controller methods are called.

So what i am looking for is a way to call methods each time a new request starts. In this way i want to call a model, recieve an array of navigation items, push this to the $data object of the controller so the view knows which navigation items to show.

On the same way i want to check on every request if the user is logged in and if he has permissions for the current action. If not, redirect the user to another page.

The advantage is to not write the hole code in every controllers method but having the code in external classes which are automatically called.

Maybe it is possible to do this with hooks or callbacks, but i like the way to have a class for security functions (like a security interceptor) and a class for navigational purposes.

Thanks!

#2
[eluser]mddd[/eluser]
Build a library and put your call to that library in the constructor of your controllers. It will be called in every request.
If you don't want to write a line in each controller, you can extend the default controller (make it a MY_controller) and put the call in there.
Then you let every controller extend from MY_Controller instead of Controller.

#3
[eluser]eddieconnecti[/eluser]
Thanks! Sounds like extending the controller class could do the job.

Quote:Build a library and put your call to that library in the constructor of your controllers

I don´t know what you mean? How can that be done? Maybe you could post a snippet of code?

#4
[eluser]eddieconnecti[/eluser]
The following code shows how i now tried to extend the base ci controller class:
Code:
class MY_Controller extends Controller {

    function MY_Controller()
    {
        parent::Controller();    
        
    }
    
    function index()
    {
        $data['menu'] = $this->Menu_model->get();
    }
}

The file is named MY_Controller.php and is located in system/libraries/

In the controllers i use following syntax:

Code:
<?php
class Users extends MY_Controller {
(...)
}
The problem is: $menu is never accessible in view/navigation/menu.php. Maybe the reason is, i use nested views:

Code:
<?php $this->load->view( 'default/header' ); ?>
<?php $this->load->view( 'navigation/menu' ); ?>
<?php $this->load->view( $layout_body ); ?>
<?php $this->load->view( 'default/footer' ); ?>

Maybe i have to bubble the $data variables into subviews? Something like
Code:
...
<?php $this->load->view( 'navigation/menu', $menu ); ?>
...

#5
[eluser]mddd[/eluser]
Once variables are loaded in one view, they are available in all subsequent views. So that is not the problem.

I understand that $data['menu'] is something you want loaded in every controller. That's why you put it in the MY_Controller class.
But you probably have a index() method in your controllers. Like Users has an index method, right? That means it overwrites the index() you made in MY_Controller. And so, the $data['menu'] is still not set! If you want $data to "automatically" contain information, you would have to do something like
Code:
class MY_Controller extends Controller {

    var $data = array();
    
    function MY_Controller()
    {
        parent::Controller();    
        $this->data['menu'] = $this->Menu_model->get();
    }
}

And in the Controller:
Code:
class Users extends MY_Controller {

    function Users()
    {
        parent::MY_Controller();
    }

    function index()
    {
       // here you add the other stuff you need in this controller/view
       $this->data['userdata'] = get_some_user_data_to_show();
       $this->load->view('myview', $this->data);
  
       // now, you have $menu in the view (which was set in the MY_Controller)
       // and also $userdata which you set specifically in this controller
    }
}

#6
[eluser]eddieconnecti[/eluser]
That´s perfect! Thanks a lot!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.