代码之家  ›  专栏  ›  技术社区  ›  Rixius

Joomla JUser getinstance问题

  •  0
  • Rixius  · 技术社区  · 16 年前

    我正在尝试制作一个身份验证插件。 JUser::getInstance() 获取一个输入,它应该是id。有没有任何方法可以使用其他标识符获取用户的实例?例如用户名、电子邮件等。

    2 回复  |  直到 16 年前
        1
  •  1
  •   Matteo Italia    13 年前

    可能没有这样的方法。但如果您确定用户名或电子邮件是唯一的,那么您可以修改libraries/joomla/user/中的user.php文件,并在其中添加一个方法。

    getInstanceByEmail($email)
    {
         $query = "select id from jos_users where email=".email;
         // use the code to get the id;
        return getInstance($id);
    } // this is just a sample code of how it can be achieved
    
        2
  •  0
  •   500 Server error    13 年前

    因为Joomla自己的身份验证是通过检查用户的用户名(当然还有密码)来完成的,所以它必须是唯一的。是的,你可以像@Rixius建议的那样做。

        // Get a database object
        $db     = JFactory::getDbo();
        $query  = $db->getQuery(true);
    
        $query->select('id, password');
        $query->from('#__users');
        $query->where('username=' . $db->Quote($credentials['username']));
    
        $db->setQuery($query);
        $result = $db->loadObject();
        $user = JFactory::getUser();
    
        if ($result)
        {
            $user = JUser::getInstance($result->id);
        }