Skip to content

Commit

Permalink
webservice MDL-21908 admin: use renderer into token pages + a bit of …
Browse files Browse the repository at this point in the history
…code cleaning
  • Loading branch information
mouneyrac committed Jul 27, 2010
1 parent 29ea008 commit 9c954e8
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 100 deletions.
113 changes: 59 additions & 54 deletions admin/webservice/forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,82 +22,86 @@
* @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.'/formslib.php';
require_once $CFG->libdir . '/formslib.php';

/**
* Display the authorised user settings form
* Including IP Restriction, Valid until and (TODO) capability
*/
class external_service_authorised_user_settings_form extends moodleform {
function definition() {
global $CFG, $USER, $DB;

function definition() {
$mform = $this->_form;
$data = $this->_customdata;

$mform->addElement('header', 'serviceusersettings', get_string('serviceusersettings', 'webservice'));
$mform->addElement('header', 'serviceusersettings',
get_string('serviceusersettings', 'webservice'));

$mform->addElement('text', 'iprestriction', get_string('iprestriction', 'webservice'));
$mform->addElement('text', 'iprestriction',
get_string('iprestriction', 'webservice'));
$mform->addHelpButton('iprestriction', 'iprestriction', 'webservice');

$mform->addElement('date_selector', 'validuntil', get_string('validuntil', 'webservice'), array('optional'=>true));
$mform->addElement('date_selector', 'validuntil',
get_string('validuntil', 'webservice'), array('optional' => true));
$mform->addHelpButton('validuntil', 'validuntil', 'webservice');

$this->add_action_buttons(true, get_string('updateusersettings', 'webservice'));

$this->set_data($data);
}
}

}

