From: jerome Date: Tue, 27 Jan 2009 06:15:36 +0000 (+0000) Subject: web service MDL-12886 implement token system for SOAP + wsdl generation on the fly X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=bb58e708f69d99f3368bcba85ec902edd9efa3b1;p=moodle.git web service MDL-12886 implement token system for SOAP + wsdl generation on the fly --- diff --git a/webservice/rest/locallib.php b/webservice/rest/locallib.php index b93a186b67..43ed78c2ed 100644 --- a/webservice/rest/locallib.php +++ b/webservice/rest/locallib.php @@ -32,7 +32,7 @@ function call_moodle_function ($rest_arguments) { if ($functionname != 'tmp_get_token') { throw new moodle_exception('identifyfirst'); } else { - ///TODO: authentication + token generation need to be implemented + /// TODO: authentication + token generation need to be implemented if (optional_param('username',null,PARAM_ALPHANUM) == 'wsuser' && optional_param('password',null,PARAM_ALPHANUM) == 'wspassword') { return '465465465468468464'; } else { @@ -40,13 +40,13 @@ function call_moodle_function ($rest_arguments) { } } } else { - ///TDO: following function will need to be modified + /// TODO: following function will need to be modified $user = mock_check_token($token); if (empty($user)) { throw new moodle_exception('wrongidentification'); } else { - ///TODO: probably change this + /// TODO: probably change this $USER = $user; } } diff --git a/webservice/soap/generatewsdl.php b/webservice/soap/generatewsdl.php index e7427eb7f1..a6511882fe 100644 --- a/webservice/soap/generatewsdl.php +++ b/webservice/soap/generatewsdl.php @@ -5,8 +5,9 @@ */ require_once('../../config.php'); +$token = optional_param('token',null,PARAM_ALPHANUM); $wsdl_generator = new wsdl_generator(); -$wsdl = $wsdl_generator->generate_wsdl(); +$wsdl = $wsdl_generator->generate_wsdl($token); echo $wsdl; /** @@ -28,11 +29,17 @@ class wsdl_generator { /** * Generate the WSDL for Moodle API * @global $CFG + * @param $token * @return string wsdl xml */ - public function generate_wsdl () { + public function generate_wsdl ($token = null) { global $CFG; + if (empty($token)) { + + return $this->generate_authentication_wsdl(); + } + ///initialize different wsdl part $wsdlmessage = ""; $wsdlporttype = ""; @@ -75,8 +82,11 @@ EOF; ///load the class $classpath = substr($fileapipath,strlen($CFG->dirroot)+1); //remove the dir root + / from the file path - $classpath = substr($classpath,0,strlen($classpath) - 10); //remove /external.php from the classpath + varlog($classpath); + $classpath = substr($classpath,0,strlen($classpath) - 13); //remove /external.php from the classpath + varlog($classpath); $classpath = str_replace('/','_',$classpath); //convert all / into _ + varlog($classpath); $classname = $classpath."_external"; $api = new $classname(); @@ -92,7 +102,7 @@ EOF; $wsdlservice .= << - + @@ -104,13 +114,26 @@ EOF; EOF; - foreach ($description['wsparams'] as $param => $paramtype) { + /* + foreach ($description['params'] as $param => $paramtype) { $wsparamtype = $this->converterMoodleParamIntoWsParam($paramtype); $wsdlmessage .= << EOF; } + foreach ($description['optionalparams'] as $param => $paramtype) { + $wsparamtype = $this->converterMoodleParamIntoWsParam($paramtype); + $wsdlmessage .= << + +EOF; + } * */ + $wsdlmessage .= << + +EOF; + $wsdlmessage .= << @@ -177,11 +200,72 @@ EOF; EOF; - $this->wsdl = $wsdl; return $wsdl; - //$this->writewsdl(); } + private function generate_authentication_wsdl() { + global $CFG; + $wsdl = << + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +EOF; + return $wsdl; + } /** * Retrieve all api.php from Moodle (except the one of the exception list) @@ -253,13 +337,6 @@ EOF; break; } } -/* - private function writewsdl() { - $fp = fopen('moodle.wsdl', 'w'); - fwrite($fp, $this->wsdl); - fclose($fp); - } -*/ } diff --git a/webservice/soap/moodle.wsdl b/webservice/soap/moodle.wsdl deleted file mode 100644 index fcaa5b049b..0000000000 --- a/webservice/soap/moodle.wsdl +++ /dev/null @@ -1,156 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/webservice/soap/server.php b/webservice/soap/server.php index 03371edcfe..c8942d2794 100644 --- a/webservice/soap/server.php +++ b/webservice/soap/server.php @@ -16,18 +16,59 @@ if (empty($CFG->enablewebservices)) { die; } -//retrieve the api name -$classpath = optional_param(classpath,null,PARAM_ALPHA); -require_once(dirname(__FILE__) . '/../../'.$classpath.'/external.php'); +// retrieve the token from the url +// if the token doesn't exist, set a class containing only get_token() +$token = optional_param('token',null,PARAM_ALPHANUM); +if (empty($token)) { + $server = new SoapServer($CFG->wwwroot."/webservice/soap/generatewsdl.php"); + $server->setClass("soap_authentication"); + $server->handle(); +} else { // if token exist, do the authentication here + /// TODO: following function will need to be modified + $user = mock_check_token($token); + if (empty($user)) { + throw new moodle_exception('wrongidentification'); + } else { + /// TODO: probably change this + global $USER; + $USER = $user; + } -//TODO retrieve the token from the url -// if the token doesn't exist create a server with a connection.wsdl -// and set a class containing only get_token() (need to create connection.wsdl and class soapiniconnection) -// if token exist, do the authentication here + //retrieve the api name + $classpath = optional_param(classpath,null,PARAM_ALPHA); + require_once(dirname(__FILE__) . '/../../'.$classpath.'/external.php'); -/// run the server -$server = new SoapServer("moodle.wsdl"); //TODO: need to call the wsdl generation on the fly -$server->setClass($classpath."_external"); //TODO: pass $user as parameter -$server->handle(); + /// run the server + $server = new SoapServer($CFG->wwwroot."/webservice/soap/generatewsdl.php?token=".$token); //TODO: need to call the wsdl generation on the fly + $server->setClass($classpath."_external"); //TODO: pass $user as parameter + $server->handle(); +} + + +function mock_check_token($token) { + //fake test + if ($token == 465465465468468464) { + ///retrieve the user + global $DB; + $user = $DB->get_record('user', array('username'=>'wsuser', 'mnethostid'=>1)); + + if (empty($user)) { + return false; + } + return $user; + } else { + return false; + } +} + +class soap_authentication { + function tmp_get_token($params) { + if ($params['username'] == 'wsuser' && $params['password'] == 'wspassword') { + return '465465465468468464'; + } else { + throw new moodle_exception('wrongusernamepassword'); + } + } +} ?> \ No newline at end of file diff --git a/webservice/soap/testclient/index.php b/webservice/soap/testclient/index.php index b5fe69d105..4b0e00ca7b 100644 --- a/webservice/soap/testclient/index.php +++ b/webservice/soap/testclient/index.php @@ -13,23 +13,34 @@ */ require_once(dirname(__FILE__) . '/../../../config.php'); -//$client = new SoapClient("moodle.wsdl"); - -$client = new SoapClient("../moodle.wsdl",array( +//1. authentication +$client = new SoapClient($CFG->wwwroot."/webservice/soap/generatewsdl.php",array( "trace" => 1, "exceptions" => 0)); try { + $token = $client->tmp_get_token(array('username' => "wsuser", 'password' => "wspassword")); + printLastRequestResponse($client); + +} catch (SoapFault $exception) { + echo $exception; +} + + +//2. test functions +$client = new SoapClient($CFG->wwwroot."/webservice/soap/generatewsdl.php?token=".$token,array( + "trace" => 1, + "exceptions" => 0)); + +try { var_dump($client->tmp_get_users(array('search' => "admin"))); printLastRequestResponse($client); var_dump($client->tmp_create_user(array('username' => "mockuser66",'firstname' => "firstname6",'lastname' => "lastname6",'email' => "mockuser6@mockuser6.com",'password' => "password6"))); printLastRequestResponse($client); var_dump($client->tmp_update_user(array('username' => "mockuser66",'mnethostid' => 1,'newusername' => "mockuser6b",'firstname' => "firstname6b"))); printLastRequestResponse($client); - //not working till authentication implemented - //(remove the has_capaibility of the user/external.php to make it work without authentication) - //var_dump($client->tmp_delete_user(array('username' => "mockuser6b",'mnethostid' => 1))); - //printLastRequestResponse($client); + var_dump($client->tmp_delete_user(array('username' => "mockuser6b",'mnethostid' => 1))); + printLastRequestResponse($client); } catch (SoapFault $exception) { echo $exception; }