【问题标题】:Tank Auth - getting the user info from the databaseTank Auth - 从数据库中获取用户信息
【发布时间】:2012-03-02 11:55:48
【问题描述】:

我创建了一个从数据库中提取用户数据的函数:

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;
}

我在任何控制器中都这样使用它:

$this->tank_auth->user()->field.

这对我来说不方便,所以我需要做什么,才能获取这样的用户数据?

$this->user->field$this->tank_auth->user->field

我的第二个问题,关于上述功能是这个功能:

function is_admin()
{
    if ( $this->user()->who )
    {
        return $this->user()->who === "ADMIN";
    }
    return FALSE;
}

确实会抛出错误:

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: libraries/Tank_auth.php

Line Number: 335

(第 335 行是if ( $this->user()->who ))。

为什么会这样?这两个函数都位于 tank_auth 的库中,因此它应该可以正常工作,但它没有。

我将不胜感激任何帮助尝试。

【问题讨论】:

  • 您可以将 $this->tank_auth->user() 设置为这样的变量。 $user = $this->tank_auth->user();这将允许您访问它的值,例如 $user->field。你想用 is_admin 方法完成什么?
  • @DougWallace 我想检查$this->tank_auth->user()->who 是否是管理员。

标签: class codeigniter tankauth


【解决方案1】:

如果将此方法添加到 users.php 模型中:

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;
}

然后,您可以在 userprofile 表中添加一个 varchar 字段 userclass 并检查用户是否属于某个类:

$profile = $this->users->get_user_profile_by_id($this->tank_auth->get_user_id());
if ($profile->userclass = "ADMIN") {
...
}

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2014-07-19
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    • 2019-10-22
    • 1970-01-01
    相关资源
    最近更新 更多