From 860d51487b103f4c050d2427c35ee70dd4b2b05e Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 19 Jan 2016 14:16:11 +0100 Subject: Allow setting user provided credentials from the personal settings page --- apps/files_external/js/settings.js | 115 +++++++++++++++++++++++++++++++------ 1 file changed, 98 insertions(+), 17 deletions(-) (limited to 'apps/files_external/js/settings.js') diff --git a/apps/files_external/js/settings.js b/apps/files_external/js/settings.js index 756804238d0..16576ce7087 100644 --- a/apps/files_external/js/settings.js +++ b/apps/files_external/js/settings.js @@ -357,6 +357,9 @@ StorageConfig.prototype = { if (this.mountPoint === '') { return false; } + if (!this.backend) { + return false; + } if (this.errors) { return false; } @@ -432,6 +435,48 @@ UserStorageConfig.prototype = _.extend({}, StorageConfig.prototype, _url: 'apps/files_external/userstorages' }); +/** + * @class OCA.External.Settings.UserGlobalStorageConfig + * @augments OCA.External.Settings.StorageConfig + * + * @classdesc User external storage config + */ +var UserGlobalStorageConfig = function (id) { + this.id = id; +}; +UserGlobalStorageConfig.prototype = _.extend({}, StorageConfig.prototype, + /** @lends OCA.External.Settings.UserStorageConfig.prototype */ { + + _url: 'apps/files_external/userglobalstorages', + + /** + * Creates or saves the storage. + * + * @param {Function} [options.success] success callback, receives result as argument + * @param {Function} [options.error] error callback + */ + save: function (options) { + var self = this; + var url = OC.generateUrl('apps/files_external/usercredentials/{id}', {id: this.id}); + + $.ajax({ + type: 'PUT', + url: url, + contentType: 'application/json', + data: JSON.stringify({ + username: this.backendOptions.user, + password: this.backendOptions.password + }), + success: function (result) { + if (_.isFunction(options.success)) { + options.success(result); + } + }, + error: options.error + }); + } +}); + /** * @class OCA.External.Settings.MountOptionsDropdown * @@ -748,7 +793,7 @@ MountConfigListView.prototype = _.extend({ $.each(authMechanismConfiguration['configuration'], _.partial( this.writeParameterInput, $td, _, _, ['auth-param'] - )); + ).bind(this)); this.trigger('selectAuthMechanism', $tr, authMechanism, authMechanismConfiguration['scheme'], onCompletion @@ -770,6 +815,7 @@ MountConfigListView.prototype = _.extend({ var $tr = this.$el.find('tr#addMountPoint'); this.$el.find('tbody').append($tr.clone()); + $tr.data('storageConfig', storageConfig); $tr.find('td').last().attr('class', 'remove'); $tr.find('td.mountOptionsToggle').removeClass('hidden'); $tr.find('td').last().removeAttr('style'); @@ -805,7 +851,7 @@ MountConfigListView.prototype = _.extend({ $tr.find('td.authentication').append(selectAuthMechanism); var $td = $tr.find('td.configuration'); - $.each(backend.configuration, _.partial(this.writeParameterInput, $td)); + $.each(backend.configuration, _.partial(this.writeParameterInput, $td).bind(this)); this.trigger('selectBackend', $tr, backend.identifier, onCompletion); this.configureAuthMechanism($tr, storageConfig.authMechanism, onCompletion); @@ -866,8 +912,14 @@ MountConfigListView.prototype = _.extend({ success: function(result) { var onCompletion = jQuery.Deferred(); $.each(result, function(i, storageParams) { + var storageConfig; + var isUserProvidedAuth = storageParams.authMechanism === 'password::userprovided'; storageParams.mountPoint = storageParams.mountPoint.substr(1); // trim leading slash - var storageConfig = new self._storageConfigClass(); + if (isUserProvidedAuth) { + storageConfig = new UserGlobalStorageConfig(); + } else { + storageConfig = new self._storageConfigClass(); + } _.extend(storageConfig, storageParams); var $tr = self.newStorage(storageConfig, onCompletion); @@ -878,12 +930,16 @@ MountConfigListView.prototype = _.extend({ var $authentication = $tr.find('.authentication'); $authentication.text($authentication.find('select option:selected').text()); - // userglobal storages do not expose configuration data - $tr.find('.configuration').text(t('files_external', 'Admin defined')); - // disable any other inputs $tr.find('.mountOptionsToggle, .remove').empty(); - $tr.find('input, select, button').attr('disabled', 'disabled'); + $tr.find('input:not(.user_provided), select:not(.user_provided)').attr('disabled', 'disabled'); + + if (isUserProvidedAuth) { + $tr.find('.configuration').find(':not(.user_provided)').remove(); + } else { + // userglobal storages do not expose configuration data + $tr.find('.configuration').text(t('files_external', 'Admin defined')); + } }); onCompletion.resolve(); } @@ -918,22 +974,40 @@ MountConfigListView.prototype = _.extend({ * @return {jQuery} newly created input */ writeParameterInput: function($td, parameter, placeholder, classes) { + var hasFlag = function(flag) { + return placeholder.indexOf(flag) !== -1; + }; classes = $.isArray(classes) ? classes : []; classes.push('added'); if (placeholder.indexOf('&') === 0) { classes.push('optional'); placeholder = placeholder.substring(1); } + + if (hasFlag('@')) { + if (this._isPersonal) { + classes.push('user_provided'); + } else { + return; + } + } + var newElement; - if (placeholder.indexOf('*') === 0) { - newElement = $(''); - } else if (placeholder.indexOf('!') === 0) { + + var trimmedPlaceholder = placeholder; + var flags = ['@', '*', '!', '#', '&']; + while(flags.indexOf(trimmedPlaceholder[0]) !== -1) { + trimmedPlaceholder = trimmedPlaceholder.substr(1); + } + if (hasFlag('*')) { + newElement = $(''); + } else if (hasFlag('!')) { var checkboxId = _.uniqueId('checkbox_'); - newElement = $(''); - } else if (placeholder.indexOf('#') === 0) { + newElement = $(''); + } else if (hasFlag('#')) { newElement = $(''); } else { - newElement = $(''); + newElement = $(''); } highlightInput(newElement); $td.append(newElement); @@ -952,7 +1026,12 @@ MountConfigListView.prototype = _.extend({ // new entry storageId = null; } - var storage = new this._storageConfigClass(storageId); + + var storage = $tr.data('storageConfig'); + if (!storage) { + storage = new this._storageConfigClass(storageId); + } + storage.errors = null; storage.mountPoint = $tr.find('.mountPoint input').val(); storage.backend = $tr.find('.backend').data('identifier'); storage.authMechanism = $tr.find('.selectAuthMechanism').val(); @@ -966,7 +1045,7 @@ MountConfigListView.prototype = _.extend({ if ($input.attr('type') === 'button') { return; } - if (!isInputValid($input)) { + if (!isInputValid($input) && !$input.hasClass('optional')) { missingOptions.push(parameter); return; } @@ -994,7 +1073,7 @@ MountConfigListView.prototype = _.extend({ var users = []; var multiselect = getSelection($tr); $.each(multiselect, function(index, value) { - var pos = value.indexOf('(group)'); + var pos = (value.indexOf)?value.indexOf('(group)'): -1; if (pos !== -1) { groups.push(value.substr(0, pos)); } else { @@ -1057,7 +1136,9 @@ MountConfigListView.prototype = _.extend({ saveStorageConfig:function($tr, callback, concurrentTimer) { var self = this; var storage = this.getStorageConfig($tr); - if (!storage.validate()) { + console.log(storage); + if (!storage || !storage.validate()) { + console.log('invalid'); return false; } -- cgit v1.2.3