MDL-58220 oauth2: Global enable/disable for issuers.

This commit is contained in:
Damyon Wiese
2017-04-03 13:41:19 +08:00
parent 6e0d700de8
commit eca128bf47
10 changed files with 99 additions and 1 deletions
@@ -154,6 +154,20 @@ class renderer extends plugin_renderer_base {
$deleteurl = new moodle_url('/admin/tool/oauth2/issuers.php', ['id' => $issuer->get('id'), 'action' => 'delete']);
$deletelink = html_writer::link($deleteurl, $OUTPUT->pix_icon('t/delete', get_string('delete')));
$links .= ' ' . $deletelink;
// Enable / Disable.
if ($issuer->get('enabled')) {
// Disable.
$disableparams = ['id' => $issuer->get('id'), 'sesskey' => sesskey(), 'action' => 'disable'];
$disableurl = new moodle_url('/admin/tool/oauth2/issuers.php', $disableparams);
$disablelink = html_writer::link($disableurl, $OUTPUT->pix_icon('t/hide', get_string('disable')));
$links .= ' ' . $disablelink;
} else {
// Enable.
$enableparams = ['id' => $issuer->get('id'), 'sesskey' => sesskey(), 'action' => 'enable'];
$enableurl = new moodle_url('/admin/tool/oauth2/issuers.php', $enableparams);
$enablelink = html_writer::link($enableurl, $OUTPUT->pix_icon('t/show', get_string('enable')));
$links .= ' ' . $enablelink;
}
if (!$last) {
// Move down.
$params = ['id' => $issuer->get('id'), 'action' => 'movedown', 'sesskey' => sesskey()];
+11
View File
@@ -111,6 +111,17 @@ if ($mform && $mform->is_cancelled()) {
$editurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
redirect($editurl, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
}
} else if ($action == 'enable') {
require_sesskey();
core\oauth2\api::enable_issuer($issuerid);
redirect($PAGE->url, get_string('issuerenabled', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
} else if ($action == 'disable') {
require_sesskey();
core\oauth2\api::disable_issuer($issuerid);
redirect($PAGE->url, get_string('issuerdisabled', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
} else if ($action == 'delete') {
@@ -84,6 +84,8 @@ $string['deleteconfirm'] = 'Are you sure you want to delete the identity issuer
$string['deleteendpointconfirm'] = 'Are you sure you want to delete the endpoint "{$a->endpoint}" for issuer "{$a->issuer}"? Any plugins relying on this endpoint will stop working.';
$string['deleteuserfieldmappingconfirm'] = 'Are you sure you want to delete the user field mapping for issuer "{$a}"?';
$string['issuerdeleted'] = 'Identity issuer deleted';
$string['issuerenabled'] = 'Identity issuer enabled';
$string['issuerdisabled'] = 'Identity issuer disabled';
$string['endpointdeleted'] = 'Endpoint deleted';
$string['userfieldmappingdeleted'] = 'User field mapping deleted';
$string['connectsystemaccount'] = 'Connect to a system account';
+2 -1
View File
@@ -180,7 +180,8 @@ class auth extends \auth_plugin_base {
* @return boolean
*/
private function is_ready_for_login_page(\core\oauth2\issuer $issuer) {
return !empty($issuer->get('clientid')) &&
return $issuer->get('enabled') &&
!empty($issuer->get('clientid')) &&
!empty($issuer->get('clientsecret')) &&
$issuer->is_authentication_supported() &&
!empty($issuer->get('showonloginpage'));
+33
View File
@@ -616,6 +616,39 @@ class api {
return $result;
}
/**
* Disable an identity issuer.
*
* Requires moodle/site:config capability at the system context.
*
* @param int $id The id of the identity issuer to enable.
* @return boolean
*/
public static function disable_issuer($id) {
require_capability('moodle/site:config', context_system::instance());
$issuer = new issuer($id);
$issuer->set('enabled', 0);
return $issuer->update();
}
/**
* Enable an identity issuer.
*
* Requires moodle/site:config capability at the system context.
*
* @param int $id The id of the identity issuer to enable.
* @return boolean
*/
public static function enable_issuer($id) {
require_capability('moodle/site:config', context_system::instance());
$issuer = new issuer($id);
$issuer->set('enabled', 1);
return $issuer->update();
}
/**
* Delete an identity issuer.
*
+4
View File
@@ -64,6 +64,10 @@ class issuer extends persistent {
'type' => PARAM_URL,
'default' => ''
),
'enabled' => array(
'type' => PARAM_BOOL,
'default' => true
),
'showonloginpage' => array(
'type' => PARAM_BOOL,
'default' => false
+1
View File
@@ -3494,6 +3494,7 @@
<FIELD NAME="loginparams" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="Additional parameters sent for a login attempt."/>
<FIELD NAME="loginparamsoffline" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="Additional parameters sent for a login attempt to generate a refresh token."/>
<FIELD NAME="scopessupported" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="The list of scopes this service supports."/>
<FIELD NAME="enabled" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="1" SEQUENCE="false"/>
<FIELD NAME="showonloginpage" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="1" SEQUENCE="false"/>
<FIELD NAME="sortorder" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="The defined sort order."/>
</FIELDS>
+1
View File
@@ -2632,6 +2632,7 @@ function xmldb_main_upgrade($oldversion) {
$table->add_field('loginparamsoffline', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
$table->add_field('scopessupported', XMLDB_TYPE_TEXT, null, null, null, null, null);
$table->add_field('showonloginpage', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
$table->add_field('enabled', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
$table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
// Adding keys to table oauth2_issuer.
+18
View File
@@ -201,6 +201,10 @@ class repository_googledocs extends repository {
if (empty($path)) {
$path = $this->build_node_path('root', get_string('pluginname', 'repository_googledocs'));
}
if (!$this->issuer->get('enabled')) {
// Empty list of files for disabled repository.
return ['dynload' => false, 'list' => [], 'nologin' => true];
}
// We analyse the path to extract what to browse.
$trail = explode('/', $path);
@@ -416,6 +420,10 @@ class repository_googledocs extends repository {
public function get_file($reference, $filename = '') {
global $CFG;
if (!$this->issuer->get('enabled')) {
throw new repository_exception('cannotdownload', 'repository');
}
$client = $this->get_user_oauth_client();
$base = 'https://www.googleapis.com/drive/v3';
@@ -557,6 +565,10 @@ class repository_googledocs extends repository {
* @param array $options additional options affecting the file serving
*/
public function send_file($storedfile, $lifetime=null , $filter=0, $forcedownload=false, array $options = null) {
if (!$this->issuer->get('enabled')) {
throw new repository_exception('cannotdownload', 'repository');
}
$source = json_decode($storedfile->get_reference());
$fb = get_file_browser();
@@ -829,6 +841,9 @@ class repository_googledocs extends repository {
* @return string updated reference (final one before it's saved to db).
*/
public function reference_file_selected($reference, $context, $component, $filearea, $itemid) {
if (!$this->issuer->get('enabled')) {
throw new repository_exception('cannotdownload', 'repository');
}
// What we need to do here is transfer ownership to the system user (or copy)
// then set the permissions so anyone with the share link can view,
// finally update the reference to contain the share link if it was not
@@ -919,6 +934,9 @@ class repository_googledocs extends repository {
* @param int $filestatus
*/
public function get_reference_details($reference, $filestatus = 0) {
if (!$this->issuer->get('enabled')) {
throw new repository_exception('cannotdownload', 'repository');
}
if (empty($reference)) {
return get_string('unknownsource', 'repository');
}
+13
View File
@@ -198,6 +198,11 @@ class repository_skydrive extends repository {
$path = $this->build_node_path('root', get_string('pluginname', 'repository_skydrive'));
}
if (!$this->issuer->get('enabled')) {
// Empty list of files for disabled repository.
return ['dynload' => false, 'list' => [], 'nologin' => true];
}
// We analyse the path to extract what to browse.
$trail = explode('/', $path);
$uri = array_pop($trail);
@@ -392,6 +397,10 @@ class repository_skydrive extends repository {
public function get_file($reference, $filename = '') {
global $CFG;
if (!$this->issuer->get('enabled')) {
throw new repository_exception('cannotdownload', 'repository');
}
$client = $this->get_user_oauth_client();
$base = 'https://graph.microsoft.com/v1.0/';
@@ -505,6 +514,10 @@ class repository_skydrive extends repository {
* @param array $options additional options affecting the file serving
*/
public function send_file($storedfile, $lifetime=null , $filter=0, $forcedownload=false, array $options = null) {
if (!$this->issuer->get('enabled')) {
throw new repository_exception('cannotdownload', 'repository');
}
$source = json_decode($storedfile->get_reference());
$fb = get_file_browser();