Welcome Guest, Not a member yet? Register   Sign In
Sessions/Authorization
#1

[eluser]conord[/eluser]
Firstly - a disclaimer: I'm brand-new to CI (started yesterday) and fairly new to MVC development from scratch.

I'm currently looking to port my current application over to CI. It's a warehouse-management system that requires a user to be logged in for every aspect of the site. There is also some very simple role management/checking a well. I looked into BitAuth, but was a little overkill for my needs. So I figured I'd just take what I needed an roll my own.

Everything seems to be working, except that I'm not able to access the session data from the library I created. I am auto-loading the library and have the constructor call the is_logged_in() method. Problem is, even though I'm getting the instance and loading session:
[code
]if($CI =& get_instance())
{
$this->input = $CI->input;
$this->load = $CI->load;
$this->config = $CI->config;
$this->lang = $CI->lang;
$this->load->database();
$this->db = $CI->db;
$this->load->library('session');

$this->session = $CI->session;
return;
}[/code]
The session->userdata is always empty, as if nothing has been set. This is after I log in and confirm that the userdata was set in the users controller.

So, is it possible to do an auto-check like this? Or do I simply need to add the check in each controller that I check?

TIA
#2

[eluser]CroNiX[/eluser]
Use a base controller. Here's a good read on that. Basically you create a special controller, called MY_Controller, which CI uses to extend the default controller class. This is where the actual auth would be preformed. Then each of your regular controllers that need the auth, extend MY_Controller. Auth is automatically performed and you don't have to tell each controller to do it for you. It is also a good place to put common variables/functions that are used by other controllers.

BTW, I'm not sure why you are creating an instance of CI, and then assigning each CI class to your own class. Just use the CI class.
Code:
class new_class {

  // Define CI so you can use in all methods
  protected $CI;

  public function __construct()
  {
    // Get the instance and assign to $CI
    $this->CI =& get_instance();
  }

  public function some_function()
  {
    // Grab session variable from the CI instance we defined above
    $userdata = $this->CI->session->userdata($data_field_name);

    //or retrieve all data for user
    $userdata = $this->CI->session->all_userdata();
  }
}
Also, if you are autoloading your database and sessions, there is no need to load them again. Keep it simple and don't over complicate things.
#3

[eluser]conord[/eluser]
Thanks for the response. The base controller sound perfect and I'm reading through that article now.

I agree about keeping it simple. Even though I was auto-loading the db and session libraries, I wasn't able to use them in the Auth library until I re-loaded them. Otherwise I'd get a 'calling method on a non-obj' error. I just figured that was due to calling the Auth library from auto-load and not from inside a controller. Though, now that I think about it, it might also be the order in how I auto-loaded the libraries - sessions is after Auth.

Either way, looks like I have a solution. Cheers!
#4

[eluser]CroNiX[/eluser]
Yes, order is important, because it will execute the __construct() of each one as it loads them. So db and sessions would need to be loaded before auth, since auth relies on them.




Theme © iAndrew 2016 - Forum software by © MyBB