MDL-65959 core_badges: Unrestricted user's badger account

* Restructure the email to be backpack specific
* Amended table defintion and functions
This commit is contained in:
Peter 2019-09-25 12:38:53 +08:00 committed by Peter Dias
parent 6594c54b2e
commit 6f7fe5df9c
8 changed files with 187 additions and 68 deletions

View file

@ -762,7 +762,7 @@ function badges_local_backpack_js($checksite = false) {
}
/**
* Create the backpack with this data.
* Create the site backpack with this data.
*
* @param stdClass $data The new backpack data.
* @return boolean
@ -773,14 +773,8 @@ function badges_create_site_backpack($data) {
require_capability('moodle/badges:manageglobalsettings', $context);
$count = $DB->count_records('badge_external_backpack');
$backpack = new stdClass();
$backpack->apiversion = $data->apiversion;
$backpack->backpackapiurl = $data->backpackapiurl;
$backpack->backpackweburl = $data->backpackweburl;
$backpack->sortorder = $count;
$DB->insert_record('badge_external_backpack', $backpack);
return true;
$data->sortorder = $count;
return badges_save_external_backpack($data);
}
/**
@ -796,15 +790,8 @@ function badges_update_site_backpack($id, $data) {
require_capability('moodle/badges:manageglobalsettings', $context);
if ($backpack = badges_get_site_backpack($id)) {
$backpack = new stdClass();
$backpack->id = $id;
$backpack->apiversion = $data->apiversion;
$backpack->backpackweburl = $data->backpackweburl;
$backpack->backpackapiurl = $data->backpackapiurl;
$backpack->password = !empty($data->password) ? $data->password : '';
$backpack->oauth2_issuerid = !empty($data->oauth2_issuerid) ? $data->oauth2_issuerid : '';
$DB->update_record('badge_external_backpack', $backpack);
return true;
$data->id = $id;
return badges_save_external_backpack($data);
}
return false;
}
@ -850,6 +837,79 @@ function badges_delete_site_backpack($id) {
return false;
}
/**
* Perform the actual create/update of external bakpacks. Any checks on the validity of the id will need to be
* performed before it reaches this function.
*
* @param stdClass $data The backpack data we are updating/inserting
* @return int Returns the id of the new/updated record
*/
function badges_save_external_backpack(stdClass $data) {
global $DB;
$backpack = new stdClass();
$backpack->apiversion = $data->apiversion;
$backpack->backpackweburl = $data->backpackweburl;
$backpack->backpackapiurl = $data->backpackapiurl;
$backpack->backpackemail = $data->backpackemail;
$backpack->password = !empty($data->password) ? $data->password : '';
$backpack->oauth2_issuerid = !empty($data->oauth2_issuerid) ? $data->oauth2_issuerid : '';
if (isset($data->sortorder)) {
$backpack->sortorder = $data->sortorder;
}
$method = 'insert_record';
if (isset($data->id) && $data->id) {
$backpack->id = $data->id;
$method = 'update_record';
}
$record = $DB->$method('badge_external_backpack', $backpack, true);
$data->externalbackpackid = $data->id ?? $record;
unset($data->id);
badges_save_backpack_credentials($data);
return $data->externalbackpackid;
}
/**
* Create a backpack with the provided details. Stores the auth details of the backpack
*
* @param stdClass $data Backpack specific data.
* @return int The id of the external backpack that the credentials correspond to
*/
function badges_save_backpack_credentials(stdClass $data) {
global $DB;
if (isset($data->backpackemail) && isset($data->password)) {
$backpack = new stdClass();
$backpack->email = $data->backpackemail;
$backpack->password = !empty($data->password) ? $data->password : '';
$backpack->externalbackpackid = $data->externalbackpackid;
$backpack->userid = $data->userid ?? 0;
$backpack->backpackuid = $data->backpackuid ?? 0;
$backpack->autosync = $data->autosync ?? 0;
$id = null;
if (isset($data->badgebackpack) && $data->badgebackpack) {
$id = $data->badgebackpack;
} else if (isset($data->id) && $data->id) {
$id = $data->id;
}
$method = $id ? 'update_record' : 'insert_record';
if ($id) {
$backpack->id = $id;
}
$DB->$method('badge_backpack', $backpack);
return $backpack->externalbackpackid;
}
return $data->externalbackpackid ?? 0;
}
/**
* Is any backpack enabled that supports open badges V1?
* @return boolean
@ -884,7 +944,11 @@ function badges_get_site_backpack($id) {
function badges_get_site_backpacks() {
global $DB, $CFG;
$all = $DB->get_records('badge_external_backpack');
$sql = "SELECT beb.*
FROM {badge_external_backpack} beb
LEFT JOIN {badge_backpack} bb ON bb.externalbackpackid = beb.id
WHERE bb.id IS NULL";
$all = $DB->get_records_sql($sql);
foreach ($all as $key => $bp) {
if ($bp->id == $CFG->badges_site_backpack) {

View file

@ -2862,5 +2862,19 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2020102100.02);
}
if ($oldversion < 2020102300.01) {
$table = new xmldb_table('badge_external_backpack');
$field = new xmldb_field('backpackemail', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$table->deleteKey('backpackapiurlkey');
$table->deleteKey('backpackweburlkey');
$table->add_key('backpackapiurlkey', XMLDB_KEY_UNIQUE, ['backpackapiurl', 'backpackemail']);
// Main savepoint reached.
upgrade_main_savepoint(true, 2020102300.01);
}
return true;
}