| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 4 | //
 | 
        
           |  |  | 5 | // Moodle is free software: you can redistribute it and/or modify
 | 
        
           |  |  | 6 | // it under the terms of the GNU General Public License as published by
 | 
        
           |  |  | 7 | // the Free Software Foundation, either version 3 of the License, or
 | 
        
           |  |  | 8 | // (at your option) any later version.
 | 
        
           |  |  | 9 | //
 | 
        
           |  |  | 10 | // Moodle is distributed in the hope that it will be useful,
 | 
        
           |  |  | 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
        
           |  |  | 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
        
           |  |  | 13 | // GNU General Public License for more details.
 | 
        
           |  |  | 14 | //
 | 
        
           |  |  | 15 | // You should have received a copy of the GNU General Public License
 | 
        
           |  |  | 16 | // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 | 
        
           |  |  | 17 |   | 
        
           |  |  | 18 | /**
 | 
        
           |  |  | 19 |  * Web services services UI
 | 
        
           |  |  | 20 |  *
 | 
        
           |  |  | 21 |  * @package   webservice
 | 
        
           |  |  | 22 |  * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
 | 
        
           |  |  | 23 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 24 |  */
 | 
        
           |  |  | 25 | require_once('../../config.php');
 | 
        
           |  |  | 26 | require_once($CFG->libdir . '/adminlib.php');
 | 
        
           |  |  | 27 | require_once($CFG->dirroot . '/' . $CFG->admin . '/webservice/lib.php');
 | 
        
           |  |  | 28 | require_once($CFG->dirroot . '/webservice/lib.php');
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 | $id = required_param('id', PARAM_INT);
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 | admin_externalpage_setup('externalserviceusers');
 | 
        
           |  |  | 33 |   | 
        
           |  |  | 34 | //define nav bar
 | 
        
           |  |  | 35 | $PAGE->set_url('/' . $CFG->admin . '/webservice/service_users.php', array('id' => $id));
 | 
        
           |  |  | 36 | $node = $PAGE->settingsnav->find('externalservices', navigation_node::TYPE_SETTING);
 | 
        
           |  |  | 37 | if ($node) {
 | 
        
           |  |  | 38 |     $node->make_active();
 | 
        
           |  |  | 39 | }
 | 
        
           |  |  | 40 | $PAGE->navbar->add(get_string('serviceusers', 'webservice'),
 | 
        
           |  |  | 41 |         new moodle_url('/' . $CFG->admin . '/webservice/service_users.php', array('id' => $id)));
 | 
        
           |  |  | 42 |   | 
        
           |  |  | 43 | $webservicemanager = new webservice();
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 | /// Get the user_selector we will need.
 | 
        
           |  |  | 46 | $potentialuserselector = new service_user_selector('addselect',
 | 
        
           |  |  | 47 |                 array('serviceid' => $id, 'displayallowedusers' => 0));
 | 
        
           |  |  | 48 | $alloweduserselector = new service_user_selector('removeselect',
 | 
        
           |  |  | 49 |                 array('serviceid' => $id, 'displayallowedusers' => 1));
 | 
        
           |  |  | 50 |   | 
        
           |  |  | 51 | /// Process incoming user assignments to the service
 | 
        
           |  |  | 52 | if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
 | 
        
           |  |  | 53 |     $userstoassign = $potentialuserselector->get_selected_users();
 | 
        
           |  |  | 54 |     if (!empty($userstoassign)) {
 | 
        
           |  |  | 55 |         foreach ($userstoassign as $adduser) {
 | 
        
           |  |  | 56 |             $serviceuser = new stdClass();
 | 
        
           |  |  | 57 |             $serviceuser->externalserviceid = $id;
 | 
        
           |  |  | 58 |             $serviceuser->userid = $adduser->id;
 | 
        
           |  |  | 59 |             $webservicemanager->add_ws_authorised_user($serviceuser);
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 |             $params = array(
 | 
        
           |  |  | 62 |                 'objectid' => $serviceuser->externalserviceid,
 | 
        
           |  |  | 63 |                 'relateduserid' => $serviceuser->userid
 | 
        
           |  |  | 64 |             );
 | 
        
           |  |  | 65 |             $event = \core\event\webservice_service_user_added::create($params);
 | 
        
           |  |  | 66 |             $event->trigger();
 | 
        
           |  |  | 67 |         }
 | 
        
           |  |  | 68 |         $potentialuserselector->invalidate_selected_users();
 | 
        
           |  |  | 69 |         $alloweduserselector->invalidate_selected_users();
 | 
        
           |  |  | 70 |     }
 | 
        
           |  |  | 71 | }
 | 
        
           |  |  | 72 |   | 
        
           |  |  | 73 | /// Process removing user assignments to the service
 | 
        
           |  |  | 74 | if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
 | 
        
           |  |  | 75 |     $userstoremove = $alloweduserselector->get_selected_users();
 | 
        
           |  |  | 76 |     if (!empty($userstoremove)) {
 | 
        
           |  |  | 77 |         foreach ($userstoremove as $removeuser) {
 | 
        
           |  |  | 78 |             $webservicemanager->remove_ws_authorised_user($removeuser, $id);
 | 
        
           |  |  | 79 |   | 
        
           |  |  | 80 |             $params = array(
 | 
        
           |  |  | 81 |                 'objectid' => $id,
 | 
        
           |  |  | 82 |                 'relateduserid' => $removeuser->id
 | 
        
           |  |  | 83 |             );
 | 
        
           |  |  | 84 |             $event = \core\event\webservice_service_user_removed::create($params);
 | 
        
           |  |  | 85 |             $event->trigger();
 | 
        
           |  |  | 86 |         }
 | 
        
           |  |  | 87 |         $potentialuserselector->invalidate_selected_users();
 | 
        
           |  |  | 88 |         $alloweduserselector->invalidate_selected_users();
 | 
        
           |  |  | 89 |     }
 | 
        
           |  |  | 90 | }
 | 
        
           |  |  | 91 | /// Print the form.
 | 
        
           |  |  | 92 | /// display the UI
 | 
        
           |  |  | 93 | $renderer = $PAGE->get_renderer('core', 'webservice');
 | 
        
           |  |  | 94 |   | 
        
           |  |  | 95 | echo $OUTPUT->header();
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 | echo $OUTPUT->heading(get_string('selectauthorisedusers', 'webservice'), 3, 'main');
 | 
        
           |  |  | 98 | $selectoroptions = new stdClass();
 | 
        
           |  |  | 99 | $selectoroptions->serviceid = $id;
 | 
        
           |  |  | 100 | $selectoroptions->alloweduserselector = $alloweduserselector;
 | 
        
           |  |  | 101 | $selectoroptions->potentialuserselector = $potentialuserselector;
 | 
        
           |  |  | 102 | echo $renderer->admin_authorised_user_selector($selectoroptions);
 | 
        
           |  |  | 103 |   | 
        
           |  |  | 104 | /// get the missing capabilities for all users (will be displayed into the renderer)
 | 
        
           |  |  | 105 | $allowedusers = $webservicemanager->get_ws_authorised_users($id);
 | 
        
           |  |  | 106 | $usersmissingcaps = $webservicemanager->get_missing_capabilities_by_users($allowedusers, $id);
 | 
        
           |  |  | 107 |   | 
        
           |  |  | 108 | //add the missing capabilities to the allowed users object to be displayed by renderer
 | 
        
           |  |  | 109 | foreach ($allowedusers as &$alloweduser) {
 | 
        
           |  |  | 110 |     if (!is_siteadmin($alloweduser->id) and array_key_exists($alloweduser->id, $usersmissingcaps)) {
 | 
        
           |  |  | 111 |         $alloweduser->missingcapabilities = $usersmissingcaps[$alloweduser->id];
 | 
        
           |  |  | 112 |     }
 | 
        
           |  |  | 113 | }
 | 
        
           |  |  | 114 |   | 
        
           |  |  | 115 | /// display the list of allowed users with their options (ip/timecreated / validuntil...)
 | 
        
           |  |  | 116 | //check that the user has the service required capability (if needed)
 | 
        
           |  |  | 117 | if (!empty($allowedusers)) {
 | 
        
           |  |  | 118 |     $renderer = $PAGE->get_renderer('core', 'webservice');
 | 
        
           |  |  | 119 |     echo $OUTPUT->heading(get_string('serviceuserssettings', 'webservice'), 3, 'main');
 | 
        
           |  |  | 120 |     echo $renderer->admin_authorised_user_list($allowedusers, $id);
 | 
        
           |  |  | 121 | }
 | 
        
           |  |  | 122 |   | 
        
           |  |  | 123 | echo $OUTPUT->footer();
 |