Skip to content

Commit b8d362d

Browse files
committed
Adapted to MOODLE_39_STABLE
1 parent 746d33b commit b8d362d

5 files changed

Lines changed: 79 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Features
5959
* Automatic certificate creation
6060
* Optionally auto create users
6161
* Support for multiple identity providers
62+
* Role mapping for admin, manager and course_creator system roles
6263
* Idp initiated flow / IdP first flow / IdP unsolicited logins, eg:
6364

6465
http://idp.local/simplesaml/saml2/idp/SSOService.php?spentityid=http://moodle.local/auth/saml2/sp/metadata.php&RelayState=http://moodle.local/course/view.php?id=2
@@ -67,7 +68,6 @@ http://idp.local/simplesaml/saml2/idp/SSOService.php?spentityid=http://moodle.lo
6768
Features not yet implemented:
6869

6970
* Enrolment - this should be an enrol plugin and not in an auth plugin
70-
* Role mapping - not yet implemented
7171

7272
Branches
7373
--------

classes/auth.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,9 @@ public function saml_login_complete($attributes) {
687687
set_config('siteadmins', implode(',', $admins));
688688
}
689689

690+
// Synchronize IdP roles to moodle
691+
sync_roles($user, $attributes, $this->config);
692+
690693
// Make sure all user data is fetched.
691694
$user = get_complete_user_data('username', $user->username);
692695

lang/en/auth_saml2.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,13 @@
218218
$string['regeneratepath'] = 'Certificate path path: {$a}';
219219
$string['regenerateheader'] = 'Regenerate Private Key and Certificate';
220220
$string['regeneratesuccess'] = 'Private Key and Certificate successfully regenerated';
221+
222+
/*
223+
* Role mapping
224+
*/
225+
$string['saml_role_map'] = "Role";
226+
$string['saml_rolemapping'] = "Role Mapping";
227+
$string['saml_rolemapping_head'] = "The IdP can use it's own roles. Set in this section the mapping between IdP and Moodle roles. Accepts multiple valued comma separated. Example: admin,owner,superuser.";
228+
$string['saml_role_siteadmin_map'] = "Site administrators";
229+
$string['saml_role_manager_map'] = "Manager";
230+
$string['saml_role_coursecreator_map'] = "Course creator";

locallib.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,32 @@ function auth_saml2_admin_nav($title, $url) {
543543
$PAGE->set_heading(get_string('pluginname', 'auth_saml2') . ': ' . $title);
544544
$PAGE->set_title(get_string('pluginname', 'auth_saml2') . ': ' . $title);
545545
}
546+
547+
/**
548+
* Map user roles from Roles array
549+
*
550+
*/
551+
function sync_roles($user,$attributes,$config) {
552+
global $CFG, $DB;
553+
554+
// Process siteadmin (special, they are stored at mdl_config)
555+
if(in_array($config->saml_role_siteadmin_map,$attributes['Role'])){
556+
$siteadmins = explode(',', $CFG->siteadmins);
557+
if (!in_array($user->id, $siteadmins)) {
558+
$siteadmins[] = $user->id;
559+
$newAdmins = implode(',', $siteadmins);
560+
set_config('siteadmins', $newAdmins);
561+
}
562+
}
563+
564+
// Process coursecreator and manager
565+
$syscontext = context_system::instance();
566+
if(in_array($config->saml_role_coursecreator_map,$attributes['Role'])){
567+
$creatorrole = $DB->get_record('role', array('shortname'=>'coursecreator'), '*', MUST_EXIST);
568+
role_assign($creatorrole->id, $user->id, $syscontext);
569+
}
570+
if (in_array($config->saml_role_manager_map, $attributes['Role'])) {
571+
$managerrole = $DB->get_record('role', array('shortname'=>'manager'), '*', MUST_EXIST);
572+
role_assign($managerrole->id, $user->id, $syscontext);
573+
}
574+
}

settings.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,42 @@
298298
$authplugin->get_ssp_version()
299299
));
300300

301+
// Role mapping
302+
$name = 'auth_saml2/field_map_role';
303+
$title = get_string('saml_role_map', 'auth_saml2');
304+
$description = '';
305+
$default = '';
306+
$setting = new admin_setting_configtext($name, $title, $description, $default, PARAM_ALPHANUMEXT);
307+
$settings->add($setting);
308+
309+
$settings->add(
310+
new admin_setting_heading(
311+
'auth_saml2/saml_rolemapping',
312+
new lang_string('saml_rolemapping', 'auth_saml2'),
313+
new lang_string('saml_rolemapping_head', 'auth_saml2')
314+
)
315+
);
316+
317+
$name = 'auth_saml2/saml_role_siteadmin_map';
318+
$title = get_string('saml_role_siteadmin_map', 'auth_saml2');
319+
$description = '';
320+
$default = '';
321+
$setting = new admin_setting_configtext($name, $title, $description, $default, PARAM_ALPHANUMEXT);
322+
$settings->add($setting);
323+
324+
$name = 'auth_saml2/saml_role_manager_map';
325+
$title = get_string('saml_role_manager_map', 'auth_saml2');
326+
$description = '';
327+
$default = '';
328+
$setting = new admin_setting_configtext($name, $title, $description, $default, PARAM_ALPHANUMEXT);
329+
$settings->add($setting);
330+
331+
$name = 'auth_saml2/saml_role_coursecreator_map';
332+
$title = get_string('saml_role_coursecreator_map', 'auth_saml2');
333+
$description = '';
334+
$default = '';
335+
$setting = new admin_setting_configtext($name, $title, $description, $default, PARAM_ALPHANUMEXT);
336+
$settings->add($setting);
301337

302338
// Display locking / mapping of profile fields.
303339
$help = get_string('auth_updatelocal_expl', 'auth');

0 commit comments

Comments
 (0)