Welcome Guest, Not a member yet? Register   Sign In
Trying to create a global current_user whose properties such as email, username, and role can be accessed in any control
#1

[eluser]johnmerlino[/eluser]
Hey all,

I want to have a current_user method whose properties can be accessed in any controller. Basically, it would have a method called current_user which returns the user session in the variable and I would like to access attributes of the object. But this seems tedious:

Code:
$this->session->set_userdata('id',$id);
                    $this->session->set_userdata('email',$email);
                    $this->session->set_userdata('role',$role);

If I do this:

Code:
$this->session->set_userdata('user',$user);
where $user is the returned record from database, can I then pass it as an argument to set_userdata, and then reassign that value to a variable like this and return it:

Code:
public function current_user(){
             $user = $this->session->userdata('user');
            return $user;
        }
and then be able to access properties like this:
Code:
return Authorization::check($this->current_user(),$action);

class Authorization extends Model {
public static function check($user,$action){
$role = $user.role;
...

To make this happen, I presume I would need to use either inheritance with MY_Controller or load the current_user method in a library to make it accessible to all controllers. But my question is can I access properties of the returned record when assigning the userdata of session to a variable like in the example above?

Thanks for response.
#2

[eluser]InsiteFX[/eluser]
You need to read this article by Phil Sturgeon!

CodeIgniter Base Classes: Keeping it DRY

InsiteFX
#3

[eluser]johnmerlino[/eluser]
Thanks for response and link, but my bigger problem is storing the object itself in the session. If I try to store the object itself, then I get a session bug with object storage as reported here: http://ellislab.com/forums/viewthread/95690/.

So what am I to do? Well, I have to store each property value of object like this and this occurs during the sign in process:

Code:
$this->session->set_userdata('id',$current_user->id);
                    $this->session->set_userdata('role',$current_user->role_id);
                    $this->session->set_userdata('email',$current_user->email);

What I now want to do is to pass the object itself into my current_user function in my application controller which is inherited by all controllers:

Code:
public function current_user(){
  $current_user = $current_user ? $current_user : $this->session->userdata('current_user');
  return $current_user;
        }

But as you seen, I could not store the object itself in set_userdata. So I'm not sure how I can return the object from my current_user method using userdata() so I could globally in all the controllers and views, do something like this:

Code:
if(current_user->role == '8') echo "authorized";

PS I dont think this will do what I want:

Code:
public function current_user(){
            $current_user = array();
            $current_user['id'] = $this->session->userdata('id',$current_user->id);
            $current_user['role'] = $this->session->userdata('role',$current_user->role_id);
            $current_user['email'] = $this->session->userdata('email',$current_user->email);
            return $current_user;
        }

thanks for response.
#4

[eluser]bubbafoley[/eluser]
you can cache the user object and just save the id in the session.

Code:
public function current_user() {
    $this->load->driver('cache', array('adapter'=>'file'));
    $user_id = $this->session->userdata('user_id');
    if( ! $user = $this->cache->get('user_'.$user_id))
    {
        $user = get_user_from_database($user_id);
        $this->cache->save('user_'.$user_id);
    }
    return $user;
}

OR you can json_encode the object before you save it to the session and then decode it after.

Code:
$this->session->set_userdata('user', json_encode($current_user));

Code:
public function current_user() {
    return json_decode($this->session->userdata('user'));
}
#5

[eluser]InsiteFX[/eluser]
The problem is the serialize and unserilize methods in the session library,
some one post this last year I think with a fix. Do a search for it.

InsiteFX
#6

[eluser]johnmerlino[/eluser]
Code:
$this->session->set_userdata('user', json_encode($current_user));

[code]

That worked. Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB