2

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.

Cyclone
  • 14,839
  • 23
  • 82
  • 114
  • You could set $this->tank_auth->user() as a variable like this. $user = $this->tank_auth->user(); which would allow you to access it's values like so $user->field. What are you trying to accomplish with the is_admin method? – Doug Wallace Mar 02 '12 at 17:29
  • @DougWallace I want to check if `$this->tank_auth->user()->who` is ADMIN or not. – Cyclone Mar 02 '12 at 17:37

1 Answers1

0

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.