From 2c7e75fa46fea31dd0c16fe6d7a468fe624e5c3f Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Thu, 14 Jan 2016 12:59:44 +0100 Subject: [PATCH] Add WS to get current portal list, add/get/remove user to portal add/get/remove course to portal See BT#10709 --- main/webservices/access_url.php | 439 ++++++++++++++++++++++++++++++++ 1 file changed, 439 insertions(+) create mode 100644 main/webservices/access_url.php diff --git a/main/webservices/access_url.php b/main/webservices/access_url.php new file mode 100644 index 0000000000..32c0b3bea8 --- /dev/null +++ b/main/webservices/access_url.php @@ -0,0 +1,439 @@ +setEventData(array('server' => $server)); + $res = $hook->notifyWSRegistration(HOOK_EVENT_TYPE_PRE); + if (!empty($res['server'])) { + $server = $res['server']; + } +} + +$server->soap_defencoding = 'UTF-8'; + +// Initialize WSDL support +$server->configureWSDL('WSAccessUrl', 'urn:WSAccessUrl'); + + + +$server->wsdl->addComplexType( + 'portalItem', + 'complexType', + 'struct', + 'all', + '', + array( + 'id' => array('name' => 'id', 'type' => 'xsd:string'), + 'url' => array('name' => 'url', 'type' => 'xsd:string') + ) +); + +$server->wsdl->addComplexType( + 'portalList', + 'complexType', + 'array', + '', + 'SOAP-ENC:Array', + array(), + array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType' => 'tns:portalItem[]')),'tns:portalItem' +); + +$server->wsdl->addComplexType( + 'getPortals', + 'complexType', + 'struct', + 'all', + '', + array( + 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string') + ) +); + +// Register the method to expose +$server->register('WSGetPortals', // method name + array('getPortals' => 'tns:getPortals'), // input parameters + array('return' => 'tns:portalList'), // output parameters + 'urn:WSAccessUrl', // namespace + 'urn:WSAccessUrl#WSGetPortals', // soapaction + 'rpc', // style + 'encoded', // use + 'This service adds a user to portal' // documentation +); + +// Define the method WSAddUserToPortal +function WSGetPortals($params) +{ + global $debug; + if (!WSHelperVerifyKey($params['secret_key'])) { + return return_error(WS_ERROR_SECRET_KEY); + } + $urlData = UrlManager::get_url_data(); + + $return = []; + foreach ($urlData as $data) { + $return[] = [ + 'id' => $data['id'], + 'url' => $data['url'], + ]; + } + if ($debug) { + error_log(print_r($return, 1)); + } + + return $return; +} + +$server->wsdl->addComplexType( + 'AddUserToPortal', + 'complexType', + 'struct', + 'all', + '', + array( + 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string'), + 'user_id' => array('name' => 'user_id', 'type' => 'xsd:string'), + 'portal_id' => array('name' => 'portal_id', 'type' => 'xsd:string') + ) +); + +// Register the method to expose +$server->register('WSAddUserToPortal', // method name + array('addUserToPortal' => 'tns:AddUserToPortal'), // input parameters + array('return' => 'xsd:string'), // output parameters + 'urn:WSAccessUrl', // namespace + 'urn:WSAccessUrl#WSAddUserToPortal', // soapaction + 'rpc', // style + 'encoded', // use + 'This service adds a user to portal' // documentation +); + +// Define the method WSAddUserToPortal +function WSAddUserToPortal($params) +{ + if (!WSHelperVerifyKey($params['secret_key'])) { + return return_error(WS_ERROR_SECRET_KEY); + } + + $userId = $params['user_id']; + $portalId = $params['portal_id']; + + UrlManager::add_user_to_url($userId, $portalId); + + $result = UrlManager::relation_url_user_exist($userId, $portalId); + if (!empty($result)) { + return 1; + } + + return 0; +} + +// Register the method to expose +$server->register('WSRemoveUserFromPortal', // method name + array('removeUserFromPortal' => 'tns:AddUserToPortal'), // input parameters + array('return' => 'xsd:string'), // output parameters + 'urn:WSAccessUrl', // namespace + 'urn:WSAccessUrl#WSRemoveUserFromPortal', // soapaction + 'rpc', // style + 'encoded', // use + 'This service remove a user from a portal' // documentation +); + +// Define the method WSDeleteUserFromGroup +function WSRemoveUserFromPortal($params) +{ + if (!WSHelperVerifyKey($params['secret_key'])) { + return return_error(WS_ERROR_SECRET_KEY); + } + + $userId = $params['user_id']; + $portalId = $params['portal_id']; + + UrlManager::delete_url_rel_user($userId, $portalId); + + $result = UrlManager::relation_url_user_exist($userId, $portalId); + if (empty($result)) { + return 1; + } + + return 0; +} + +$server->wsdl->addComplexType( + 'getPortalListFromUser', + 'complexType', + 'struct', + 'all', + '', + array( + 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string'), + 'user_id' => array('name' => 'user_id', 'type' => 'xsd:string'), + ) +); + +// Register the method to expose +$server->register('WSGetPortalListFromUser', // method name + array('getPortalListFromUser' => 'tns:getPortalListFromUser'), // input parameters + array('return' => 'tns:portalList'), // output parameters + 'urn:WSAccessUrl', // namespace + 'urn:WSAccessUrl#WSGetPortalListFromUser', // soapaction + 'rpc', // style + 'encoded', // use + 'This service remove a user from a portal' // documentation +); + +// Define the method WSDeleteUserFromGroup +function WSGetPortalListFromUser($params) +{ + if (!WSHelperVerifyKey($params['secret_key'])) { + return return_error(WS_ERROR_SECRET_KEY); + } + + $userId = $params['user_id']; + + $result = UrlManager::get_access_url_from_user($userId); + if (!empty($result)) { + foreach ($result as &$data) { + $data['id'] = $data['access_url_id']; + } + } + + return $result; +} + + +// Course + +$server->wsdl->addComplexType( + 'getPortalListFromCourse', + 'complexType', + 'struct', + 'all', + '', + array( + 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string'), + 'course_id' => array('name' => 'course_id', 'type' => 'xsd:string'), + ) +); + +// Register the method to expose +$server->register('WSGetPortalListFromCourse', // method name + array('getPortalListFromCourse' => 'tns:getPortalListFromCourse'), // input parameters + array('return' => 'tns:portalList'), // output parameters + 'urn:WSAccessUrl', // namespace + 'urn:WSAccessUrl#getPortalListFromCourse', // soapaction + 'rpc', // style + 'encoded', // use + 'This service remove a user from a portal' // documentation +); + +// Define the method WSDeleteUserFromGroup +function WSGetPortalListFromCourse($params) +{ + if (!WSHelperVerifyKey($params['secret_key'])) { + return return_error(WS_ERROR_SECRET_KEY); + } + + $courseId = $params['course_id']; + + $result = UrlManager::get_access_url_from_course($courseId); + + if (!empty($result)) { + foreach ($result as &$data) { + $data['id'] = $data['access_url_id']; + } + } + + return $result; +} + +$server->wsdl->addComplexType( + 'addCourseToPortal', + 'complexType', + 'struct', + 'all', + '', + array( + 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string'), + 'portal_id' => array('name' => 'portal_id', 'type' => 'xsd:string'), + 'course_id' => array('name' => 'course_id', 'type' => 'xsd:string') + ) +); + +// Register the method to expose +$server->register('WSAddCourseToPortal', // method name + array('addCourseToPortal' => 'tns:addCourseToPortal'), // input parameters + array('return' => 'xsd:string'), // output parameters + 'urn:WSAccessUrl', // namespace + 'urn:WSAccessUrl#WSAddCourseToPortal', // soapaction + 'rpc', // style + 'encoded', // use + 'This service adds a course to portal' // documentation +); + +// Define the method WSAddUserToPortal +function WSAddCourseToPortal($params) +{ + if (!WSHelperVerifyKey($params['secret_key'])) { + return return_error(WS_ERROR_SECRET_KEY); + } + + $courseId = $params['course_id']; + $portalId = $params['portal_id']; + + UrlManager::add_course_to_url($courseId, $portalId); + + $result = UrlManager::relation_url_course_exist($courseId, $portalId); + + return intval($result); +} + +// Register the method to expose +$server->register('WSRemoveCourseFromPortal', // method name + array('removeCourseFromPortal' => 'tns:addCourseToPortal'), // input parameters + array('return' => 'xsd:string'), // output parameters + 'urn:WSAccessUrl', // namespace + 'urn:WSAccessUrl#WSRemoveCourseFromPortal', // soapaction + 'rpc', // style + 'encoded', // use + 'This service remove a course from a portal' // documentation +); + +// Define the method WSDeleteUserFromGroup +function WSRemoveCourseFromPortal($params) +{ + if (!WSHelperVerifyKey($params['secret_key'])) { + return return_error(WS_ERROR_SECRET_KEY); + } + + $courseId = $params['course_id']; + $portalId = $params['portal_id']; + + UrlManager::delete_url_rel_course($courseId, $portalId); + $result = UrlManager::relation_url_course_exist($courseId, $portalId); + + if (empty($result)) { + return true; + } + return false; +} + + +/* Delete user from group Web Service end */ + +// Add more webservices through hooks from plugins +if (!empty($hook)) { + $hook->setEventData(array('server' => $server)); + $res = $hook->notifyWSRegistration(HOOK_EVENT_TYPE_POST); + if (!empty($res['server'])) { + $server = $res['server']; + } +} + +// Use the request to (try to) invoke the service +$GLOBALS['HTTP_RAW_POST_DATA'] = file_get_contents('php://input'); +$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; + +// If you send your data in utf8 then this value must be false. +$decodeUTF8 = api_get_setting('registration.soap.php.decode_utf8'); +if ($decodeUTF8 === 'true') { + $server->decode_utf8 = true; +} else { + $server->decode_utf8 = false; +} +$server->service($HTTP_RAW_POST_DATA);