]> git.mjollnir.org Git - moodle.git/commitdiff
webservice MDL-12886 commit for Petr to work on the last code this weekend (SOAP...
authorjerome <jerome>
Fri, 9 Oct 2009 09:57:07 +0000 (09:57 +0000)
committerjerome <jerome>
Fri, 9 Oct 2009 09:57:07 +0000 (09:57 +0000)
admin/webservices.php
lib/adminlib.php
user/externallib.php
webservice/soap/lib.php
webservice/soap/server.php
webservice/soap/testclient/zend/zend_soap_client.php

index 98da008f250d20fb9ebd9c4a29d6b9a5bea7e13c..8628a53b66a0f82d59f6e05e75c60b81665d3b2d 100644 (file)
@@ -21,6 +21,7 @@ require_once($CFG->dirroot . '/webservice/lib.php');
 
 
 $serviceid      = optional_param('serviceid', '', PARAM_FORMAT);
+$remove         = optional_param('remove', '', PARAM_FORMAT);
 
 $pagename = 'webservicessettings';
 
@@ -36,6 +37,13 @@ if (!confirm_sesskey()) {
     print_error('confirmsesskeybad', '', $baseurl);
 }
 
+if (!empty($remove)) {
+    $functionserviceid         = optional_param('functionserviceid', '', PARAM_FORMAT);
+    if (!empty($functionserviceid)) {
+        $DB->delete_records('external_services_functions',array('id' => $functionserviceid));
+    } 
+}
+
 if (!empty($serviceid)) {
     admin_externalpage_print_header();
 
@@ -50,9 +58,9 @@ if (!empty($serviceid)) {
     }
 
     //display service functions
-    $servicesfunctions = $DB->get_records_sql("SELECT fs.id as id, f.component as component, fs.enabled as enabled, s.name as servicename, s.id as serviceid, f.name as functionname, f.id as functionid
+    $servicesfunctions = $DB->get_records_sql("SELECT fs.id as id, f.component as component, s.name as servicename, s.id as serviceid, f.name as functionname, f.id as functionid
                                     FROM {external_services} s, {external_functions} f, {external_services_functions} fs
-                                   WHERE fs.externalserviceid = s.id AND fs.externalfunctionid = f.id AND s.id = ?", array($serviceid));
+                                   WHERE fs.externalserviceid = s.id AND fs.functionname = f.name AND s.id = ?", array($serviceid));
 
     //save the administrator changes
     $saved      = optional_param('saved', 0, PARAM_NUMBER);
@@ -75,13 +83,15 @@ if (!empty($serviceid)) {
 
     $data = array();
     reset($servicesfunctions);
-    foreach($servicesfunctions as $function) {
-        $checkbox = html_select_option::make_checkbox($function->functionid, $function->enabled, 'functionenabled');
+    foreach($servicesfunctions as $servicefunction) {
+        $checkbox = html_select_option::make_checkbox($servicefunction->functionid, $servicefunction->enabled, 'functionenabled');
         $checkbox->label->add_class('accesshide');
-        $data[] = array($function->functionname, $function->component, $OUTPUT->checkbox($checkbox, $function->functionname));
+        $checkbox->disabled;
+        $checkboxhtml = $OUTPUT->checkbox($checkbox, $servicefunction->functionname);
+        $data[] = array($servicefunction->functionname, $servicefunction->component, "<a href=?sesskey=".sesskey()."&amp;serviceid=".$serviceid."&amp;remove=1&amp;functionserviceid=".$servicefunction->id.">".get_string("removefunctionfromservice","webservice")."</a>");
     }
     $table = new html_table();
-    $table->head  = array(get_string('functionname', 'webservice'), get_string('component', 'webservice'), get_string('enabled', 'webservice'));
+    $table->head  = array(get_string('functionname', 'webservice'), get_string('component', 'webservice'), get_string('actions', 'webservice'));
     $table->size  = array('40%', '40%', '20%');
     $table->align = array('left', 'left', 'left');
     //$table->width = '30%';
index b7a970283bbe8a5acc7ae598172a8eea7c519ce5..86a12107da6a4d4d8dac9cd70beb26ebaf3ef5d5 100644 (file)
@@ -6291,9 +6291,9 @@ class admin_setting_managewsservicelist extends admin_setting {
 
         //retrieve all services
         // $services = $DB->get_records('external_services',null,'custom DESC');
-        $servicesfunctions = $DB->get_records_sql("SELECT  fs.id as id, s.id as serviceid, s.custom as custom, s.name as servicename, s.enabled as serviceenabled, f.name as functionname, fs.enabled as functionenabled
+        $servicesfunctions = $DB->get_records_sql("SELECT  fs.id as id, s.id as serviceid, s.component as component, s.name as servicename, s.enabled as serviceenabled, f.name as functionname
                                     FROM {external_services} s, {external_functions} f, {external_services_functions} fs
-                                   WHERE fs.externalserviceid = s.id AND fs.externalfunctionid = f.id ORDER BY s.name ASC");
+                                   WHERE fs.externalserviceid = s.id AND fs.functionname = f.name ORDER BY s.name ASC");
 
         //create a services array
         $services = array();
index a0b4155b3e31704c16a4765f4f2af0275a807507..22dd9386140648bfadfed0a8d7f4ec661da40ca9 100644 (file)
@@ -29,53 +29,58 @@ require_once("$CFG->libdir/externallib.php");
 class moodle_user_external extends external_api {
 
     public static function create_users_parameters() {
-
-//TODO: the format of the description is not decided yet
-
-        $userpreference = array();
-        $userpreference->name =  array(PARAM_ALPHANUMEXT, 'The name of the preference to set');
-        $userpreference->value =  array(PARAM_RAW, 'The value of the preference');
-
-        $usercustomfields = new object();
-        $usercustomfields->name =  array(PARAM_ALPHANUMEXT, 'The name of the custom field (must exist)');
-        $usercustomfields->value =  array(PARAM_RAW, 'The value of the custom field');
-
-        $usertocreate = new object();
-        $usertocreate->username    = array(PARAM_USERNAME, 'Username policy is defined in Moodle security config', REQUIRED);
-        $usertocreate->password    = array(PARAM_RAW, 'Moodle passwords can consist of any character', REQUIRED);
-        $usertocreate->firstname   = array(PARAM_NOTAGS, 'The first name(s) of the user', REQUIRED);
-        $usertocreate->lastname    = array(PARAM_NOTAGS, 'The family name of the user', REQUIRED);
-        $usertocreate->email       = array(PARAM_EMAIL, 'A valid and unique email address', REQUIRED);
-        $usertocreate->auth        = array(PARAM_AUTH, 'Auth plugins include manual, ldap, imap, etc');
-        $usertocreate->confirmed   = array(PARAM_NUMBER, 'Active user: 1 if confirmed, 0 otherwise');
-        $usertocreate->idnumber    = array(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution');
-        $usertocreate->emailstop   = array(PARAM_NUMBER, 'Email is blocked: 1 is blocked and 0 otherwise');
-        $usertocreate->lang        = array(PARAM_LANG, 'Language code such as "en_utf8", must exist on server');
-        $usertocreate->theme       = array(PARAM_THEME, 'Theme name such as "standard", must exist on server');
-        $usertocreate->timezone    = array(PARAM_ALPHANUMEXT, 'Timezone code such as Australia/Perth, or 99 for default');
-        $usertocreate->mailformat  = array(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc');
-        $usertocreate->description = array(PARAM_TEXT, 'User profile description, as HTML');
-        $usertocreate->city        = array(PARAM_NOTAGS, 'Home city of the user');
-        $usertocreate->country     = array(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ');
-        $usertocreate->preferences = array('multiple' => $userpreference);
-        $usertocreate->custom      = array('multiple' => $usercustomfields);
-
-        $createusersparams = new object();
-        $createusersparams->users  = array('multiple' => $usertocreate);
-
-        return $createusersparams;
+        return new external_function_parameters(
+            array(
+                'users' => new external_multiple_structure(
+                    new external_single_structure(
+                        array(
+                            'username' => new external_value(PARAM_USERNAME, 'Username policy is defined in Moodle security config'),
+                            'password' => new external_value(PARAM_RAW, 'Moodle passwords can consist of any character'),
+                            'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user'),
+                            'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user'),
+                            'email' => new external_value(PARAM_EMAIL, 'A valid and unique email address'),
+                            'auth' => new external_value(PARAM_AUTH, 'Auth plugins include manual, ldap, imap, etc', false),
+                            'confirmed' => new external_value(PARAM_NUMBER, 'Active user: 1 if confirmed, 0 otherwise', false),
+                            'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', false),
+                            'emailstop' => new external_value(PARAM_NUMBER, 'Email is blocked: 1 is blocked and 0 otherwise', false),
+                            'lang' => new external_value(PARAM_LANG, 'Language code such as "en_utf8", must exist on server', false),
+                            'theme' => new external_value(PARAM_THEME, 'Theme name such as "standard", must exist on server', false),
+                            'timezone' => new external_value(PARAM_ALPHANUMEXT, 'Timezone code such as Australia/Perth, or 99 for default', false),
+                            'mailformat' => new external_value(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc', false),
+                            'description' => new external_value(PARAM_TEXT, 'User profile description, as HTML', false),
+                            'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', false),
+                            'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', false),
+                            'preferences' => new external_multiple_structure(
+                                new external_single_structure(
+                                    array(
+                                        'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'),
+                                        'value' => new external_value(PARAM_RAW, 'The value of the preference')
+                                    )
+                                ), 'User preferences', false),
+                            'customfields' => new external_multiple_structure(
+                                new external_single_structure(
+                                    array(
+                                        'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
+                                        'value' => new external_value(PARAM_RAW, 'The value of the custom field')
+                                    )
+                                ), 'User custom fields', false)
+                        )
+                    )
+                )
+            )
+        );
     }
 
     /**
      * Create one or more users
      *
-     * @param $params  An array of users to create.  Each user is defined by $usertocreate above.
+     * @param array $params  An array of users to create.  Each user is defined by $usertocreate above.
      *
-     * @return $return  An array of userids, one for each user that was created
+     * @return array An array of userids, one for each user that was created
      */
-    public static function create_users($params) {
+    public static function create_users($userlist) {
         global $CFG, $DB;
-
+        //varlog('I\'m in create_users()');
         // Ensure the current user is allowed to run this function
         $context = get_context_instance(CONTEXT_SYSTEM);
         require_capability('moodle/user:create', $context);
@@ -87,13 +92,13 @@ class moodle_user_external extends external_api {
         //      2) All required items were sent
         //      3) All data passes clean_param without changes (yes this is strict)
         // If any problems are found then exceptions are thrown with helpful error messages
-        $params = self::validate_parameters(self::create_users_parameters(), $params);
+        $params = self::validate_parameters(self::create_users_parameters(), array('users'=>$userlist));
 
 
         // Perform further checks and build up a clean array of user data
         // Nothing is actually performed until the whole dataset is checked
         $users = array();
-        foreach ($params['users'] as $user) {
+        foreach ($userlist as $user) {
 
             // Empty or no auth is assumed to be manual
             if (empty($user['auth'])) {
index 12c1cfcbb60698caba015ebd5b1f6507d95226f8..26e64295ce958e2ff564ff0aa8a4f3b7eb226057 100644 (file)
@@ -48,9 +48,12 @@ final class soap_server extends webservice_server {
         if (empty($enable)) {
             die;
         }
-        global $CFG;
-        include_once "Zend/Loader.php";
-        Zend_Loader::registerAutoload();
+        global $CFG, $DB;
+        //include_once "Zend/Loader/Autoloader.php";
+        //Zend_Loader_Autoloader::autoload('Zend_Loader');
+
+        include_once "Zend/Soap/Server.php";
+        include_once "Zend/Soap/AutoDiscover.php";
 
         // retrieve the token from the url
         // if the token doesn't exist, set a class containing only get_token()
@@ -85,8 +88,7 @@ final class soap_server extends webservice_server {
                 $autodiscover->setClass('ws_authentication');
                 $autodiscover->handle();
             } else {
-
-                $soap = new Zend_Soap_Server($CFG->wwwroot."/webservice/soap/server.php?wsdl"); // this current file here
+                $soap = new Zend_Soap_Server($CFG->wwwroot."/webservice/soap/server.php?wsdl=true"); // this current file here
                 
                 $soap->registerFaultException('moodle_exception');
                             
@@ -96,6 +98,7 @@ final class soap_server extends webservice_server {
         } else { // if token exist, do the authentication here
             /// TODO: following function will need to be modified
             $user = webservice_lib::mock_check_token($token);
+          //  varlog($user);
             if (empty($user)) {
                 throw new moodle_exception('wrongidentification');
             } else {
@@ -103,22 +106,39 @@ final class soap_server extends webservice_server {
                 global $USER;
                 $USER = $user;
             }
-            //retrieve the api name
-            if (empty($classpath)) {
-                $classpath = optional_param('classpath',null,PARAM_ALPHANUM);
-            }
-            require_once(dirname(__FILE__) . '/../../'.$classpath.'/external.php');
+
+            //load the service functions that the user can access
+                $sql = 'SELECT f.id, f.classname, f.classpath, f.methodname FROM {external_functions} f, {external_services} s, {external_services_functions} sf, {external_services_users} su
+                                 WHERE s.enabled = 1 AND f.id = sf.id AND sf.externalserviceid = su.externalserviceid AND s.id = su.externalserviceid AND su.userid = ?';
+                $functions = $DB->get_records_sql($sql,array($USER->id));
+                $classlist = array(); //key is the classname, value is the class path
+                foreach($functions as $function) {
+                    $classlist[$function->classname] = $function->classpath;
+                }
+                //varlog('List of services that '.$USER->username.' can access:');
+                //varlog($classlist);
+
             /// run the server
             if(isset($_GET['wsdl'])) {
                 $autodiscover = new Zend_Soap_AutoDiscover();
 
                 //this is a hack, because there is a bug in Zend framework (http://framework.zend.com/issues/browse/ZF-5736)
-                $autodiscover->setUri($CFG->wwwroot."/webservice/soap/server.php/".$token."/".$classpath);
-                $autodiscover->setClass($classpath."_external");
+                $autodiscover->setUri($CFG->wwwroot."/webservice/soap/server.php/".$token);
+
+                //we loading all class contaning the requested function
+                foreach ($classlist as $classname => $classpath) {
+                     require_once($CFG->dirroot."/".$classpath);
+                     $autodiscover->setClass($classname);
+                }
+
+                //$autodiscover->setClass($classpath."_external");
                 $autodiscover->handle();
             } else {
-                $soap = new Zend_Soap_Server($CFG->wwwroot."/webservice/soap/server.php?token=".$token."&classpath=".$classpath."&wsdl"); // this current file here
-                $soap->setClass($classpath."_external");
+                $soap = new Zend_Soap_Server($CFG->wwwroot."/webservice/soap/server.php?token=".$token."&wsdl"); // this current file here
+                 foreach ($classlist as $classname => $classpath) {
+                     require_once($CFG->dirroot."/".$classpath);
+                     $soap->setClass($classname);
+                }
                 $soap->registerFaultException('moodle_exception');
                 $soap->handle();
             }
index caee02c6c1809d6cf34cc3e72fc7b5e1d4675932..c2afd845b4ae0da7a9e30454b2f8d72874ccd7f1 100644 (file)
@@ -29,7 +29,7 @@
  */
 require_once(dirname(__FILE__) . '/../../config.php');
 require_once('lib.php');
-
+ini_set("soap.wsdl_cache_enabled", "0"); //must not cache wsdl
 if (empty($CFG->enablewebservices)) {
     die;
 }
index cc8d7c77808ebc5f41c42d98f0623f66d71d5e04..b15461dd6e7ee42a4c39e1954f786b048b654cb7 100644 (file)
@@ -32,9 +32,8 @@ require_once('../../../../config.php');
 require_once('../../lib.php');
 
 
-
-include "Zend/Loader.php";
-Zend_Loader::registerAutoload();
+ini_set("soap.wsdl_cache_enabled", "0");
+include_once "Zend/Soap/Client.php";
 
 $PAGE->set_course($COURSE);
 $PAGE->set_url('webservice/soap/testclient/zend/zend_soap_client.php');
@@ -71,7 +70,7 @@ if (!webservice_lib::display_webservices_availability("soap")) {
 
 /// authenticate => get a conversation token from the server
 /// You need a wsuser/wspassword user in the remote Moodle
-$client = new Zend_Soap_Client($CFG->wwwroot."/webservice/soap/server.php?wsdl");
+$client = new Zend_Soap_Client($CFG->wwwroot."/webservice/soap/server.php?wsdl=true");
 try {
 
     $params = new stdClass();
@@ -104,77 +103,78 @@ try {
 
 
 /// build the Zend SOAP client from the remote WSDL
-$client = new Zend_Soap_Client($CFG->wwwroot."/webservice/soap/server.php?token=".$token."&classpath=user&wsdl");
+$client = new Zend_Soap_Client($CFG->wwwroot."/webservice/soap/server.php?token=".$token."&wsdl=true");
 print "<br><br><strong>You are accessing the WSDL: </strong>";
 print "<br><br>".$CFG->wwwroot."/webservice/soap/server.php?token=".$token."&classpath=user&wsdl<br>";
 
 /// Get any user with string "admin"
-print "<br><br><strong>Get users:</strong>";
-print "<pre>\n";
-try {
-    $params = new stdClass();
-    $params->search = "admin";
-    var_dump($client->get_users($params));
-} catch (exception $exception) {
-    print $exception;
-    print "<br><br><strong>An exception occured: \n</strong>";
-    printLastRequestResponse($client);
-}
-print "</pre>";
+//print "<br><br><strong>Get users:</strong>";
+//print "<pre>\n";
+//try {
+//    $params = new stdClass();
+//    $params->search = "admin";
+//    var_dump($client->get_users($params));
+//} catch (exception $exception) {
+//    print $exception;
+//    print "<br><br><strong>An exception occured: \n</strong>";
+//    printLastRequestResponse($client);
+//}
+//print "</pre>";
 
 /// Create a user with "mockuser66" username
 print "<br><br><strong>Create user:</strong>";
 print "<pre>\n";
 try {
-
-    $user = new stdClass();
-    $user->password = "password6";
-    $user->email = "mockuser6@mockuser6.com";
-    $user->username = "mockuser66";
-    $user->firstname = "firstname6";
-    $user->lastname = "lastname6";
+    $user = array();
+    $user['password'] = "password6";
+    $user['email'] = "mockuser6@mockuser6.com";
+    $user['username'] = "mockuser66";
+    $user['firstname'] = "firstname6";
+    $user['lastname'] = "lastname6";
     $params = new stdClass();
     $params->users = array($user);
-    var_dump($client->create_users($params));
+    $userlist = array();
+    $userlist[] = $user;
+    var_dump($client->create_users($userlist));
 } catch (exception $exception) {
     print $exception;
     print "<br><br><strong>An exception occured: \n</strong>";
-    printLastRequestResponse($client);
+    //printLastRequestResponse($client);
 }
 print "</pre>";
 
-/// Update this user
-print "<br><br><strong>Update user:</strong>";
-print "<pre>\n";
-try {
-    $usertoupdate = new stdClass();
-    $usertoupdate->email = "mockuser6@mockuser6.com";
-    $usertoupdate->username = "mockuser66";
-    $usertoupdate->newusername = 'mockuser6b';
-    $usertoupdate->firstname = "firstname6b";
-    $params = new stdClass();
-    $params->users = array($usertoupdate);
-    var_dump($client->update_users($params));
-} catch (exception $exception) {
-    print $exception;
-    print "<br><br><strong>An exception occured: \n</strong>";
-    printLastRequestResponse($client);
-}
-print "</pre>";
-
-/// Delete this user
-print "<br><br><strong>Delete user:</strong>";
-print "<pre>\n";
-try {
-    $params = new stdClass();
-    $params->usernames = array("mockuser6b");
-    var_dump($client->delete_users($params));
-} catch (exception $exception) {
-    print $exception;
-    print "<br><br><strong>An exception occured: \n</strong>";
-    printLastRequestResponse($client);
-}
-print "</pre>";
+///// Update this user
+//print "<br><br><strong>Update user:</strong>";
+//print "<pre>\n";
+//try {
+//    $usertoupdate = new stdClass();
+//    $usertoupdate->email = "mockuser6@mockuser6.com";
+//    $usertoupdate->username = "mockuser66";
+//    $usertoupdate->newusername = 'mockuser6b';
+//    $usertoupdate->firstname = "firstname6b";
+//    $params = new stdClass();
+//    $params->users = array($usertoupdate);
+//    var_dump($client->update_users($params));
+//} catch (exception $exception) {
+//    print $exception;
+//    print "<br><br><strong>An exception occured: \n</strong>";
+//    printLastRequestResponse($client);
+//}
+//print "</pre>";
+//
+///// Delete this user
+//print "<br><br><strong>Delete user:</strong>";
+//print "<pre>\n";
+//try {
+//    $params = new stdClass();
+//    $params->usernames = array("mockuser6b");
+//    var_dump($client->delete_users($params));
+//} catch (exception $exception) {
+//    print $exception;
+//    print "<br><br><strong>An exception occured: \n</strong>";
+//    printLastRequestResponse($client);
+//}
+//print "</pre>";
 
 /// Display Moodle page footer
 echo $OUTPUT->footer();