Skip to content

Commit

Permalink
MDL-58128 googledocs: Don't copy files
Browse files Browse the repository at this point in the history
When linking - add the moodle account to the file and
organise it on the moodle side. Also allow read access with link,
but don't make any other changes to the perm/ownership.

Part of MDL-58220
  • Loading branch information
Damyon Wiese committed Apr 3, 2017
1 parent 989e14f commit 8ece1d7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 34 deletions.
1 change: 1 addition & 0 deletions lang/en/repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@
$string['upload'] = 'Upload this file';
$string['uploading'] = 'Uploading...';
$string['uploadsucc'] = 'The file has been uploaded successfully';
$string['unknownsource'] = 'Unknown source';
$string['undisclosedsource'] = '(Undisclosed)';
$string['undisclosedreference'] = '(Undisclosed)';
$string['uselatestfile'] = 'Use latest file';
Expand Down
1 change: 1 addition & 0 deletions repository/googledocs/lang/en/repository_googledocs.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
$string['supportedreturntypes'] = 'Supported files';
$string['defaultreturntype'] = 'Default return type';
$string['fileoptions'] = 'The types and defaults for returned files is configurable here. Note that all files linked externally will be updated so that the owner is the Moodle system account.';
$string['owner'] = 'Owned by: {$a}';

// Deprecated since Moodle 3.3.
$string['oauthinfo'] = '<p>To use this plugin, you must register your site with Google, as described in the documentation <a href="{$a->docsurl}">Google OAuth 2.0 setup</a>.</p><p>As part of the registration process, you will need to enter the following URL as \'Authorized Redirect URIs\':</p><p>{$a->callbackurl}</p><p>Once registered, you will be provided with a client ID and secret which can be used to configure all Google Drive and Picasa plugins.</p><p>Please also note that you will have to enable the service \'Drive API\'.</p>';
Expand Down
91 changes: 57 additions & 34 deletions repository/googledocs/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,10 @@ protected function query($q, $path = null, $page = 0) {
);
} else {
// This is a file.
$link = isset($gfile->webContentLink) ? $gfile->webContentLink : '';
$link = isset($gfile->webViewLink) ? $gfile->webViewLink : '';
if (empty($link)) {
$link = isset($gfile->webContentLink) ? $gfile->webContentLink : '';
}
if (isset($gfile->fileExtension)) {
// The file has an extension, therefore we can download it.
$source = json_encode(['id' => $gfile->id, 'exportformat' => 'download', 'link' => $link]);
Expand Down Expand Up @@ -521,7 +524,6 @@ public function callback() {
* @param array $options additional options affecting the file serving
*/
public function send_file($storedfile, $lifetime=null , $filter=0, $forcedownload=false, array $options = null) {
// TODO.
$source = json_decode($storedfile->get_reference());

if ($source->link) {
Expand Down Expand Up @@ -605,6 +607,23 @@ protected function get_file_capabilities(\repository_googledocs\rest $client, $f
return $client->call('get', $params);
}

/**
* Get simple file info for humans.
*
* @param \core\oauth2\client $client Authenticated client.
* @param string $fileid The file we are querying.
*
* @return stdClass
*/
protected function get_file_summary(\repository_googledocs\rest $client, $fileid) {
$fields = "id,name,owners";
$params = [
'fileid' => $fileid,
'fields' => $fields
];
return $client->call('get', $params);
}

/**
* Update file owner.
*
Expand Down Expand Up @@ -665,7 +684,7 @@ protected function add_writer_to_file($client, $fileid, $email) {
'role' => 'writer',
'type' => 'user'
];
$params = ['fileid' => $fileid];
$params = ['fileid' => $fileid, 'sendNotificationEmail' => 'false'];
$response = $client->call('create_permission', $params, json_encode($updateeditor));
if (empty($response->id)) {
$details = 'Cannot add user ' . $email . ' as a writer for document: ' . $fileid;
Expand Down Expand Up @@ -783,12 +802,6 @@ public function reference_file_selected($reference, $context) {
$readshareupdaterequired = true;
$ownerupdaterequired = true;
foreach ($permissions->permissions as $permission) {
if ($permission->type == 'user' &&
$permission->role == 'owner' &&
isset($permission->emailAddress) &&
$permission->emailAddress == $systemuseremail) {
$ownerupdaterequired = false;
}
if ($permission->id == 'anyoneWithLink' &&
$permission->type == 'anyone' &&
$permission->role == 'reader' &&
Expand All @@ -797,12 +810,17 @@ public function reference_file_selected($reference, $context) {
}
}

// Add Moodle as writer.
$this->add_writer_to_file($userservice, $source->id, $systemuseremail);

// Now move it to a sensible folder.
$contextlist = array_reverse($context->get_parent_contexts(true));

$parentid = 'root';
foreach ($contextlist as $context) {
// Make sure a folder exists here.
$foldername = $context->get_context_name();

$folderid = $this->folder_exists_in_folder($systemservice, $foldername, $parentid);
if ($folderid !== false) {
$parentid = $folderid;
Expand All @@ -812,38 +830,43 @@ public function reference_file_selected($reference, $context) {
}
}

// See if we have edit capability before the copy.
$fileinfo = $this->get_file_capabilities($userservice, $source->id);
$canedit = !empty($fileinfo->capabilities->canEdit);
$writerscanshare = !empty($fileinfo->writersCanShare);

// The owner was not the system user so we have to update the file.
if ($ownerupdaterequired) {
$this->move_file_from_root_to_folder($systemservice, $source->id, $parentid);

$worked = $this->update_file_owner($userservice, $source->id, $systemuseremail);
if (!$worked) {
// Updating the owner only works for "google files" like documents etc. For binary
// files we will get here.
$source = $this->copy_file($systemservice, $source->id);

$readshareupdaterequired = true;
$writerscanshare = true;
if ($readshareupdaterequired) {
$this->set_file_sharing_anyone_with_link_can_read($systemservice, $source->id);
}

if ($canedit) {
$this->add_writer_to_file($systemservice, $source->id, $useremail);
}
}
// We did not update the reference at all.
return $reference;
}

$this->move_file_from_root_to_folder($systemservice, $source->id, $parentid);
/**
* Get human readable file info from a the reference.
*
* @param string $reference
* @param int $filestatus
*/
public function get_reference_details($reference, $filestatus = 0) {
if (empty($reference)) {
return get_string('unknownsource', 'repository');
}
$source = json_decode($reference);
$systemauth = \core\oauth2\api::get_system_oauth_client($this->issuer);

if ($writerscanshare) {
// We don't want anyone but Moodle to change the sharing settings.
$this->prevent_writers_from_sharing_file($systemservice, $source->id);
if ($systemauth === false) {
return '';
}
$systemservice = new repository_googledocs\rest($systemauth);
$info = $this->get_file_summary($systemservice, $source->id);

if ($readshareupdaterequired) {
$this->set_file_sharing_anyone_with_link_can_read($systemservice, $source->id);
$owner = '';
if (!empty($info->owners[0]->displayName)) {
$owner = $info->owners[0]->displayName;
}
if ($owner) {
return get_string('owner', 'repository_googledocs', $owner);
} else {
return $info->name;
}
}

Expand Down

0 comments on commit 8ece1d7

Please sign in to comment.