Trying to create a global current_user whose properties such as email, username, and role can be accessed in any control |
[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); If I do this: Code: $this->session->set_userdata('user',$user); Code: public function current_user(){ Code: return Authorization::check($this->current_user(),$action); 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.
[eluser]InsiteFX[/eluser]
You need to read this article by Phil Sturgeon! CodeIgniter Base Classes: Keeping it DRY InsiteFX
[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); 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(){ 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(){ thanks for response.
[eluser]bubbafoley[/eluser]
you can cache the user object and just save the id in the session. Code: public function current_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() {
[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
[eluser]johnmerlino[/eluser]
Code: $this->session->set_userdata('user', json_encode($current_user)); [code] That worked. Thanks. |
Welcome Guest, Not a member yet? Register Sign In |