]> git.mjollnir.org Git - moodle.git/commitdiff
web service MDL-12886 implement token system for SOAP + wsdl generation on the fly
authorjerome <jerome>
Tue, 27 Jan 2009 06:15:36 +0000 (06:15 +0000)
committerjerome <jerome>
Tue, 27 Jan 2009 06:15:36 +0000 (06:15 +0000)
webservice/rest/locallib.php
webservice/soap/generatewsdl.php
webservice/soap/moodle.wsdl [deleted file]
webservice/soap/server.php
webservice/soap/testclient/index.php

index b93a186b67ab1f80f318c460f88100a3a2a1de56..43ed78c2ed8759b9ceb36c9c90aa4517e87880b2 100644 (file)
@@ -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;
         }
     }
index e7427eb7f19f6afbfc31825949194b5bf0eee76f..a6511882fe4c2901cb32f982da8e6021317b8b3c 100644 (file)
@@ -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 <type> $CFG
+     * @param <type> $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 .= <<<EOF
         <service name='{$classpath}Service'>
                 <port name='{$classpath}Port' binding='{$classpath}Binding'>
-                    <soap:address location='{$CFG->wwwroot}/webservice/soap/server.php?classpath={$classpath}'/>
+                    <soap:address location='{$CFG->wwwroot}/webservice/soap/server.php?classpath={$classpath}&amp;token={$token}'/>
                 </port>
         </service>
 
@@ -104,13 +114,26 @@ EOF;
         <message name="{$functionname}Request">
 
 EOF;
-            foreach ($description['wsparams'] as $param => $paramtype) {
+                /*
+            foreach ($description['params'] as $param => $paramtype) {
                 $wsparamtype = $this->converterMoodleParamIntoWsParam($paramtype);
                 $wsdlmessage .= <<<EOF
             <part name="{$param}" type="xsd:{$wsparamtype}"/>
 
 EOF;
             }
+            foreach ($description['optionalparams'] as $param => $paramtype) {
+                $wsparamtype = $this->converterMoodleParamIntoWsParam($paramtype);
+                $wsdlmessage .= <<<EOF
+            <part name="{$param}" type="xsd:{$wsparamtype}"/>
+
+EOF;
+            }   * */
+              $wsdlmessage .= <<<EOF
+            <part name="params" type="xsd:object"/>
+
+EOF;
+
              $wsdlmessage .= <<<EOF
         </message>
         <message name="{$functionname}Response">
@@ -177,11 +200,72 @@ EOF;
 
 EOF;
 
-        $this->wsdl =  $wsdl;
         return $wsdl;
-        //$this->writewsdl();
     }
 
