0

I'm experiencing a problem using the HMVC implementation of Codeigniter, with modules.

I created an authentication module, called autenticacion, which, for obvious reasons, in order to check session validity and expiration, is always used within the MX_Controller, this way:

Modules::run('autenticacion/logout', $user, true);

or

Modules::run('autenticacion/update', $user);

depending on certain conditions.

Ever since I added this athentication implementation, the normal access to other modules was broken.

Now if I try to, for instance, access

www.domain.com/items

having that I coded this module files:

modules/items/controllers/Items.php (this extends MX_Controller) , and the view:

modules/items/views/items_view.php

The view won't be found despite it's loaded within the Items controller.

If I print out $this in the Items constructor, the MY_Loader instance displays this property:

[_module:protected] => autenticacion

I understand this means the items module is not being loaded within the loader, despite I can reach its controller. The other module (autenticacion) seems that is messing it all.

How could I fix this?

EDIT:

This is what I changed in MX_Controller in order to handle session checks and updates:

<?

class MX_Controller 
{
    public $autoload = array();

    public function __construct() 
    {       
        // Validación de la autenticación/caducidad de la sesión
        $this->_validar_sesion();   

        // Original actions
        //....
    }


    //... method __get()


    /**
     * Checks whether the user has not yet created a valid authenticated user session, or if it has expired.
     * In both cases, it redirects to the authentication page, deleting the expired session if it was already created.
     */
    private function _validar_sesion()
    {
        if (stristr(get_class($this), 'autenticacion')) return; 

        require_once APPPATH . 'third_party/usuarios/Usuario.php';
        $this->load->model('autenticacion/autenticacion_model');

        // Get user instance from session
        $usuario = unserialize($this->session->userdata('usuario'));

        // No authenticated session yet: redirecting to the authentication page
        if (!stristr(current_url(), 'autenticacion') && ! $this->session->logged_in) {

            $this->load->library('uri');
            $uri = $this->uri->uri_string();

            redirect(base_url() . 'autenticacion' . ($uri ? '?redirect=' . $uri : ''));
        }

        // There is already an authenticated session, and the request is not coming from the authentication page
        elseif (!stristr(current_url(), 'autenticacion')) {

            // Check session expiration, in which case, we do logout
            if ($this->autenticacion_model->sesion_caducada($usuario, config_item('sess_companyname_expiration'))) {

                // This will delete the session from DB, destroy it, and redirect to the authentication page
                Modules::run('autenticacion/logout', $usuario, true);

            }
            // Session has not expired yet: we update the session timestamp in DB to extend the expiration time
            else {

                Modules::run('autenticacion/update', $usuario);

            }           

        }

    }
}
Luis Martin
  • 910
  • 3
  • 14
  • 31
luis.ap.uyen
  • 1,314
  • 1
  • 11
  • 29
  • have you try like this Modules::run('module/controller/method', $param, $...); – Sanjay Sinalkar Nov 19 '15 at 15:04
  • Yes, it doesn't work. The fact is that I access the authentication module that way (Modules::run(...)), without the http interface, but I do need to access the items module through http and the router, which leads to the Items controller. But the MY_Loader instance within Items has "autenticacion" as its module. That's why it will not find the required view. – luis.ap.uyen Nov 19 '15 at 15:14
  • This may be a bit off-topic, sorry, but I can't help thinking that using a whole HMVC module to check for session validity is a bit of an overkill. Why not implement those checks in a model or a library? Or even better, create a separate class for modules, let it extend MX_Controller, load auth checking model in its constructor, then inherit all other modules needing protection off of it. Login/logout will stay in a separate module, of course. – Vaviloff Nov 19 '15 at 18:37
  • @Vaviloff: well I'm using a whole HMVC module to let it have its own controller to process the authentication requests, models (one for users, another for permissions), and the view is the authentication page. – Luis Martin Nov 19 '15 at 19:24
  • Since I need to check session expiration, and I'm using my own database session solution, because the native from CI doesn't perform update on each request to extend session validity, I need to use the authentication module on every request, including those where I also need to use the items module. Once the former is loaded, it seems to prevent the latter from being loaded too. – Luis Martin Nov 19 '15 at 19:29
  • Sorry now I'm commenting with my personal account. The other one is the one I use at work. We're the same person. – Luis Martin Nov 19 '15 at 19:33
  • There is also a possibility that when HMVC module is called from another controller and it can't load one of its files (misspelled view path being the reason, for example) then Loader will throw 404 error. – Vaviloff Nov 20 '15 at 00:52
  • @Vaviloff: That's not my case. The view is correctly spelled (path and name). In fact, if I comment out the call within MX_Controller to the method that does the session check tasks, the URLs using the other module work again. I'm starting to think on a quick solution like moving the "items" module files to the main controllers/models/views directories and discard it as a module :( – luis.ap.uyen Nov 20 '15 at 08:13
  • " In fact, if I comment out the call within MX_Controller to the method that does the session check tasks, the URLs using the other module work again" Then definitely there's something wrong with that class. What if you called it from http? Like http://www.domain.com/autenticacion/update/ Is there an error? – Vaviloff Nov 20 '15 at 09:08
  • @Vaviloff: I've just edited the post so you can see what I did in MX_Controller – Luis Martin Nov 22 '15 at 18:49
  • Thanks. A couple of suggestions to embetter your code. In one place you do « redirect(base_url() . 'autenticacion' . ($uri ? '?redirect=' . $uri : '')); » to send user to login page. So why can't you later do « redirect(base_url() . 'autenicacation/logout') » instead of using Module::Run? That would be more consistent. And then, instead of calling Modules::run('autenticacion/update') you should move update code to autenticacion_model and do autenticacion_model->update($usario) from MX_Controller. (And also from autenticacion/update method if needed. – Vaviloff Nov 23 '15 at 07:36
  • I will do that as soon as possible. Since I had to apply a quick solution, I dumped all the items module into the main controllers/models/views folders. But I will try that at home. Thank you! – luis.ap.uyen Nov 25 '15 at 07:52

0 Answers0