Skip to content

Commit

Permalink
MDL-58280 fileconverter_googledrive: More helpful errors
Browse files Browse the repository at this point in the history
Detect some config errors and give a better error message.
  • Loading branch information
Damyon Wiese committed Apr 20, 2017
1 parent 32f01c5 commit 0b1a14a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
4 changes: 4 additions & 0 deletions files/converter/googledrive/classes/converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ public static function are_requirements_met() {
return false;
}

if (!$issuer->get('enabled')) {
return false;
}

if (!$issuer->is_system_account_connected()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@
$string['test_conversion'] = 'Test document conversion';
$string['test_conversionready'] = 'This document converter is configured properly.';
$string['test_conversionnotready'] = 'This document converter is not configured properly.';
$string['test_issuerinvalid'] = 'The OAuth service in the document converter settings is set to an invalid value.';
$string['test_issuernotenabled'] = 'The OAuth service set in the document converter settings is not enabled.';
$string['test_issuernotconnected'] = 'The OAuth service set in the document converter settings does not have a system account connected.';
$string['test_issuernotset'] = 'The OAuth service needs to be set in the document converter settings.';
31 changes: 30 additions & 1 deletion files/converter/googledrive/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,36 @@
$msg .= html_writer::link($pdflink, get_string('test_conversion', 'fileconverter_googledrive'));
$msg .= html_writer::empty_tag('br');
} else {
$msg = $OUTPUT->notification(get_string('test_conversionnotready', 'fileconverter_googledrive'), 'warning');

// Diagnostics time.
$issuerid = get_config('fileconverter_googledrive', 'issuerid');
$msg = '';
if (empty($issuerid)) {
$msg = $OUTPUT->notification(get_string('test_issuernotset', 'fileconverter_googledrive'), 'warning');
}

if (empty($msg)) {
$issuer = \core\oauth2\api::get_issuer($issuerid);
if (empty($issuer)) {
$msg = $OUTPUT->notification(get_string('test_issuerinvalid', 'fileconverter_googledrive'), 'warning');
}
}

if (empty($msg)) {
if (!$issuer->get('enabled')) {
$msg = $OUTPUT->notification(get_string('test_issuernotenabled', 'fileconverter_googledrive'), 'warning');
}
}

if (empty($msg)) {
if (!$issuer->is_system_account_connected()) {
$msg = $OUTPUT->notification(get_string('test_issuernotconnected', 'fileconverter_googledrive'), 'warning');
}
}

if (empty($msg)) {
$msg = $OUTPUT->notification(get_string('test_conversionnotready', 'fileconverter_googledrive'), 'warning');
}
}
$returl = new moodle_url('/admin/settings.php', array('section' => 'fileconvertergoogledrive'));
$msg .= $OUTPUT->continue_button($returl);
Expand Down
22 changes: 19 additions & 3 deletions lib/classes/oauth2/issuer.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,25 @@ public function is_system_account_connected() {
return false;
}
$sys = system_account::get_record(['issuerid' => $this->get('id')]);
if (!empty($sys) and !empty($sys->get('refreshtoken'))) {
return true;
if (empty($sys) || empty($sys->get('refreshtoken'))) {
return false;
}
return false;

$scopes = api::get_system_scopes_for_issuer($this);

$grantedscopes = $sys->get('grantedscopes');

$scopes = explode(' ', $scopes);

foreach ($scopes as $scope) {
if (!empty($scope)) {
if (strpos(' ' . $grantedscopes . ' ', ' ' . $scope . ' ') === false) {
// We have not been granted all the scopes that are required.
return false;
}
}
}

return true;
}
}

0 comments on commit 0b1a14a

Please sign in to comment.