Joomla用户登录,注册,开始会话

时间:2019-05-17 12:27:14

标签: php session joomla

我正在准备joomla插件/ api以将网站与mobileapp连接。我不能使用Cookie,我只需要按要求进行操作即可。

因此要登录,我可以转到http://example.net/?user=aaa&pass=bbb,它创建会话并返回令牌。

要转到用户个人资料,我可以转到:http://example.net/profile?token=8asd7g ...,如果令牌与数据库中的会话ID匹配,则它将在Joomla框架中设置会话cookie(在php端)

我应使用哪个事件:

  1. 检查令牌并维护会话
  2. 检查登录名,用户和登录/创建会话
  3. 检查登录名,用户和注册用户

第二个问题是如何:

  1. 设置$session->set('userid',$user->id);是否足够?
  2. 我创建了伪造的$ response,$app->triggerEvent('onUserLogin', array((array) $response, array('action' => 'core.login.admin')));够了吗?

1 个答案:

答案 0 :(得分:0)

如果有人需要,这里的解决方案几乎完成了。它允许用户通过用户登录并通过url或request标头形式传递信息,它允许基于令牌而不是cookie的网站访问受限部分,它允许将参数从标头传递给JInput-> get。

<?php
/**
 * @package     API
 * @subpackage  System.sittetokenlogin
 *
 */

defined('_JEXEC') or die('Unauthorized Access');

jimport('joomla.filesystem.file');


class PlgSystemSittetokenlogin extends JPlugin
{
    public function __construct(&$subject, $config)
    {
        parent::__construct($subject, $config);
    }


    public function onUserAuthenticate()
    {
        //die('onUserAuthenticate');

    }
    public function onUserLogin()
    {//wykonuje się
        //die('onUserLogin');

    }

    public function onUserLogout()
    {//wykonuje się
        //die('onUserLogout');

    }   


    public function onAfterInitialise()
    {
        //wstępne ustawienie obiektów
        $app = JFactory::getApplication();
        if ($app->isClient('administrator')) return;
        $input = JFactory::getApplication()->input;
        $headers = getallheaders ();
        $db = JFactory::getDbo();

        //pobranie danych z rządania
        $loginToken = $headers['logintoken']; if(!$loginToken) $loginToken = $input->get->get('logintoken', '', 'STRING');
        $suser = $headers['suser']; if(!$suser) $suser = $input->get->get('suser', '', 'STRING');
        $spass = $headers['spass']; if(!$spass) $spass = $input->get->get('spass', '', 'STRING');

        if ($loginToken) // logowanie na bazie tokenu
            {
            JPluginHelper::importPlugin('user');
            $sesja = $db->setQuery('SELECT * FROM `#__session` WHERE `session_id`='.$db->quote($loginToken).' LIMIT 1')->loadObject();      
            $user = $db->setQuery('SELECT * FROM `#__users` WHERE `id`='.$db->quote($sesja->userid).' LIMIT 1')->loadObject();      

            $response = new JAuthenticationResponse();
            $response->type          = 'Joomla';
            $response->email         = $user->email;
            $response->fullname      = $user->name;
            $response->username      = $user->username;
            $response->password      = '';
            $response->status        = JAuthentication::STATUS_SUCCESS; 
            $response->error_message = null;
            //print_r($response);
            $app->triggerEvent('onUserLogin', array((array) $response, array('action' => 'core.login.site')));

            //$testuser = JFactory::getUser(); die(print_r($testuser,true));  
            }

        elseif ($suser && $spass) //logowanie na bazie loginu i hasła
            {
            $error = $app->login([
                'username' => $suser,
                'password' => $spass,
            ]);
            $user = JFactory::getUser();
            if ($user->id>0) die(JFactory::getSession()->getId());
            else die('login_error');
            }


        //przekazywanie parametrów
        $option = $headers['option']; $input->set('option',$option);
        $view = $headers['view']; $input->set('view',$view);
        $id = $headers['id']; $input->set('id',$id);
        $catid = $headers['catid']; $input->set('catid',$catid);
        $Itemid = $headers['Itemid']; $input->set('Itemid',$Itemid);
        $tmpl = $headers['tmpl'];  $input->set('tmpl',$tmpl);

        //$input->set('option','com_guru');
        //$input->set('view','gurupcategs');


    }
}