From 35b9a80aa6321e95d4883fd4cb15efc782b9a161 Mon Sep 17 00:00:00 2001 From: jerome Date: Fri, 9 Oct 2009 09:57:07 +0000 Subject: [PATCH] webservice MDL-12886 commit for Petr to work on the last code this weekend (SOAP code very not clean) --- admin/webservices.php | 22 +++- lib/adminlib.php | 4 +- user/externallib.php | 87 ++++++------- webservice/soap/lib.php | 48 +++++--- webservice/soap/server.php | 2 +- .../soap/testclient/zend/zend_soap_client.php | 116 +++++++++--------- 6 files changed, 157 insertions(+), 122 deletions(-) diff --git a/admin/webservices.php b/admin/webservices.php index 98da008f25..8628a53b66 100644 --- a/admin/webservices.php +++ b/admin/webservices.php @@ -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, "id.">".get_string("removefunctionfromservice","webservice").""); } $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%'; diff --git a/lib/adminlib.php b/lib/adminlib.php index b7a970283b..86a12107da 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -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(); diff --git a/user/externallib.php b/user/externallib.php index a0b4155b3e..22dd938614 100644 --- a/user/externallib.php +++ b/user/externallib.php @@ -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'])) { diff --git a/webservice/soap/lib.php b/webservice/soap/lib.php index 12c1cfcbb6..26e64295ce 100644 --- a/webservice/soap/lib.php +++ b/webservice/soap/lib.php @@ -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(); } diff --git a/webservice/soap/server.php b/webservice/soap/server.php index caee02c6c1..c2afd845b4 100644 --- a/webservice/soap/server.php +++ b/webservice/soap/server.php @@ -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; } diff --git a/webservice/soap/testclient/zend/zend_soap_client.php b/webservice/soap/testclient/zend/zend_soap_client.php index cc8d7c7780..b15461dd6e 100644 --- a/webservice/soap/testclient/zend/zend_soap_client.php +++ b/webservice/soap/testclient/zend/zend_soap_client.php @@ -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 "

You are accessing the WSDL: "; print "

".$CFG->wwwroot."/webservice/soap/server.php?token=".$token."&classpath=user&wsdl
"; /// Get any user with string "admin" -print "

Get users:"; -print "
\n";
-try {
-    $params = new stdClass();
-    $params->search = "admin";
-    var_dump($client->get_users($params));
-} catch (exception $exception) {
-    print $exception;
-    print "

An exception occured: \n"; - printLastRequestResponse($client); -} -print "
"; +//print "

Get users:"; +//print "
\n";
+//try {
+//    $params = new stdClass();
+//    $params->search = "admin";
+//    var_dump($client->get_users($params));
+//} catch (exception $exception) {
+//    print $exception;
+//    print "

An exception occured: \n"; +// printLastRequestResponse($client); +//} +//print "
"; /// Create a user with "mockuser66" username print "

Create user:"; print "
\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 "

An exception occured: \n"; - printLastRequestResponse($client); + //printLastRequestResponse($client); } print "
"; -/// Update this user -print "

Update user:"; -print "
\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 "

An exception occured: \n"; - printLastRequestResponse($client); -} -print "
"; - -/// Delete this user -print "

Delete user:"; -print "
\n";
-try {
-    $params = new stdClass();
-    $params->usernames = array("mockuser6b");
-    var_dump($client->delete_users($params));
-} catch (exception $exception) {
-    print $exception;
-    print "

An exception occured: \n"; - printLastRequestResponse($client); -} -print "
"; +///// Update this user +//print "

Update user:"; +//print "
\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 "

An exception occured: \n"; +// printLastRequestResponse($client); +//} +//print "
"; +// +///// Delete this user +//print "

Delete user:"; +//print "
\n";
+//try {
+//    $params = new stdClass();
+//    $params->usernames = array("mockuser6b");
+//    var_dump($client->delete_users($params));
+//} catch (exception $exception) {
+//    print $exception;
+//    print "

An exception occured: \n"; +// printLastRequestResponse($client); +//} +//print "
"; /// Display Moodle page footer echo $OUTPUT->footer(); -- 2.39.5