Skip to content

Commit

Permalink
MDL-40050 webservice: Replace add_to_log() with new events
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic Massart committed Sep 24, 2013
1 parent 7f3836d commit d733a8c
Show file tree
Hide file tree
Showing 15 changed files with 1,398 additions and 32 deletions.
21 changes: 18 additions & 3 deletions admin/webservice/service.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@
}
//The user has confirmed the deletion, delete and redirect
$webservicemanager->delete_service($service->id);
add_to_log(SITEID, 'webservice', 'delete', $returnurl, get_string('deleteservice', 'webservice', $service));
$params = array(
'objectid' => $service->id
);
$event = \core\event\webservice_service_deleted::create($params);
$event->add_record_snapshot('external_services', $service);
$event->trigger();
redirect($returnurl);
}

Expand All @@ -75,7 +80,12 @@
//create operation
if (empty($servicedata->id)) {
$servicedata->id = $webservicemanager->add_external_service($servicedata);
add_to_log(SITEID, 'webservice', 'add', $returnurl, get_string('addservice', 'webservice', $servicedata));
$params = array(
'objectid' => $servicedata->id
);
$event = \core\event\webservice_service_updated::create($params);
$event->add_record_snapshot('external_services', $servicedata);
$event->trigger();

//redirect to the 'add functions to service' page
$addfunctionpage = new moodle_url(
Expand All @@ -85,7 +95,12 @@
} else {
//update operation
$webservicemanager->update_external_service($servicedata);
add_to_log(SITEID, 'webservice', 'edit', $returnurl, get_string('editservice', 'webservice', $servicedata));
$params = array(
'objectid' => $servicedata->id
);
$event = \core\event\webservice_service_created::create($params);
$event->add_record_snapshot('external_services', $servicedata);
$event->trigger();
}

redirect($returnurl);
Expand Down
18 changes: 14 additions & 4 deletions admin/webservice/service_users.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@
$serviceuser->externalserviceid = $id;
$serviceuser->userid = $adduser->id;
$webservicemanager->add_ws_authorised_user($serviceuser);
add_to_log(SITEID, 'core', 'assign', $CFG->admin . '/webservice/service_users.php?id='
. $id, 'add', '', $adduser->id);

$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();
Expand All @@ -71,8 +76,13 @@
if (!empty($userstoremove)) {
foreach ($userstoremove as $removeuser) {
$webservicemanager->remove_ws_authorised_user($removeuser, $id);
add_to_log(SITEID, 'core', 'assign', $CFG->admin . '/webservice/service_users.php?id='
. $id, 'remove', '', $removeuser->id);

$params = array(
'objectid' => $id,
'relateduserid' => $removeuser->id
);
$event = \core\event\webservice_service_user_removed::create($params);
$event->trigger();
}
$potentialuserselector->invalidate_selected_users();
$alloweduserselector->invalidate_selected_users();
Expand Down
8 changes: 8 additions & 0 deletions lang/en/webservice.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@
$string['errorinvalidparam'] = 'The param "{$a}" is invalid.';
$string['errornotemptydefaultparamarray'] = 'The web service description parameter named \'{$a}\' is an single or multiple structure. The default can only be empty array. Check web service description.';
$string['erroroptionalparamarray'] = 'The web service description parameter named \'{$a}\' is an single or multiple structure. It can not be set as VALUE_OPTIONAL. Check web service description.';
$string['event_webservice_function_called'] = 'Web service function called';
$string['event_webservice_login_failed'] = 'Web service login failed';
$string['event_webservice_service_created'] = 'Web service service created';
$string['event_webservice_service_updated'] = 'Web service service updated';
$string['event_webservice_service_user_added'] = 'Web service service user added';
$string['event_webservice_service_user_removed'] = 'Web service service user removed';
$string['event_webservice_token_created'] = 'Web service token created';
$string['event_webservice_token_sent'] = 'Web service token sent';
$string['execute'] = 'Execute';
$string['executewarnign'] = 'WARNING: If you press execute your database will be modified and changes can not be reverted automatically!';
$string['externalservice'] = 'External service';
Expand Down
101 changes: 101 additions & 0 deletions lib/classes/event/webservice_function_called.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* core webservice function_called event.
*
* @package core
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core\event;
defined('MOODLE_INTERNAL') || die();

/**
* core webservice function_called event class.
*
* @package core
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class webservice_function_called extends \core\event\base {

/**
* Legacy log data.
*/
protected $legacylogdata;

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The web service function '{$this->other['function']}' has been called.";
}

/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
return $this->legacylogdata;
}

/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_webservice_function_called', 'webservice');
}

/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['level'] = self::LEVEL_OTHER;
$this->context = \context_system::instance();
}

/**
* Return the legacy event log data.
*
* @return void
*/
public function set_legacy_logdata($legacydata) {
$this->legacylogdata = $legacydata;
}

/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
if (!isset($this->other['function'])) {
throw new \coding_exception('The key \'function\' needs to be set in $other.');
}
}

}
107 changes: 107 additions & 0 deletions lib/classes/event/webservice_login_failed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* core web service login failed event.
*
* @package core
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core\event;
defined('MOODLE_INTERNAL') || die();

/**
* core web service login_failed event class.
*
* @package core
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class webservice_login_failed extends \core\event\base {

/**
* Legacy log data.
*
* @var null|array
*/
protected $legacylogdata;

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "Web service authentication failed with code: {$this->other['reason']}.";
}

/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
return $this->legacylogdata;
}

/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_webservice_login_failed', 'webservice');
}

/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['level'] = self::LEVEL_OTHER;
$this->context = \context_system::instance();
}

/**
* Set the legacy event log data.
*
* @param array $logdata The log data.
* @return void
*/
public function set_legacy_logdata($logdata) {
$this->legacylogdata = $logdata;
}

/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
if (!isset($this->other['reason'])) {
throw new \coding_exception('The key \'reason\' needs to be set in $other.');
} else if (!isset($this->other['method'])) {
throw new \coding_exception('The key \'method\' needs to be set in $other.');
} else if (!isset($this->other['token']) && !isset($this->other['tokenid']) && !isset($this->other['username'])) {
throw new \coding_exception('The keys \'username\', \'token\' or \'tokenid\' need to be set in $other.');
}
}
}
88 changes: 88 additions & 0 deletions lib/classes/event/webservice_service_created.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* core webservice service created event.
*
* @package core
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core\event;
defined('MOODLE_INTERNAL') || die();

/**
* core webservice service created event class.
*
* @package core
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class webservice_service_created extends \core\event\base {

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The web service service $this->objectid has been created by user $this->userid.";
}

/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
global $CFG;
$service = $this->get_record_snapshot('external_services', $this->objectid);
return array(SITEID, 'webservice', 'add', $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices",
get_string('addservice', 'webservice', $service));
}

/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_webservice_service_created', 'webservice');
}

/**
* Get URL related to the action.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/admin/settings.php', array('section' => 'externalservices'));
}

/**
* Init method.
*
* @return void
*/
protected function init() {
$this->context = \context_system::instance();
$this->data['crud'] = 'c';
$this->data['level'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'external_services';
}

}
Loading

0 comments on commit d733a8c

Please sign in to comment.