Completed
Push — stable9 ( 485cb1...e094cf )
by Lukas
26:41 queued 26:23
created

apps/files_sharing/js/external.js (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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