Reputation: 15269
I've created a function which withdraw the user data from the database:
function user()
{
$user_id = $this->ci->session->userdata('user_id');
if (!is_null($user = $this->ci->users->get_user_by_id($user_id, TRUE))) {
return $this->ci->users->get_user($user_id); //model
}
return FALSE;
}
I'm using it like this in any controller:
$this->tank_auth->user()->field
.
Its not convenient for me, so what I need to do, to get the user data like this?
$this->user->field
or $this->tank_auth->user->field
.
My second question, regarding the above function is this function:
function is_admin()
{
if ( $this->user()->who )
{
return $this->user()->who === "ADMIN";
}
return FALSE;
}
It does throw error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: libraries/Tank_auth.php
Line Number: 335
(Line 335 is if ( $this->user()->who )
).
Why does this happend? Both functions are located in the tank_auth's library, so its supposed to work fine, but it doesnt.
I'll appreciate any help attempt.
Upvotes: 2
Views: 2203
Reputation: 23
If you add this method to the users.php model:
function get_user_profile_by_id($user_id){
$this->db->where('user_id', $user_id);
$query = $this->db->get($this->profile_table_name);
if ($query->num_rows() == 1) return $query->row();
return NULL;
}
you can then for example add a varchar field userclass in the userprofile table and check if a user belongs to a certain class with:
$profile = $this->users->get_user_profile_by_id($this->tank_auth->get_user_id());
if ($profile->userclass = "ADMIN") {
...
}
Hope that helps.
Upvotes: 0