+    private function generate_authentication_wsdl() {
+        global $CFG;
+        $wsdl = <<<EOF
+<?xml version ='1.0' encoding ='UTF-8' ?>
+    <definitions name='User'
+                 targetNamespace='http://example.org/User'
+                 xmlns:tns=' http://example.org/User '
+                 xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
+                 xmlns:xsd='http://www.w3.org/2001/XMLSchema'
+                 xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
+                 xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
+                 xmlns='http://schemas.xmlsoap.org/wsdl/'>
+
+        <types>
+            <xsd:schema targetNamespace="http://example.org/User"
+                        xmlns="http://www.w3.org/2001/XMLSchema">
+                <xsd:complexType name="object">
+                </xsd:complexType>
+            </xsd:schema>
+        </types>
+
+        <message name="tmp_get_tokenRequest">
+            <part name="params" type="xsd:object"/>
+        </message>
+        <message name="tmp_get_tokenResponse">
+            <part name="user" type="xsd:object"/>
+        </message>
+
+        <portType name='userPortType'>
+            <operation name='tmp_get_token'>
+                <input message='tns:tmp_get_tokenRequest'/>
+                <output message='tns:tmp_get_tokenResponse'/>
+            </operation>
+        </portType>
+
+        <binding name='userBinding' type='tns:userPortType'>
+            <soap:binding style='rpc'
+                          transport='http://schemas.xmlsoap.org/soap/http'/>
+
+            <operation name='tmp_get_token'>
+                <soap:operation soapAction='urn:xmethods-delayed-quotes#tmp_get_token'/>
+                <input>
+                    <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
+                               encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
+                </input>
+                <output>
+
+                    <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
+                               encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
+                </output>
+           </operation>
+
+        </binding>
+
+        <service name='userService'>
+                <port name='userPort' binding='userBinding'>
+                    <soap:address location='{$CFG->wwwroot}/webservice/soap/server.php'/>
+                </port>
+        </service>
+    </definitions>
+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 (file)
index fcaa5b0..0000000
+++ /dev/null
@@ -1,156 +0,0 @@
-<?xml version ='1.0' encoding ='UTF-8' ?>
-    <definitions name='User'
-                 targetNamespace='http://example.org/User'
-                 xmlns:tns=' http://example.org/User '
-                 xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
-                 xmlns:xsd='http://www.w3.org/2001/XMLSchema'
-                 xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
-                 xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
-                 xmlns='http://schemas.xmlsoap.org/wsdl/'>
-
-        <types>
-            <xsd:schema targetNamespace="http://example.org/User"
-                        xmlns="http://www.w3.org/2001/XMLSchema">
-                <xsd:complexType name="object">
-                </xsd:complexType>
-            </xsd:schema>
-        </types>
-
-        <message name="tmp_get_usersRequest">
-            <part name="params" type="xsd:object"/>
-        </message>
-        <message name="tmp_get_usersResponse">
-            <part name="user" type="xsd:object"/>
-        </message>
-        <message name="tmp_create_userRequest">
-            <part name="params" type="xsd:object"/>
-        </message>
-        <message name="tmp_create_userResponse">
-            <part name="userid" type="xsd:string"/>
-        </message>
-        <message name="tmp_namedparams_get_usersRequest">
-            <part name="search" type="xsd:string"/>
-
-        </message>
-        <message name="tmp_namedparams_get_usersResponse">
-            <part name="user" type="xsd:object"/>
-        </message>
-        <message name="tmp_delete_userRequest">
-             <part name="params" type="xsd:object"/>
-        </message>
-        <message name="tmp_delete_userResponse">
-
-            <part name="result" type="xsd:object"/>
-        </message>
-        <message name="tmp_update_userRequest">
-             <part name="params" type="xsd:object"/>
-        </message>
-        <message name="tmp_update_userResponse">
-
-            <part name="result" type="xsd:object"/>
-        </message>
-        <portType name='userPortType'>
-            <operation name='tmp_get_users'>
-                <input message='tns:tmp_get_usersRequest'/>
-                <output message='tns:tmp_get_usersResponse'/>
-            </operation>
-
-            <operation name='tmp_create_user'>
-
-                <input message='tns:tmp_create_userRequest'/>
-                <output message='tns:tmp_create_userResponse'/>
-            </operation>
-
-            <operation name='tmp_namedparams_get_users'>
-                <input message='tns:tmp_namedparams_get_usersRequest'/>
-                <output message='tns:tmp_namedparams_get_usersResponse'/>
-            </operation>
-
-            <operation name='tmp_delete_user'>
-
-                <input message='tns:tmp_delete_userRequest'/>
-                <output message='tns:tmp_delete_userResponse'/>
-            </operation>
-
-            <operation name='tmp_update_user'>
-                <input message='tns:tmp_update_userRequest'/>
-                <output message='tns:tmp_update_userResponse'/>
-            </operation>
-        </portType>
-
-        <binding name='userBinding' type='tns:userPortType'>
-            <soap:binding style='rpc'
-                          transport='http://schemas.xmlsoap.org/soap/http'/>
-
-            <operation name='tmp_get_users'>
-                <soap:operation soapAction='urn:xmethods-delayed-quotes#tmp_get_users'/>
-                <input>
-                    <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
-                               encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
-                </input>
-                <output>
-
-                    <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
-                               encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
-                </output>
-           </operation>
-
-            <operation name='tmp_create_user'>
-                <soap:operation soapAction='urn:xmethods-delayed-quotes#tmp_create_user'/>
-                <input>
-                    <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
-                               encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
-                </input>
-
-                <output>
-                    <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
-                               encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
-                </output>
-           </operation>
-
-            <operation name='tmp_namedparams_get_users'>
-                <soap:operation soapAction='urn:xmethods-delayed-quotes#tmp_namedparams_get_users'/>
-                <input>
-                    <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
-                               encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
-
-                </input>
-                <output>
-                    <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
-                               encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
-                </output>
-           </operation>
-
-            <operation name='tmp_delete_user'>
-                <soap:operation soapAction='urn:xmethods-delayed-quotes#tmp_delete_user'/>
-                <input>
-
-                    <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
-                               encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
-                </input>
-                <output>
-                    <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
-                               encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
-                </output>
-           </operation>
-
-            <operation name='tmp_update_user'>
-                <soap:operation soapAction='urn:xmethods-delayed-quotes#tmp_update_user'/>
-
-                <input>
-                    <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
-                               encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
-                </input>
-                <output>
-                    <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
-                               encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
-                </output>
-           </operation>
-        </binding>
-
-        <service name='userService'>
-                <port name='userPort' binding='userBinding'>
-                    <soap:address location='http://jerome.moodle.com/Moodle_HEAD/moodle/webservice/soap/server.php?classpath=user'/>
-                </port>
-        </service>
-    </definitions>
index 03371edcfeb402f266018382f733c1a1d64f2c8b..c8942d279490ce6995d43ebff4841c3f308f0fe6 100644 (file)
@@ -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
index b5fe69d1054c95a9515ee5f3ef1d44adfacd73cb..4b0e00ca7b307af6cbc2b3485680b8ca3eb9bbc4 100644 (file)
  */
 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;
 }