From fd8790d5cf7d65a0ecc51355a5a3dc985213d8a2 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Fri, 2 Jun 2017 11:19:42 +0800 Subject: [PATCH] MDL-59008 repository_googledocs: add method to serve login on a popup This commit also add a new string logintoaccount to make the login button more clear. --- lang/en/repository.php | 1 + repository/googledocs/lib.php | 36 ++++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lang/en/repository.php b/lang/en/repository.php index 1a89457bfbb..53825f5c098 100644 --- a/lang/en/repository.php +++ b/lang/en/repository.php @@ -155,6 +155,7 @@ $string['linkexternal'] = 'Link external'; $string['listview'] = 'View as list'; $string['loading'] = 'Loading...'; $string['login'] = 'Login to your account'; +$string['logintoaccount'] = 'Login to your {$a} account'; $string['logout'] = 'Logout'; $string['lostsource'] = 'Error. Source is missing. {$a}'; $string['makefileinternal'] = 'Make a copy of the file'; diff --git a/repository/googledocs/lib.php b/repository/googledocs/lib.php index 0c340e833e7..401c71d56f6 100644 --- a/repository/googledocs/lib.php +++ b/repository/googledocs/lib.php @@ -131,6 +131,33 @@ class repository_googledocs extends repository { } } + /** + * Print the login in a popup. + * + * @param array|null $attr Custom attributes to be applied to popup div. + */ + public function print_login_popup($attr = null) { + global $OUTPUT, $PAGE; + + $client = $this->get_user_oauth_client(false); + $url = new moodle_url($client->get_login_url()); + $state = $url->get_param('state') . '&reloadparent=true'; + $url->param('state', $state); + + $PAGE->set_pagelayout('embedded'); + echo $OUTPUT->header(); + + $repositoryname = get_string('pluginname', 'repository_googledocs'); + + $button = new single_button($url, get_string('logintoaccount', 'repository', $repositoryname), 'post', true); + $button->add_action(new popup_action('click', $url, 'Login')); + $button->class = 'mdl-align'; + $button = $OUTPUT->render($button); + echo html_writer::div($button, '', $attr); + + echo $OUTPUT->footer(); + } + /** * Build the breadcrumb from a path. * @@ -613,8 +640,15 @@ class repository_googledocs extends repository { $storedfile->get_filename(), $forcedownload); $url->param('sesskey', sesskey()); - $userauth = $this->get_user_oauth_client($url); + $param = ($options['embed'] == true) ? false : $url; + $userauth = $this->get_user_oauth_client($param); if (!$userauth->is_logged_in()) { + if ($options['embed'] == true) { + // Due to Same-origin policy, we cannot redirect to googledocs login page. + // If the requested file is embed and the user is not logged in, add option to log in using a popup. + $this->print_login_popup(['style' => 'margin-top: 250px']); + exit; + } redirect($userauth->get_login_url()); } if ($userauth === false) {