MDL-59008 repository_onedrive: add method to serve login on a popup

This commit is contained in:
Simey Lameze 2017-06-02 11:21:09 +08:00
parent fd8790d5cf
commit 1ba1412fa3

View File

@ -127,6 +127,33 @@ class repository_onedrive 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_onedrive');
$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.
*
@ -564,8 +591,16 @@ class repository_onedrive 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 onedrive 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) {
@ -703,8 +738,10 @@ class repository_onedrive extends repository {
* @return boolean
*/
protected function set_file_sharing_anyone_with_link_can_read(\repository_onedrive\rest $client, $fileid) {
$type = (isset($this->options['embed']) && $this->options['embed'] == true) ? 'embed' : 'view';
$updateread = [
'type' => 'view',
'type' => $type,
'scope' => 'anonymous'
];
$params = ['fileid' => $fileid];