开发者

Joomla JUser getinstance Questions

开发者 https://www.devze.com 2022-12-31 16:25 出处:网络
I am attempti开发者_开发知识库ng to make an authentication plugin. JUser::getInstance() takes one input, and it is supposed to be the id. Is there any way to get an instance of a User using some other

I am attempti开发者_开发知识库ng to make an authentication plugin. JUser::getInstance() takes one input, and it is supposed to be the id. Is there any way to get an instance of a User using some other indentifier? such as username, email etc.


Probably there isnt any such method. But yes if you are sure that username or email are unique then you can modify your file user.php in libraries/joomla/user/ and add a method there.

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


Since Joomla's own authentication is done by checking the user's username (and password of course), it has to be unique. And yes you can do something like what @Rixius suggested.

Here's my version:

    // 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);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消