mbischof / LimeSurvey

The most popular FOSS online survey tool on the web.
https://www.limesurvey.org
Other
0 stars 0 forks source link

RemoteControl Refactoring? #6

Open mbischof opened 4 years ago

mbischof commented 4 years ago
//application.config.config.php
return array(
    'components' => array(
        'remoteController' => array(
            'class' => 'application.helpers.remotecontrol.RemoteController'
        )
    )
);
//application.controllers.admin.remotecontrol.php
$oHandler = Yii::app()->remoteController;
//application.helpers.remotecontrol.RemoteController.php
class RemoteController extends CApplicationComponent
{
    public function init()
    {
        $behaviors = array(
            'get_session_key'       => 'application.helpers.remotecontrol.behaviors.GetSessionKeyBehavior',
            'release_session_key'   => 'application.helpers.remotecontrol.behaviors.ReleaseSessionKeyBehavior',
        );

        $this->behaviors = array_merge($behaviors, $this->behaviors);
        parent::init();
    }
}
//application.helpers.remotecontrol.behaviors.GetSessionKeyBehavior.php
Yii::import('application.helpers.remotecontrol.behaviors.RemoteControlBehavior');

class GetSessionKeyBehavior extends RemoteControlBehavior
{
    public function get_session_key($username, $password, $plugin = 'Authdb')
    {
        $username = (string) $username;
        $password = (string) $password;
        $loginResult = $this->_doLogin($username, $password, $plugin);

        if ($loginResult === true) {
            $this->_jumpStartSession($username);
            $sSessionKey = Yii::app()->securityManager->generateRandomString(32);
            $session = new Session;
            $session->id = $sSessionKey;
            $session->expire = time() + (int) Yii::app()->getConfig('iSessionExpirationTime',ini_get('session.gc_maxlifetime'));
            $session->data = $username;
            $session->save();
            return $sSessionKey;
        }

        if (is_string($loginResult)) {
            return array('status' => $loginResult);
        }

        return array('status' => 'Invalid user name or password');
    }
}
//application.controllers.TestController
class TestController extends CController
{
    public function actionIndex()
    {

        Yii::import('application.libraries.jsonRPCClient');
        $client = new jsonRPCClient( 'http://limesurvey.localhost/index.php/admin/remotecontrol');
        $sessionKey = $client->get_session_key('user', 'password');

        var_dump($sessionKey); exit;
    }
}
mbischof commented 4 years ago
//application.config.config.php
return array(
    'components' => array(
        'remoteController' => array(
            'class' => 'application.helpers.remotecontrol.RemoteController',
            'behaviors' => array(
                'get_session_key' => 'application.components.remotecontrol.MyGetSessionKeyBehavior',
                'my_own_rpc_method' => 'application.components.remotecontrol.MyOwnRPCMethod'
            ),
        )
    )
);
olleharstedt commented 4 years ago

Me and Denis have already discussed this issue slightly: https://bugs.limesurvey.org/view.php?id=15817

Shnoulle commented 4 years ago

About extensible, we can extend https://gitlab.com/SondagesPro/coreAndTools/twigExtendByPlugins/-/blob/master/twigExtendByPlugins.php#L27