Skip to content

Commit

Permalink
MDL17980 mnet improvements
Browse files Browse the repository at this point in the history
* Allow multiple roles to be allocated by identity provider
* Allow existing enrolment plugins to manage mnet enrolments


Author: Peter Bulmer <[email protected]>
  • Loading branch information
peterbulmer committed Aug 3, 2009
1 parent ab97dbe commit 1b509b3
Show file tree
Hide file tree
Showing 30 changed files with 1,920 additions and 110 deletions.
2 changes: 2 additions & 0 deletions admin/mnet/mnet_review.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
$tabs[] = new tabobject('mnetthemes', 'mnet_themes.php?step=list&amp;hostid='.$mnet_peer->id, $strmnetthemes, $strmnetthemes, false);
if ($mnet_peer->application->name == 'moodle') {
$tabs[] = new tabobject('mnetlog', $logurl, $strmnetlog, $strmnetlog, false);
$tabs[] = new tabobject('rolespermitted', 'mnet_rolespermitted.php?hostid='.$mnet_peer->id, $strrolespermitted, $strrolespermitted, false);
$tabs[] = new tabobject('rolemapping', 'mnet_rolemapping.php?hostid='.$mnet_peer->id, $strrolemapping, $strrolemapping, false);
}
} else {
$tabs[] = new tabobject('mnetdetails', '#', $strmnetedithost, $strmnetedithost, false);
Expand Down
100 changes: 100 additions & 0 deletions admin/mnet/mnet_rolemapping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
require_once('mnet_rolemapping_form.php');
require_once($CFG->dirroot .'/mnet/lib.php');

$mnethostid = required_param('hostid', PARAM_INT);
$mnetpeer = mnet_get_peer_host($mnethostid);
$PAGE->set_generaltype('form');
$PAGE->set_url('admin/mnet/mnet_rolemapping.php', array('hostid' => $mnethostid));

$pagetitle = get_string('mnetmaproles');
$extranavlinks = array(
array('name' => 'Administration', 'link' => '', 'type'=> 'title'),
array('name' => 'Networking', 'link' => '', 'type' => 'title'),
array('name' => 'Peers', 'link' => $CFG->wwwroot . '/admin/mnet/peers.php', 'type' => 'title'),
array('name' => $mnetpeer->name, 'link' => $CFG->wwwroot . '/admin/mnet/peers.php?hostid='.$mnethostid, 'type' => 'title'),
array('name' => $pagetitle, 'link' => '', 'type' => 'title'),
);
$navigation = build_navigation($extranavlinks);
print_header_simple($pagetitle, $pagetitle, $navigation, '', '', false);

$mform = new rolemapping_form();
$rolessql =
'SELECT r.id, r.shortname, r.name, rm.remoterole, rm.id as mapid ' .
'FROM {role} r ' .
' LEFT JOIN {mnet_role_mapping} rm ON rm.localrole = r.id AND rm.mnethost = ? ';
$rolesparams = array($mnethostid);
if ($mform->is_cancelled()){
redirect($CFG->wwwroot . '/admin/mnet/peers.php?hostid=' . $mnethostid, get_string('changescancelled'), 1);
} else if ($fromform=$mform->get_data()) {
$roles = $DB->get_records_sql($rolessql, $rolesparams);
$usersinqueue = 0;

if (!empty($roles)) {
foreach ($roles as $role) {
if (isset($fromform->{'rolemapping-' . $role->id})) {
$newchoice = $fromform->{'rolemapping-' . $role->id};
} else {
// User has somehow supplied the form without saying what to do with this role - assume they mean no role mapping:
$newchoice = -1;
}
$toform['rolemapping-' . $role->id] = $newchoice;
// Don't actually store non-mappings in db:
if ($newchoice == -1) {
$newchoice = NULL;
}
if ($newchoice === $role->remoterole) {
// No change in this role mapping, nothing to do
continue;
}
// Role mapping has changed - get everyone with this local role in queue to get their remote role updated.
$usersinqueue += manage_role_mapping($mnethostid, $role->id);

$rolemappingobj = new stdclass;
$rolemappingobj->mnethost = $mnethostid;
$rolemappingobj->localrole = $role->id;
$rolemappingobj->remoterole = $newchoice;
if (isset($role->remoterole)) {
if ($newchoice === NULL) {
$DB->delete_records('mnet_role_mapping', array('id' => $role->mapid));
} else {
$rolemappingobj->id = $role->mapid;
$DB->update_record('mnet_role_mapping', $rolemappingobj);
}
} else {
$DB->insert_record('mnet_role_mapping', $rolemappingobj);
}
}
}
print_string('settingssaved');
echo '<br />';
if ($usersinqueue) {
print_string('mnetusersinqueue', 'moodle', $usersinqueue);
}

$toform['hostid'] = $mnethostid;
$mform->set_data($toform);
$mform->display();
print_footer();

} else {
$toform = array();
$roles = $DB->get_records_sql($rolessql, $rolesparams);
if (!empty($roles)) {
foreach ($roles as $role) {
if (isset($role->remoterole)) {
$toform['rolemapping-' . $role->id] = $role->remoterole;
} else {
$toform['rolemapping-' . $role->id] = -1;
}
}
}
$toform['hostid'] = $mnethostid;

$mform->set_data($toform);
$mform->display();
print_footer();

}
?>
80 changes: 80 additions & 0 deletions admin/mnet/mnet_rolemapping_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
require_once ('../../config.php');
require_once ($CFG->libdir . '/formslib.php');

class rolemapping_form extends moodleform {

function definition() {

global $CFG;
$mform =& $this->_form;
$mform->addElement('header', 'mapping', get_string('mnetrolemapping'));
$mform->addElement('static', 'instructions', '',get_string('mnetrolemappinginstructions'), ' ');
$mform->addElement('hidden', 'hostid', 'yes');
$this->add_action_buttons();

}

function definition_after_data() {
global $CFG, $DB;
require_once($CFG->dirroot . '/mnet/lib.php');
$mform =& $this->_form;
$mnethostid = $mform->getElementValue('hostid');
$mnethost = $DB->get_record('mnet_host', array('id' => $mnethostid));
$remoteroleoptions = array();

$rolessql = 'SELECT r.id, r.shortname, r.name, rm.remoterole, rm.id as mapid ' .
'FROM {role} r ' .
' LEFT JOIN {mnet_role_mapping} rm ON rm.localrole = r.id and rm.mnethost = ? ' .
'ORDER BY r.id';
$rolesparams = array($mnethostid);
$roles = $DB->get_records_sql($rolessql, $rolesparams);

// Determine if we already have a role mapped to a remote role (mappings to default role don't count)
$actualremoterole = false;
foreach ($roles as $role) {
if (!empty($role->remoterole)) {
$actualremoterole = true;
break;
}
}

$remotedefaultrole = false;
if (!$actualremoterole) {
// Remote peer may be old & unable to tell us what roles it shares
// See if the mnet peer has upgraded to new mnet code since we last checked
// If it knows how to tell us it's default role, it also knows how to tell us what roles it shares w/ us
$remotedefaultrole = mnet_get_default_role($mnethostid);
if (!empty($remotedefaultrole)) {
$DB->set_field('mnet_role_mapping', 'remoterole', $remotedefaultrole->id,
array('remoterole' => 0, 'mnethost' => $mnethostid));
$actualremoterole = true;
}
}
if ($actualremoterole) {
$remoteroles = mnet_get_allocatable_roles($mnethostid);
foreach ($remoteroles as $remoterole) {
$remoteroleoptions[$remoterole->id] = $mnethost->name . ' - ' . $remoterole->shortname;
}
} else {
//Still talking to an mnet peer that does't publish more than one role,
// and doesn't know how to tell us what that role is:
$remoteroleoptions[0] = $mnethost->name . ' - Default Role';
}

$remoteroleoptions[-1] = 'No Role';
foreach ($roles as $role) {
$mform->addElement('select', 'rolemapping-' . $role->id,
$role->name . ' (' . $role->shortname . ') ',
$remoteroleoptions);
if (!empty($role->remoterole) && !isset($remoteroleoptions[$role->remoterole])) {
//The remote role that this role is currently mapped to isn't shared any more
$DB->delete_records('mnet_role_mapping', array('id' => $role->mapid));
manage_role_mapping($mnethostid, $role->id);
}
}
$mform->removeElement('buttonar');
$this->add_action_buttons();
}
}
?>
75 changes: 75 additions & 0 deletions admin/mnet/mnet_rolespermitted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
require_once('mnet_rolespermitted_form.php');
require_once($CFG->dirroot .'/mnet/lib.php');

$mnethostid = required_param('hostid', PARAM_INT);
$mnetpeer = mnet_get_peer_host($mnethostid);
$PAGE->set_generaltype('form');
$PAGE->set_url('admin/mnet/mnet_rolespermitted.php', array('hostid' => $mnethostid));

$pagetitle = get_string('mnetauthorisemnetroles');
$extranavlinks = array(
array('name' => 'Administration', 'link' => '', 'type' => 'title'),
array('name' => 'Networking', 'link' => '', 'type' => 'title'),
array('name' => 'Peers', 'link' => $CFG->wwwroot . '/admin/mnet/peers.php', 'type' => 'title'),
array('name' => $mnetpeer->name, 'link' => $CFG->wwwroot . '/admin/mnet/peers.php?hostid='.$mnethostid, 'type' => 'title'),
array('name' => $pagetitle, 'link' => '', 'type' => 'title'),
);
$navigation = build_navigation($extranavlinks);
print_header_simple($pagetitle, $pagetitle, $navigation, '', '', false);
$rolessql =
'SELECT ' .
' r.id, r.shortname, r.name, rp.localrole as prepublished ' .
'FROM {role} r ' .
' LEFT JOIN {mnet_role_published} rp ON rp.localrole = r.id and rp.mnethost = ? ';
$rolesparams = array($mnethostid);

$mform = new rolespermitted_form();
if ($mform->is_cancelled()){
redirect($CFG->wwwroot . '/admin/mnet/peers.php?hostid=' . $mnethostid, get_string('changescancelled'), 1);
} else if ($fromform=$mform->get_data()) {
$roles = $DB->get_records_sql($rolessql, $rolesparams);
$rolepublication = new stdclass();
$rolepublication->mnethost = $mnethostid;
foreach ($roles as $role) {
$rolepublication->localrole = $role->id;
if(isset($fromform->{$role->shortname})) {
// Role checkbox was ticked - add publication entry if not already present.
if (empty($role->prepublished)) {
$DB->insert_record('mnet_role_published', $rolepublication);
$toform[$role->shortname] = 1;
}
} else {
// Role checkbox was not ticked - delete its publication entry (if present)
if (!empty($role->prepublished)) {
$DB->delete_records('mnet_role_published',
array('mnethost' => $rolepublication->mnethost,
'localrole' => $rolepublication->localrole));
unassign_role_peer($rolepublication->localrole, $rolepublication->mnethost);
}
}
}
print_string('settingssaved');
$toform['hostid'] = $mnethostid;
$mform->set_data($toform);
$mform->display();
print_footer();

} else {
$toform = array();

$roles = $DB->get_records_sql($rolessql, $rolesparams);
foreach ($roles as $role) {
if (!empty($role->prepublished)) {
$toform[$role->shortname] = 1;
}
}
$toform['hostid'] = $mnethostid;

$mform->set_data($toform);
$mform->display();
print_footer();

}
?>
24 changes: 24 additions & 0 deletions admin/mnet/mnet_rolespermitted_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
require_once ('../../config.php');
require_once ($CFG->libdir . '/formslib.php');

class rolespermitted_form extends moodleform {

function definition() {

global $DB;
$mform =& $this->_form;
$mform->addElement('header', 'permitted', get_string('mnetpermittedroles'));
$mform->addElement('static', 'instructions', '',get_string('mnetpermittedrolesinstructions'), ' ');
$mform->addElement('hidden', 'hostid', 'yes');
$roles = $DB->get_records('role', array(), 'id');
foreach ($roles as $role) {
$mform->addElement('checkbox', $role->shortname, $role->name,'('.$role->shortname.')');
}


// buttons
$this->add_action_buttons();
}
}

2 changes: 2 additions & 0 deletions admin/mnet/peers.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
$strmnetlog = get_string('mnetlog', 'mnet');
$strmnetedithost = get_string('reviewhostdetails', 'mnet');
$strmnetthemes = get_string('mnetthemes', 'mnet');
$strrolespermitted = get_string('mnetrolespermitted');
$strrolemapping = get_string('mnetrolemapping');

if (!isset($CFG->mnet_dispatcher_mode)) set_config('mnet_dispatcher_mode', 'off');

Expand Down
10 changes: 10 additions & 0 deletions admin/roles/define.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@
$title = get_string('editingrolex', 'role', $rolenames[$roleid]->localname);
}
print_heading_with_help($title, 'roles');
$rolepublishedsql =
'SELECT distinct(localrole), localrole ' .
'FROM {mnet_role_published} ' .
'WHERE localrole = ? ';
$params = array($roleid);
$rolepublished = $DB->get_records_sql($rolepublishedsql, $params);

if (!empty($rolepublished)) {
print_box(get_string('mnetroleispublished','moodle'), 'generalbox', 'notice');
}

/// Work out some button labels.
if ($action == 'add' || $action == 'duplicate') {
Expand Down
3 changes: 0 additions & 3 deletions admin/settings/mnet.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
$ADMIN->add('mnet', new admin_externalpage('ssoaccesscontrol', get_string('ssoaccesscontrol', 'mnet'),
"$CFG->wwwroot/$CFG->admin/mnet/access_control.php",
'moodle/site:config'));
$ADMIN->add('mnet', new admin_externalpage('mnetenrol', get_string('mnetenrol', 'mnet'),
"$CFG->wwwroot/$CFG->admin/mnet/enr_hosts.php",
'moodle/site:config'));
$ADMIN->add('mnet', new admin_externalpage('trustedhosts', get_string('trustedhosts', 'mnet'),
"$CFG->wwwroot/$CFG->admin/mnet/trustedhosts.php",
'moodle/site:config'));
Expand Down
Loading

0 comments on commit 1b509b3

Please sign in to comment.