mirror of
https://github.com/moodle/moodle.git
synced 2025-04-15 05:25:08 +02:00
webservice MDL-21524 add role_assign and role_unassign ws functions
This commit is contained in:
parent
5be949984d
commit
e9b66095a3
@ -25,6 +25,129 @@ class webservice_test_client_form extends moodleform {
|
||||
|
||||
// === Test client forms ===
|
||||
|
||||
|
||||
class moodle_enrol_role_assign_form extends moodleform {
|
||||
public function definition() {
|
||||
global $CFG;
|
||||
|
||||
$mform = $this->_form;
|
||||
|
||||
|
||||
$mform->addElement('header', 'wstestclienthdr', get_string('testclient', 'webservice'));
|
||||
|
||||
//note: these values are intentionally PARAM_RAW - we want users to test any rubbish as parameters
|
||||
$data = $this->_customdata;
|
||||
if ($data['authmethod'] == 'simple') {
|
||||
$mform->addElement('text', 'wsusername', 'wsusername');
|
||||
$mform->addElement('text', 'wspassword', 'wspassword');
|
||||
} else if ($data['authmethod'] == 'token') {
|
||||
$mform->addElement('text', 'token', 'token');
|
||||
}
|
||||
|
||||
$mform->addElement('hidden', 'authmethod', $data['authmethod']);
|
||||
$mform->setType('authmethod', PARAM_SAFEDIR);
|
||||
|
||||
/// specific to the create users function
|
||||
$mform->addElement('text', 'userid', 'userid');
|
||||
$mform->addElement('text', 'roleid', 'roleid');
|
||||
$mform->addElement('text', 'contextid', 'contextid');
|
||||
$mform->addElement('text', 'timestart', 'timestart');
|
||||
$mform->addElement('text', 'timeend', 'timeend');
|
||||
|
||||
$mform->addElement('hidden', 'function');
|
||||
$mform->setType('function', PARAM_SAFEDIR);
|
||||
|
||||
$mform->addElement('hidden', 'protocol');
|
||||
$mform->setType('protocol', PARAM_SAFEDIR);
|
||||
|
||||
|
||||
|
||||
$mform->addElement('static', 'warning', '', get_string('executewarnign', 'webservice'));
|
||||
|
||||
$this->add_action_buttons(true, get_string('execute', 'webservice'));
|
||||
}
|
||||
|
||||
public function get_params() {
|
||||
if (!$data = $this->get_data()) {
|
||||
return null;
|
||||
}
|
||||
// remove unused from form data
|
||||
unset($data->submitbutton);
|
||||
unset($data->protocol);
|
||||
unset($data->function);
|
||||
unset($data->wsusername);
|
||||
unset($data->wspassword);
|
||||
unset($data->token);
|
||||
unset($data->authmethod);
|
||||
|
||||
$params = array();
|
||||
$params['enrolments'] = array();
|
||||
$params['enrolments'][] = (array)$data;
|
||||
|
||||
return $params;
|
||||
}
|
||||
}
|
||||
|
||||
class moodle_enrol_role_unassign_form extends moodleform {
|
||||
public function definition() {
|
||||
global $CFG;
|
||||
|
||||
$mform = $this->_form;
|
||||
|
||||
|
||||
$mform->addElement('header', 'wstestclienthdr', get_string('testclient', 'webservice'));
|
||||
|
||||
//note: these values are intentionally PARAM_RAW - we want users to test any rubbish as parameters
|
||||
$data = $this->_customdata;
|
||||
if ($data['authmethod'] == 'simple') {
|
||||
$mform->addElement('text', 'wsusername', 'wsusername');
|
||||
$mform->addElement('text', 'wspassword', 'wspassword');
|
||||
} else if ($data['authmethod'] == 'token') {
|
||||
$mform->addElement('text', 'token', 'token');
|
||||
}
|
||||
|
||||
$mform->addElement('hidden', 'authmethod', $data['authmethod']);
|
||||
$mform->setType('authmethod', PARAM_SAFEDIR);
|
||||
|
||||
/// specific to the create users function
|
||||
$mform->addElement('text', 'userid', 'userid');
|
||||
$mform->addElement('text', 'roleid', 'roleid');
|
||||
$mform->addElement('text', 'contextid', 'contextid');
|
||||
|
||||
$mform->addElement('hidden', 'function');
|
||||
$mform->setType('function', PARAM_SAFEDIR);
|
||||
|
||||
$mform->addElement('hidden', 'protocol');
|
||||
$mform->setType('protocol', PARAM_SAFEDIR);
|
||||
|
||||
|
||||
|
||||
$mform->addElement('static', 'warning', '', get_string('executewarnign', 'webservice'));
|
||||
|
||||
$this->add_action_buttons(true, get_string('execute', 'webservice'));
|
||||
}
|
||||
|
||||
public function get_params() {
|
||||
if (!$data = $this->get_data()) {
|
||||
return null;
|
||||
}
|
||||
// remove unused from form data
|
||||
unset($data->submitbutton);
|
||||
unset($data->protocol);
|
||||
unset($data->function);
|
||||
unset($data->wsusername);
|
||||
unset($data->wspassword);
|
||||
unset($data->token);
|
||||
unset($data->authmethod);
|
||||
|
||||
$params = array();
|
||||
$params['unenrolments'] = array();
|
||||
$params['unenrolments'][] = (array)$data;
|
||||
|
||||
return $params;
|
||||
}
|
||||
}
|
||||
|
||||
class moodle_user_create_users_form extends moodleform {
|
||||
public function definition() {
|
||||
global $CFG;
|
||||
|
158
enrol/externallib.php
Normal file
158
enrol/externallib.php
Normal file
@ -0,0 +1,158 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* External enrol API
|
||||
*
|
||||
* @package moodlecore
|
||||
* @subpackage webservice
|
||||
* @copyright 2009 Moodle Pty Ltd (http://moodle.com)
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
require_once("$CFG->libdir/externallib.php");
|
||||
|
||||
class moodle_enrol_external extends external_api {
|
||||
|
||||
/**
|
||||
* Returns description of method parameters
|
||||
* @return external_function_parameters
|
||||
*/
|
||||
public static function role_assign_parameters() {
|
||||
global $CFG;
|
||||
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
'enrolments' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
'roleid' => new external_value(PARAM_RAW, 'Role to assign to the user'),
|
||||
'userid' => new external_value(PARAM_RAW, 'The user that is going to be assigned'),
|
||||
'contextid' => new external_value(PARAM_NOTAGS, 'The context to assign the user into '),
|
||||
'timestart' => new external_value(PARAM_EMAIL, 'A valid and unique email address', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED),
|
||||
'timeend' => new external_value(PARAM_SAFEDIR, 'Auth plugins include manual, ldap, imap, etc', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign roles to users
|
||||
*
|
||||
* @param array $enrolment An array of enrolment
|
||||
* @return null
|
||||
*/
|
||||
public static function role_assign($enrolments) {
|
||||
global $CFG, $DB;
|
||||
|
||||
// Do basic automatic PARAM checks on incoming data, using params description
|
||||
// If any problems are found then exceptions are thrown with helpful error messages
|
||||
$params = self::validate_parameters(self::role_assign_parameters(), array('enrolments'=>$enrolments));
|
||||
|
||||
$transaction = $DB->start_delegated_transaction();
|
||||
|
||||
$success = true;
|
||||
|
||||
foreach ($params['enrolments'] as $enrolment) {
|
||||
// Ensure the current user is allowed to run this function in the enrolment context
|
||||
$context = get_context_instance_by_id($enrolment['contextid']);
|
||||
self::validate_context($context);
|
||||
require_capability('moodle/role:assign', $context);
|
||||
|
||||
if(!role_assign($enrolment['roleid'], $enrolment['userid'], null, $enrolment['contextid'], $enrolment['timestart'], $enrolment['timeend'])) {
|
||||
$success = false;
|
||||
}
|
||||
}
|
||||
|
||||
$transaction->allow_commit();
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value
|
||||
* @return external_description
|
||||
*/
|
||||
public static function role_assign_returns() {
|
||||
return new external_value(PARAM_BOOL, 'If all assignement succeed returns true');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns description of method parameters
|
||||
* @return external_function_parameters
|
||||
*/
|
||||
public static function role_unassign_parameters() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
'unenrolments' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
'roleid' => new external_value(PARAM_RAW, 'Role to assign to the user'),
|
||||
'userid' => new external_value(PARAM_RAW, 'The user that is going to be assigned'),
|
||||
'contextid' => new external_value(PARAM_NOTAGS, 'The context to assign the user into '),
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unassign roles to users
|
||||
*
|
||||
* @param array $unenrolment An array of unenrolment
|
||||
* @return null
|
||||
*/
|
||||
public static function role_unassign($unenrolments) {
|
||||
global $CFG, $DB;
|
||||
|
||||
// Do basic automatic PARAM checks on incoming data, using params description
|
||||
// If any problems are found then exceptions are thrown with helpful error messages
|
||||
$params = self::validate_parameters(self::role_unassign_parameters(), array('unenrolments'=>$unenrolments));
|
||||
|
||||
$transaction = $DB->start_delegated_transaction();
|
||||
|
||||
$success = true;
|
||||
|
||||
foreach ($params['unenrolments'] as $unenrolment) {
|
||||
// Ensure the current user is allowed to run this function in the unenrolment context
|
||||
$context = get_context_instance_by_id($unenrolment['contextid']);
|
||||
self::validate_context($context);
|
||||
require_capability('moodle/role:assign', $context);
|
||||
|
||||
if (!role_unassign($unenrolment['roleid'], $unenrolment['userid'], null, $unenrolment['contextid'])) {
|
||||
$success = false;
|
||||
}
|
||||
}
|
||||
|
||||
$transaction->allow_commit();
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value
|
||||
* @return external_description
|
||||
*/
|
||||
public static function role_unassign_returns() {
|
||||
return new external_value(PARAM_BOOL, 'If all unassignement succeed returns true');
|
||||
}
|
||||
|
||||
}
|
@ -116,4 +116,20 @@ $functions = array(
|
||||
'description' => 'Update users.',
|
||||
'type' => 'write',
|
||||
),
|
||||
|
||||
'moodle_enrol_role_assign' => array(
|
||||
'classname' => 'moodle_enrol_external',
|
||||
'methodname' => 'role_assign',
|
||||
'classpath' => 'enrol/externallib.php',
|
||||
'description' => 'Enrol users.',
|
||||
'type' => 'write',
|
||||
),
|
||||
|
||||
'moodle_enrol_role_unassign' => array(
|
||||
'classname' => 'moodle_enrol_external',
|
||||
'methodname' => 'role_unassign',
|
||||
'classpath' => 'enrol/externallib.php',
|
||||
'description' => 'Unenrol users.',
|
||||
'type' => 'write',
|
||||
),
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user