Skip to content

Commit

Permalink
webservice MDL-21524 add role_assign and role_unassign ws functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mouneyrac committed Feb 16, 2010
1 parent 5be9499 commit e9b6609
Show file tree
Hide file tree
Showing 3 changed files with 297 additions and 0 deletions.
123 changes: 123 additions & 0 deletions admin/webservice/testclient_forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,129 @@ public function definition() {

// === 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;
Expand Down
158 changes: 158 additions & 0 deletions enrol/externallib.php
Original file line number Diff line number Diff line change
@@ -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');
}

}
16 changes: 16 additions & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,20 @@
'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',
),
);

0 comments on commit e9b6609

Please sign in to comment.