class external_service_form extends moodleform {
function definition() {
global $CFG, $USER;

function definition() {
$mform = $this->_form;
$service = $this->_customdata;

$mform->addElement('header', 'extservice', get_string('externalservice', 'webservice'));
$mform->addElement('header', 'extservice',
get_string('externalservice', 'webservice'));

$mform->addElement('text', 'name', get_string('name'));
$mform->addRule('name', get_string('required'), 'required', null, 'client');
$mform->addElement('advcheckbox', 'enabled', get_string('enabled', 'webservice'));
$mform->addElement('advcheckbox', 'restrictedusers', get_string('restrictedusers', 'webservice'));

$mform->addElement('advcheckbox', 'restrictedusers',
get_string('restrictedusers', 'webservice'));

/// needed to select automatically the 'No required capability" option
$currentcapabilityexist = false;
if (empty($service->requiredcapability))
{
$service->requiredcapability = "norequiredcapability";
$currentcapabilityexist = true;
if (empty($service->requiredcapability)) {
$service->requiredcapability = "norequiredcapability";
$currentcapabilityexist = true;
}



// Prepare the list of capabilites to choose from
$systemcontext = get_context_instance(CONTEXT_SYSTEM);
$allcapabilities = fetch_context_capabilities($systemcontext);
$capabilitychoices = array();
$capabilitychoices['norequiredcapability'] = get_string('norequiredcapability', 'webservice');
$capabilitychoices['norequiredcapability'] = get_string('norequiredcapability',
'webservice');
foreach ($allcapabilities as $cap) {
$capabilitychoices[$cap->name] = $cap->name . ': ' . get_capability_string($cap->name);
if (!empty($service->requiredcapability) && $service->requiredcapability == $cap->name) {
$capabilitychoices[$cap->name] = $cap->name . ': '
. get_capability_string($cap->name);
if (!empty($service->requiredcapability)
&& $service->requiredcapability == $cap->name) {
$currentcapabilityexist = true;
}
}

$mform->addElement('searchableselector', 'requiredcapability', get_string('requiredcapability', 'webservice'), $capabilitychoices);
$mform->addElement('searchableselector', 'requiredcapability',
get_string('requiredcapability', 'webservice'), $capabilitychoices);
$mform->addHelpButton('requiredcapability', 'requiredcapability', 'webservice');
$mform->setAdvanced('requiredcapability');
/// display notification error if the current requiredcapability doesn't exist anymore
if(empty($currentcapabilityexist)) {
if (empty($currentcapabilityexist)) {
global $OUTPUT;
$mform->addElement('static', 'capabilityerror', '', $OUTPUT->notification(get_string('selectedcapabilitydoesntexit','webservice', $service->requiredcapability)));
$mform->addElement('static', 'capabilityerror', '',
$OUTPUT->notification(get_string('selectedcapabilitydoesntexit',
'webservice', $service->requiredcapability)));
$service->requiredcapability = "norequiredcapability";
}

$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);

Expand All @@ -120,32 +124,32 @@ function validation($data, $files) {
$errors = parent::validation($data, $files);
return $errors;
}
}

}

class external_service_functions_form extends moodleform {

function definition() {
global $CFG, $USER, $DB;
global $CFG;

$mform = $this->_form;
$data = $this->_customdata;

$mform->addElement('header', 'addfunction', get_string('addfunctions', 'webservice'));

$select = "name NOT IN (SELECT s.functionname
FROM {external_services_functions} s
WHERE s.externalserviceid = :sid
)";

$functions = $DB->get_records_select_menu('external_functions', $select, array('sid'=>$data['id']), 'name', 'id, name');
require_once($CFG->dirroot . "/webservice/lib.php");
$webservicemanager = new webservice();
$functions = $webservicemanager->get_not_associated_external_functions($data['id']);

//we add the descriptions to the functions
foreach ($functions as $functionid => $functionname) {
$function = external_function_info($functionname); //retrieve full function information (including the description)
$functions[$functionid] = $function->name.':'.$function->description;
//retrieve full function information (including the description)
$function = external_function_info($functionname);
$functions[$functionid] = $function->name . ':' . $function->description;
}

$mform->addElement('searchableselector', 'fid', get_string('name'), $functions, array('multiple' => true));
$mform->addElement('searchableselector', 'fid', get_string('name'),
$functions, array('multiple' => true));

$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
Expand All @@ -157,32 +161,30 @@ function definition() {

$this->set_data($data);
}
}

}

class web_service_token_form extends moodleform {

function definition() {
global $CFG, $USER, $DB;
global $USER, $DB;

$mform = $this->_form;
$data = $this->_customdata;

$mform->addElement('header', 'token', get_string('token', 'webservice'));

if (empty($data->nouserselection)) {
//user searchable selector
//user searchable selector - get all users (admin and guest included)
$sql = "SELECT u.id, u.firstname, u.lastname
FROM {user} u
WHERE NOT EXISTS ( SELECT 1
FROM {user} au, {role_assignments} r
WHERE au.id=u.id AND r.roleid = 1 AND r.userid = au.id)
ORDER BY u.lastname";
$users = $DB->get_records_sql($sql,array());
$users = $DB->get_records_sql($sql, array());
$options = array();
foreach ($users as $userid => $user) {
$options[$userid] = $user->firstname. " " . $user->lastname;
$options[$userid] = $user->firstname . " " . $user->lastname;
}
$mform->addElement('searchableselector', 'user', get_string('user'),$options);
$mform->addElement('searchableselector', 'user', get_string('user'), $options);
$mform->addRule('user', get_string('required'), 'required', null, 'client');
}

Expand All @@ -191,20 +193,22 @@ function definition() {
$options = array();
$systemcontext = get_context_instance(CONTEXT_SYSTEM);
foreach ($services as $serviceid => $service) {
//check that the user has the required capability (only for generation by the profil page)
if (empty($data->nouserselection)
|| empty($service->requiredcapability)
|| has_capability($service->requiredcapability, $systemcontext, $USER->id)) {
//check that the user has the required capability
//(only for generation by the profil page)
if (empty($data->nouserselection)
|| empty($service->requiredcapability)
|| has_capability($service->requiredcapability, $systemcontext, $USER->id)) {
$options[$serviceid] = $service->name;
}
}
$mform->addElement('select', 'service', get_string('service', 'webservice'),$options);
$mform->addElement('select', 'service', get_string('service', 'webservice'), $options);
$mform->addRule('service', get_string('required'), 'required', null, 'client');


$mform->addElement('text', 'iprestriction', get_string('iprestriction', 'webservice'));

$mform->addElement('date_selector', 'validuntil', get_string('validuntil', 'webservice'), array('optional'=>true));
$mform->addElement('date_selector', 'validuntil',
get_string('validuntil', 'webservice'), array('optional' => true));

$mform->addElement('hidden', 'action');
$mform->setType('action', PARAM_ACTION);
Expand All @@ -218,4 +222,5 @@ function validation($data, $files) {
$errors = parent::validation($data, $files);
return $errors;
}

}
79 changes: 37 additions & 42 deletions admin/webservice/tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
* @copyright 2009 Moodle Pty Ltd (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once('../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once('forms.php');
require_once($CFG->libdir.'/externallib.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->dirroot . '/admin/webservice/forms.php');
require_once($CFG->libdir . '/externallib.php');

$action = optional_param('action', '', PARAM_ACTION);
$action = optional_param('action', '', PARAM_ACTION);
$tokenid = optional_param('tokenid', '', PARAM_SAFEDIR);
$confirm = optional_param('confirm', 0, PARAM_BOOL);

Expand All @@ -38,7 +37,8 @@
$PAGE->navbar->add(get_string('administrationsite'));
$PAGE->navbar->add(get_string('plugins', 'admin'));
$PAGE->navbar->add(get_string('webservices', 'webservice'));
$PAGE->navbar->add(get_string('managetokens', 'webservice'), new moodle_url('/admin/settings.php?section=webservicetokens'));
$PAGE->navbar->add(get_string('managetokens', 'webservice'),
new moodle_url('/admin/settings.php?section=webservicetokens'));
if ($action == "delete") {
$PAGE->navbar->add(get_string('delete'));
} else {
Expand All @@ -47,31 +47,29 @@

admin_externalpage_setup('addwebservicetoken');

require_login();
require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));

$returnurl = "$CFG->wwwroot/$CFG->admin/settings.php?section=webservicetokens";

////////////////////////////////////////////////////////////////////////////////
// process actions

if (!confirm_sesskey()) {
redirect($returnurl);
}
$tokenlisturl = new moodle_url("/admin/settings.php", array('section' => 'webservicetokens'));

switch ($action) {

case 'create':
$mform = new web_service_token_form(null, array('action' => 'create'));
$data = $mform->get_data();
if ($mform->is_cancelled()) {
redirect($returnurl);
} else if ($data = $mform->get_data()) {
ignore_user_abort(true); // no interruption here!
external_generate_token(EXTERNAL_TOKEN_PERMANENT, $data->service, $data->user, get_context_instance(CONTEXT_SYSTEM), $data->validuntil, $data->iprestriction);
redirect($returnurl);
redirect($tokenlisturl);
} else if ($data and confirm_sesskey()) {
ignore_user_abort(true);
//TODO improvement: either move this function from externallib.php to webservice/lib.php
// either move most of webservicelib.php functions into externallib.php
// (create externalmanager class) MDL-23523
external_generate_token(EXTERNAL_TOKEN_PERMANENT, $data->service,
$data->user, get_context_instance(CONTEXT_SYSTEM),
$data->validuntil, $data->iprestriction);
redirect($tokenlisturl);
}

//ask for function id
//OUTPUT: create token form
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('createtoken', 'webservice'));
$mform->display();
Expand All @@ -80,29 +78,26 @@
break;

case 'delete':
$sql = "SELECT
t.id, t.token, u.firstname, u.lastname, s.name
FROM
{external_tokens} t, {user} u, {external_services} s
WHERE
t.creatorid=? AND t.id=? AND t.tokentype = ".EXTERNAL_TOKEN_PERMANENT." AND s.id = t.externalserviceid AND t.userid = u.id";
$token = $DB->get_record_sql($sql, array($USER->id, $tokenid), MUST_EXIST); //must be the token creator
if (!$confirm) {
echo $OUTPUT->header();
$optionsyes = array('tokenid'=>$tokenid, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey());
$optionsno = array('section'=>'webservicetokens', 'sesskey'=>sesskey());
$formcontinue = new single_button(new moodle_url('/admin/webservice/tokens.php', $optionsyes), get_string('delete'));
$formcancel = new single_button(new moodle_url('/admin/settings.php', $optionsno), get_string('cancel'), 'get');
echo $OUTPUT->confirm(get_string('deletetokenconfirm', 'webservice', (object)array('user'=>$token->firstname." ".$token->lastname, 'service'=>$token->name)), $formcontinue, $formcancel);
echo $OUTPUT->footer();
die;
require_once($CFG->dirroot . "/webservice/lib.php");
$webservicemanager = new webservice();
$token = $webservicemanager->get_created_by_user_ws_token($USER->id, $tokenid);

//Delete the token
if ($confirm and confirm_sesskey()) {
$webservicemanager->delete_user_ws_token($token->id);
redirect($tokenlisturl);
}
$DB->delete_records('external_tokens', array('id'=>$token->id));
redirect($returnurl);

////OUTPUT: display delete token confirmation box
echo $OUTPUT->header();
$renderer = $PAGE->get_renderer('core', 'webservice');
echo $renderer->admin_delete_token_confirmation($token);
echo $OUTPUT->footer();
die;
break;

default:
//wrong url access
redirect($tokenlisturl);
break;
}

redirect($returnurl);
Loading

0 comments on commit 9c954e8

Please sign in to comment.