From 9d9af8fa8c6a47991ccb9726cff7c5524dfd9862 Mon Sep 17 00:00:00 2001 From: jerome Date: Thu, 22 Jan 2009 04:58:50 +0000 Subject: [PATCH] web service MDL-12886 rename wsapi.php as external.php, function tmp_delete_user don't use api.php anymore and include capability checking --- lib/moodleexternal.php | 47 ++++++ user/external.php | 112 +++++++++++++ webservice/rest/lib.php | 13 +- webservice/soap/generatewsdl.php | 12 +- webservice/soap/moodle.wsdl | 262 ++++++++++++++++++------------- webservice/soap/server.php | 4 +- 6 files changed, 321 insertions(+), 129 deletions(-) create mode 100644 lib/moodleexternal.php create mode 100644 user/external.php diff --git a/lib/moodleexternal.php b/lib/moodleexternal.php new file mode 100644 index 0000000000..c3802aac07 --- /dev/null +++ b/lib/moodleexternal.php @@ -0,0 +1,47 @@ +descriptions = array(); + } + + /** + * + * @param $functionname + */ + public function get_function_webservice_description($functionname) { + if (key_exists($functionname, $this->descriptions)) { + return $this->descriptions[$functionname]; + } + else { + return false; + } + } + + /** + * + * @return + */ + public function get_descriptions() { + return $this->descriptions; + } + +} +?> diff --git a/user/external.php b/user/external.php new file mode 100644 index 0000000000..d7f08b791f --- /dev/null +++ b/user/external.php @@ -0,0 +1,112 @@ +descriptions = array(); + ///The desciption of the web service + /// + ///'wsparams' and 'return' are used to described the web services to the end user (can build WSDL file from these information) + /// + ///Note: web services param names have not importance. However 'paramorder' must match the function params order. + ///And all web services param names defined into 'wsparams' should be included into 'paramorder' (otherwise they will not be used) + $this->descriptions['tmp_create_user'] = array( 'wsparams' => array('username'=> PARAM_RAW, 'firstname'=> PARAM_RAW, 'lastname'=> PARAM_RAW, 'email'=> PARAM_RAW, 'password'=> PARAM_RAW), + 'return' => array('userid' => PARAM_RAW)); + + $this->descriptions['tmp_get_users'] = array( 'wsparams' => array('search'=> PARAM_ALPHANUM), + 'return' => array('user' => array('id' => PARAM_RAW, 'auth' => PARAM_RAW, 'confirmed' => PARAM_RAW, 'username' => PARAM_RAW, 'idnumber' => PARAM_RAW, + 'firstname' => PARAM_RAW, 'lastname' => PARAM_RAW, 'email' => PARAM_RAW, 'emailstop' => PARAM_RAW, + 'lang' => PARAM_RAW, 'theme' => PARAM_RAW, 'timezone' => PARAM_RAW, 'mailformat' => PARAM_RAW))); + + $this->descriptions['tmp_delete_user'] = array( 'wsparams' => array('username'=> PARAM_ALPHANUM, 'mnethostid'=> PARAM_NUMBER), + 'return' => array('result' => PARAM_BOOL)); + + $this->descriptions['tmp_update_user'] = array( 'wsparams' => array('username'=> PARAM_ALPHANUM, 'mnethostid'=> PARAM_NUMBER, 'newusername' => PARAM_ALPHANUM, 'firstname' => PARAM_ALPHANUM), + 'return' => array('result' => PARAM_BOOL)); + } + + /** + * Retrieve all user + * @param string $search + * @return object user + */ + static function tmp_get_users($search) { + $selectioncriteria = new stdClass(); + $selectioncriteria->search = $search; + return user_api::tmp_get_users('firstname ASC', 999999, 0, 'id, auth, confirmed, username, idnumber, firstname, lastname, email, emailstop, lang, theme, timezone, mailformat', $selectioncriteria); + } + + /** + * Create a user + * @param string $username + * @param string $firstname + * @param string $lastname + * @param string $email + * @param string $password + * @return integer id of new user + */ + static function tmp_create_user($username, $firstname, $lastname, $email, $password) { + $user = array(); + $user['username'] = $username; + $user['firstname'] = $firstname; + $user['lastname'] = $lastname; + $user['email'] = $email; + $user['password'] = $password; + return user_api::tmp_create_user($user); + } + + /** + * Delete a user + * @global object $DB + * @param string $username + * @param integer $mnethostid + * @return boolean true if success + */ + static function tmp_delete_user($username, $mnethostid) { + global $DB; + $user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$mnethostid)); + /// PLEASE UNCOMMENT HERE ONCE AUTHENTICATION IS IMPLEMENTED - $USER/context need to be set here + //if (require_capability('moodle/user:delete', get_context_instance(CONTEXT_SYSTEM))) { + return delete_user($user); //this function is in moodlelib.php + //} + //else { + // throw new moodle_exception('couldnotdeleteuser'); + //} + } + + /** + * Update some user information + * @global object $DB + * @param string $username + * @param integer $mnethostid + * @param string $newusername + * @param string $firstname + * @return boolean true if success + */ + static function tmp_update_user($username, $mnethostid, $newusername, $firstname) { + global $DB; + $user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$mnethostid)); + $user->username = $newusername; + $user->firstname = $firstname; + + return user_api::tmp_update_user($user); + } + +} + +?> diff --git a/webservice/rest/lib.php b/webservice/rest/lib.php index 661ffefe4f..d5848133f4 100644 --- a/webservice/rest/lib.php +++ b/webservice/rest/lib.php @@ -22,13 +22,11 @@ function call_moodle_function ($rest_arguments) { $classname = str_replace('/', '_', $apipath); // convert '/' into '_' (e.g. /mod/forum/ => _mod_forum_) $classname = substr($classname,1, strlen($classname) - 1); //remove first _ (e.g. _mod_forum => mod_forum) - $classname .= 'ws_api'; + $classname .= 'external'; -///these three next lines can be generic => create a function - require_once($CFG->dirroot.$apipath.'wsapi.php'); - $api = new $classname(); - - $description = $api->get_function_webservice_description($functionname); //retrieve the web service description for this function + require_once($CFG->dirroot.$apipath.'external.php'); + $wsapi = new $classname(); + $description = $wsapi->get_function_webservice_description($functionname); //retrieve the web service description for this function ///This following line is only REST protocol $params = retrieve_params ($description); //retrieve the REST params @@ -37,7 +35,6 @@ function call_moodle_function ($rest_arguments) { if ($params === false) { //return an error message, the REST params doesn't match with the web service description } - //require_once($CFG->dirroot.$apipath.'api.php'); $res = call_user_func_array ( $classname.'::'.$functionname, $params); ///Transform result into xml in order to send the REST response @@ -54,9 +51,7 @@ function call_moodle_function ($rest_arguments) { * @return */ function retrieve_params ($description) { -// $params = $description['wsparams']; //retrieve REST param matching the description (warning: PHP assign the first instanciation as the first position in the table) - foreach ($description['wsparams'] as $paramname => $paramtype) { $value = optional_param($paramname,null,$paramtype); if (!empty($value)) { diff --git a/webservice/soap/generatewsdl.php b/webservice/soap/generatewsdl.php index 2667c8f938..e7427eb7f1 100644 --- a/webservice/soap/generatewsdl.php +++ b/webservice/soap/generatewsdl.php @@ -20,8 +20,8 @@ class wsdl_generator { function __construct () { // The exception list - // if ever there is some wsapi.php file that are not a web service class, they need to be declared here - // example: $this->exceptionlist['/home/jerome/Projects/Moodle_HEAD/moodle/mod/scorm/wsapi.php'] = true; + // if ever there is some external.php file that are not a web service class, they need to be declared here + // example: $this->exceptionlist['/home/jerome/Projects/Moodle_HEAD/moodle/mod/scorm/external.php'] = true; $this->exceptionlist = array(); } @@ -75,9 +75,9 @@ 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 /wsapi.php from the classpath + $classpath = substr($classpath,0,strlen($classpath) - 10); //remove /external.php from the classpath $classpath = str_replace('/','_',$classpath); //convert all / into _ - $classname = $classpath."_ws_api"; + $classname = $classpath."_external"; $api = new $classname(); $wsdlporttype .= <<setListApiFiles($files, $path); } ///retrieve api.php file - else if ($file == "wsapi.php" && ! $this->inExceptionList($path)) { + else if ($file == "external.php" && ! $this->inExceptionList($path)) { $files[] = $path; } } @@ -223,7 +223,7 @@ EOF; /** * Hacky function - * We need to define if we remove all wsapi.php file from Moodle when they do not really + * We need to define if we remove all external.php file from Moodle when they do not really * are ws api file for Moodle ws API * @param string $path * @return boolean true if the path if in the exception list diff --git a/webservice/soap/moodle.wsdl b/webservice/soap/moodle.wsdl index 916d80b9b9..5849f620b2 100644 --- a/webservice/soap/moodle.wsdl +++ b/webservice/soap/moodle.wsdl @@ -1,127 +1,165 @@ - - - - - - - - - - - - + + + + + + + + + + + + + + - - + + + + + + + + + + - - - - - - + + + - - + + - - - + + + - - + + + - - - - - + + + + + - - + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - + + + + - \ No newline at end of file + diff --git a/webservice/soap/server.php b/webservice/soap/server.php index 278d3c1619..703cca879a 100644 --- a/webservice/soap/server.php +++ b/webservice/soap/server.php @@ -18,11 +18,11 @@ if (empty($CFG->enablewebservices)) { //retrieve the api name $classpath = optional_param(classpath,null,PARAM_ALPHA); -require_once(dirname(__FILE__) . '/../../'.$classpath.'/wsapi.php'); +require_once(dirname(__FILE__) . '/../../'.$classpath.'/external.php'); /// run the server $server = new SoapServer("moodle.wsdl"); -$server->setClass($classpath."_ws_api"); +$server->setClass($classpath."_external"); $server->handle(); ?> \ No newline at end of file -- 2.39.5