external.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2014-2016 ownCloud, Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. (function() {
  7. OCA.Sharing = OCA.Sharing || {}
  8. /**
  9. * Shows "add external share" dialog.
  10. *
  11. * @param {Object} share the share
  12. * @param {String} share.remote remote server URL
  13. * @param {String} share.owner owner name
  14. * @param {String} share.name name of the shared folder
  15. * @param {String} share.token authentication token
  16. * @param {boolean} passwordProtected true if the share is password protected
  17. * @param {Function} callback the callback
  18. */
  19. OCA.Sharing.showAddExternalDialog = function(share, passwordProtected, callback) {
  20. var remote = share.remote;
  21. var owner = share.ownerDisplayName || share.owner;
  22. var name = share.name;
  23. var remoteClean = (remote.substr(0, 8) === 'https://') ? remote.substr(8) : remote.substr(7);
  24. if (!passwordProtected) {
  25. OC.dialogs.confirm(
  26. t(
  27. 'files_sharing',
  28. 'Do you want to add the remote share {name} from {owner}@{remote}?',
  29. { name: name, owner: owner, remote: remoteClean }
  30. ),
  31. t('files_sharing', 'Remote share'),
  32. function(result) {
  33. callback(result, share);
  34. },
  35. true
  36. ).then(this._adjustDialog);
  37. } else {
  38. OC.dialogs.prompt(
  39. t(
  40. 'files_sharing',
  41. 'Do you want to add the remote share {name} from {owner}@{remote}?',
  42. { name: name, owner: owner, remote: remoteClean }
  43. ),
  44. t('files_sharing', 'Remote share'),
  45. function(result, password) {
  46. share.password = password;
  47. callback(result, share);
  48. },
  49. true,
  50. t('files_sharing', 'Remote share password'),
  51. true
  52. ).then(this._adjustDialog);
  53. }
  54. };
  55. OCA.Sharing._adjustDialog = function() {
  56. var $dialog = $('.oc-dialog:visible');
  57. var $buttons = $dialog.find('button');
  58. // hack the buttons
  59. $dialog.find('.ui-icon').remove();
  60. $buttons.eq(1).text(t('core', 'Cancel'));
  61. $buttons.eq(2).text(t('files_sharing', 'Add remote share'));
  62. };
  63. OCA.Sharing.ExternalShareDialogPlugin = {
  64. filesApp: null,
  65. attach: function(filesApp) {
  66. var self = this;
  67. this.filesApp = filesApp;
  68. this.processIncomingShareFromUrl();
  69. if (!$('#header').find('div.notifications').length) {
  70. // No notification app, display the modal
  71. this.processSharesToConfirm();
  72. }
  73. $('body').on('OCA.Notification.Action', function(e) {
  74. if (e.notification.app === 'files_sharing' && e.notification.object_type === 'remote_share' && e.action.type === 'POST') {
  75. // User accepted a remote share reload
  76. self.filesApp.fileList.reload();
  77. }
  78. });
  79. },
  80. /**
  81. * Process incoming remote share that might have been passed
  82. * through the URL
  83. */
  84. processIncomingShareFromUrl: function() {
  85. var fileList = this.filesApp.fileList;
  86. var params = OC.Util.History.parseUrlQuery();
  87. // manually add server-to-server share
  88. if (params.remote && params.token && params.name) {
  89. var callbackAddShare = function(result, share) {
  90. var password = share.password || '';
  91. if (result) {
  92. $.post(
  93. OC.generateUrl('apps/federatedfilesharing/askForFederatedShare'),
  94. {
  95. remote: share.remote,
  96. token: share.token,
  97. owner: share.owner,
  98. ownerDisplayName: share.ownerDisplayName || share.owner,
  99. name: share.name,
  100. password: password
  101. }
  102. ).done(function(data) {
  103. if (data.hasOwnProperty('legacyMount')) {
  104. fileList.reload();
  105. } else {
  106. OC.Notification.showTemporary(data.message);
  107. }
  108. }).fail(function(data) {
  109. OC.Notification.showTemporary(JSON.parse(data.responseText).message);
  110. });
  111. }
  112. };
  113. // clear hash, it is unlikely that it contain any extra parameters
  114. location.hash = '';
  115. params.passwordProtected = parseInt(params.protected, 10) === 1;
  116. OCA.Sharing.showAddExternalDialog(
  117. params,
  118. params.passwordProtected,
  119. callbackAddShare
  120. );
  121. }
  122. },
  123. /**
  124. * Retrieve a list of remote shares that need to be approved
  125. */
  126. processSharesToConfirm: function() {
  127. var fileList = this.filesApp.fileList;
  128. // check for new server-to-server shares which need to be approved
  129. $.get(OC.generateUrl('/apps/files_sharing/api/externalShares'), {}, function(shares) {
  130. var index;
  131. for (index = 0; index < shares.length; ++index) {
  132. OCA.Sharing.showAddExternalDialog(
  133. shares[index],
  134. false,
  135. function(result, share) {
  136. if (result) {
  137. // Accept
  138. $.post(OC.generateUrl('/apps/files_sharing/api/externalShares'), {id: share.id})
  139. .then(function() {
  140. fileList.reload();
  141. });
  142. } else {
  143. // Delete
  144. $.ajax({
  145. url: OC.generateUrl('/apps/files_sharing/api/externalShares/'+share.id),
  146. type: 'DELETE'
  147. });
  148. }
  149. }
  150. );
  151. }});
  152. }
  153. };
  154. })(OC, OCA);
  155. OC.Plugins.register('OCA.Files.App', OCA.Sharing.ExternalShareDialogPlugin);