$PAGE->navbar->add(get_string('serviceusers', 'webservice'), new moodle_url('/' . $CFG->admin . '/webservice/service_users.php', array('id' => $id))); $webservicemanager = new webservice(); /// Get the user_selector we will need. $potentialuserselector = new service_user_selector('addselect', array('serviceid' => $id, 'displayallowedusers' => 0)); $alloweduserselector = new service_user_selector('removeselect', array('serviceid' => $id, 'displayallowedusers' => 1)); /// Process incoming user assignments to the service if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) { $userstoassign = $potentialuserselector->get_selected_users(); if (!empty($userstoassign)) { foreach ($userstoassign as $adduser) { $serviceuser = new stdClass(); $serviceuser->externalserviceid = $id; $serviceuser->userid = $adduser->id; $webservicemanager->add_ws_authorised_user($serviceuser); $params = array('objectid' => $serviceuser->externalserviceid, 'relateduserid' => $serviceuser->userid); $event = \core\event\webservice_service_user_added::create($params); $event->trigger(); } $potentialuserselector->invalidate_selected_users(); $alloweduserselector->invalidate_selected_users(); } } /// Process removing user assignments to the service if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) { $userstoremove = $alloweduserselector->get_selected_users(); if (!empty($userstoremove)) { foreach ($userstoremove as $removeuser) { $webservicemanager->remove_ws_authorised_user($removeuser, $id); $params = array('objectid' => $id, 'relateduserid' => $removeuser->id); $event = \core\event\webservice_service_user_removed::create($params); $event->trigger();
public function test_service_user_added() { global $CFG; // The Web service API doesn't allow the testing of the events directly by // calling some functions which trigger the events, so what we are going here // is just checking that the event returns the expected information. $sink = $this->redirectEvents(); $params = array('objectid' => 1, 'relateduserid' => 2); $event = \core\event\webservice_service_user_added::create($params); $event->trigger(); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); $this->assertEquals(context_system::instance(), $event->get_context()); $this->assertEquals(1, $event->objectid); $this->assertEquals(2, $event->relateduserid); $expected = array(SITEID, 'core', 'assign', $CFG->admin . '/webservice/service_users.php?id=' . $params['objectid'], 'add', '', $params['relateduserid']); $this->assertEventLegacyLogData($expected, $event); }