Skip to content

Commit

Permalink
Properly send expiration date with all share.php calls
Browse files Browse the repository at this point in the history
There are three share.php calls for link shares:
- when changing password
- when setting expiration date
- when switching "allow public upload"

This fix makes sure that ALL the required info is sent for each of these
calls.

It fixes an issue where the expiration date was not passed when changing
the password.
  • Loading branch information
Vincent Petry committed Oct 20, 2014
1 parent 8da6e4b commit c1efef5
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 64 deletions.
119 changes: 66 additions & 53 deletions core/js/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,33 @@ OC.Share={
$('#defaultExpireMessage').show('blind');
}
$.datepicker.setDefaults(datePickerOptions);
},

/**
* Read the link share values from the share dropdown.
*
* @return {Object.<String,Object>} values
*/
_readLinkShareValues: function() {
var $dropDown = $('#dropdown');
var data = {
allowPublicUpload: $dropDown.find('#sharingDialogAllowPublicUpload').is(':checked'),
itemType: $dropDown.data('item-type'),
itemSource: $dropDown.data('item-source'),
itemSourceName: $dropDown.data('item-source-name'),
expirationDate: ''
};

if ($dropDown.find('#expirationCheckbox').is(':checked') === true) {
data.expirationDate = $dropDown.find('#expirationDate').val();
}
// Calculate permissions
if (data.allowPublicUpload) {
data.permissions = OC.PERMISSION_UPDATE + OC.PERMISSION_CREATE + OC.PERMISSION_READ;
} else {
data.permissions = OC.PERMISSION_READ;
}
return data;
}
};

Expand Down Expand Up @@ -897,7 +924,7 @@ $(document).ready(function() {
var itemType = $dropDown.data('item-type');
var itemSource = $dropDown.data('item-source');
var itemSourceName = $dropDown.data('item-source-name');
var $loading = $dropDown.find('#link .icon-loading-small');
var $loading = $dropDown.find('#link>.icon-loading-small');
var $button = $(this);

if (!$loading.hasClass('hidden')) {
Expand Down Expand Up @@ -970,35 +997,28 @@ $(document).ready(function() {

// Gather data
var $dropDown = $('#dropdown');
var allowPublicUpload = $(this).is(':checked');
var itemType = $dropDown.data('item-type');
var itemSource = $dropDown.data('item-source');
var itemSourceName = $dropDown.data('item-source-name');
var expirationDate = '';
if ($('#expirationCheckbox').is(':checked') === true) {
expirationDate = $( "#expirationDate" ).val();
}
var permissions = 0;
var $button = $(this);
var $loading = $dropDown.find('#allowPublicUploadWrapper .icon-loading-small');
var data = OC.Share._readLinkShareValues();

if (!$loading.hasClass('hidden')) {
// already in progress
return false;
}

// Calculate permissions
if (allowPublicUpload) {
permissions = OC.PERMISSION_UPDATE + OC.PERMISSION_CREATE + OC.PERMISSION_READ;
} else {
permissions = OC.PERMISSION_READ;
}

// Update the share information
$button.addClass('hidden');
$button.prop('disabled', true);
$loading.removeClass('hidden');
OC.Share.share(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', permissions, itemSourceName, expirationDate, function(data) {
OC.Share.share(
data.itemType,
data.itemSource,
OC.Share.SHARE_TYPE_LINK,
'',
data.permissions,
data.itemSourceName,
data.expirationDate,
function(data) {
$loading.addClass('hidden');
$button.removeClass('hidden');
$button.prop('disabled', false);
Expand All @@ -1008,22 +1028,19 @@ $(document).ready(function() {
$(document).on('click', '#dropdown #showPassword', function() {
$('#linkPass').toggle('blind');
if (!$('#showPassword').is(':checked') ) {
var itemType = $('#dropdown').data('item-type');
var itemSource = $('#dropdown').data('item-source');
var itemSourceName = $('#dropdown').data('item-source-name');
var allowPublicUpload = $('#sharingDialogAllowPublicUpload').is(':checked');
var permissions = 0;
var data = OC.Share._readLinkShareValues();
var $loading = $('#showPassword .icon-loading-small');

// Calculate permissions
if (allowPublicUpload) {
permissions = OC.PERMISSION_UPDATE + OC.PERMISSION_CREATE + OC.PERMISSION_READ;
} else {
permissions = OC.PERMISSION_READ;
}

$loading.removeClass('hidden');
OC.Share.share(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', permissions, itemSourceName).then(function() {
OC.Share.share(
data.itemType,
data.itemSource,
OC.Share.SHARE_TYPE_LINK,
'',
data.permissions,
data.itemSourceName,
data.expirationDate
).then(function() {
$loading.addClass('hidden');
$('#linkPassText').attr('placeholder', t('core', 'Choose a password for the public link'));
});
Expand All @@ -1035,34 +1052,30 @@ $(document).ready(function() {
$(document).on('focusout keyup', '#dropdown #linkPassText', function(event) {
var linkPassText = $('#linkPassText');
if ( linkPassText.val() != '' && (event.type == 'focusout' || event.keyCode == 13) ) {

var allowPublicUpload = $('#sharingDialogAllowPublicUpload').is(':checked');
var data = OC.Share._readLinkShareValues();
var dropDown = $('#dropdown');
var itemType = dropDown.data('item-type');
var itemSource = dropDown.data('item-source');
var itemSourceName = $('#dropdown').data('item-source-name');
var permissions = 0;
var $loading = dropDown.find('#linkPass .icon-loading-small');

// Calculate permissions
if (allowPublicUpload) {
permissions = OC.PERMISSION_UPDATE + OC.PERMISSION_CREATE + OC.PERMISSION_READ;
} else {
permissions = OC.PERMISSION_READ;
}

$loading.removeClass('hidden');
OC.Share.share(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, $('#linkPassText').val(), permissions, itemSourceName, function(data) {
$loading.addClass('hidden');
linkPassText.val('');
linkPassText.attr('placeholder', t('core', 'Password protected'));
OC.Share.share(
data.itemType,
data.itemSource,
OC.Share.SHARE_TYPE_LINK,
$('#linkPassText').val(),
data.permissions,
data.itemSourceName,
data.expirationDate,
function(data) {
$loading.addClass('hidden');
linkPassText.val('');
linkPassText.attr('placeholder', t('core', 'Password protected'));

if (oc_appconfig.core.enforcePasswordForPublicLink) {
OC.Share.showLink(data.token, "password set", itemSource);
OC.Share.updateIcon(itemType, itemSource);
if (oc_appconfig.core.enforcePasswordForPublicLink) {
OC.Share.showLink(data.token, "password set", itemSource);
OC.Share.updateIcon(itemType, itemSource);
}
}
});

);
}
});

Expand Down
116 changes: 105 additions & 11 deletions core/js/tests/specs/shareSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ describe('OC.Share tests', function() {
var $el = $('<div></div>').data('ui-autocomplete', {});
return $el;
});

$('#testArea').append('<table id="filestable" data-allow-public-upload="yes"></table>');
});
afterEach(function() {
/* jshint camelcase:false */
Expand Down Expand Up @@ -90,6 +92,20 @@ describe('OC.Share tests', function() {
// TODO: expect that other parts are rendered correctly
});
describe('Share with link', function() {
function showDropDown(itemType) {
if (_.isUndefined(itemType)) {
itemType = 'file';
}
OC.Share.showDropDown(
itemType,
123,
$container,
true,
31,
'name-' + itemType
);
}

// TODO: test ajax calls
// TODO: test password field visibility (whenever enforced or not)
it('shows share with link checkbox when allowed', function() {
Expand Down Expand Up @@ -261,23 +277,29 @@ describe('OC.Share tests', function() {
OC.generateUrl('/s/') + 'anothertoken';
expect($('#dropdown #linkText').val()).toEqual(link);
});
it('saves link share', function() {
var req, params;
showDropDown();
$('#dropdown [name=linkCheckbox]').click();
expect(fakeServer.requests.length).toEqual(1);
req = fakeServer.requests[0];
expect(req.url).toEqual(OC.webroot + '/index.php/core/ajax/share.php');
params = OC.parseQueryString(req.requestBody);
expect(params.action).toEqual('share');
expect(params.itemType).toEqual('file');
expect(params.itemSource).toEqual('123');
expect(params.shareType).toEqual('' + OC.Share.SHARE_TYPE_LINK);
expect(params.shareWith).toEqual('');
expect(params.permissions).toEqual('1');
expect(params.itemSourceName).toEqual('name-file');
expect(params.expirationDate).toEqual('');
});
describe('expiration date', function() {
var shareData;
var shareItem;
var clock;
var expectedMinDate;

function showDropDown() {
OC.Share.showDropDown(
'file',
123,
$container,
true,
31,
'folder'
);
}

beforeEach(function() {
// pick a fake date
clock = sinon.useFakeTimers(new Date(2014, 0, 20, 14, 0, 0).getTime());
Expand Down Expand Up @@ -391,6 +413,78 @@ describe('OC.Share tests', function() {
expect($.datepicker._defaults.minDate).toEqual(expectedMinDate);
expect($.datepicker._defaults.maxDate).toEqual(new Date(2014, 0, 27, 0, 0, 0, 0));
});
it('saves expiration date after changing', function() {
var req, params;
showDropDown();
$('#dropdown [name=linkCheckbox]').click();
expect(fakeServer.requests.length).toEqual(1);
$('#dropdown [name=expirationCheckbox]').click();
$('#dropdown #expirationDate').val('27-01-2014').trigger('change');
expect(fakeServer.requests.length).toEqual(2);
req = fakeServer.requests[1];
expect(req.url).toEqual(OC.webroot + '/index.php/core/ajax/share.php');
params = OC.parseQueryString(req.requestBody);
expect(params.action).toEqual('setExpirationDate');
expect(params.itemType).toEqual('file');
expect(params.itemSource).toEqual('123');
expect(params.date).toEqual('27-01-2014');
});
});
describe('password protect', function() {
it('saves share after setting password', function() {
var req, params;
showDropDown();
$('#dropdown [name=linkCheckbox]').click();
expect(fakeServer.requests.length).toEqual(1);
$('#dropdown [name=showPassword]').click();
$('#dropdown #expirationCheckbox').click();
$('#dropdown #expirationDate').val('27-01-2014');
$('#dropdown #linkPassText').val('thepassword').trigger('focusout');
expect(fakeServer.requests.length).toEqual(2);
req = fakeServer.requests[1];
expect(req.url).toEqual(OC.webroot + '/index.php/core/ajax/share.php');
params = OC.parseQueryString(req.requestBody);
expect(params.action).toEqual('share');
expect(params.itemType).toEqual('file');
expect(params.itemSource).toEqual('123');
expect(params.shareType).toEqual('' + OC.Share.SHARE_TYPE_LINK);
expect(params.shareWith).toEqual('thepassword');
expect(params.permissions).toEqual('1');
expect(params.itemSourceName).toEqual('name-file');
expect(params.expirationDate).toEqual('27-01-2014');
});
});
describe('allow upload', function() {
it('does not show checkbox for files', function() {
showDropDown('file');
$('#dropdown [name=linkCheckbox]').click();
expect($('#dropdown [name=allowPublicUpload]').length).toEqual(0);
});
it('does not show checkbox if public upload is globally disabled', function() {
$('#filestable').data('allow-public-upload', 'no'); // ugh
showDropDown('folder');
$('#dropdown [name=linkCheckbox]').click();
expect($('#dropdown [name=allowPublicUpload]').length).toEqual(0);
});
it('saves share after allowing upload', function() {
var req, params;
showDropDown('folder');
$('#dropdown [name=linkCheckbox]').click();
expect(fakeServer.requests.length).toEqual(1);
$('#dropdown [name=allowPublicUpload]').click();
expect(fakeServer.requests.length).toEqual(2);
req = fakeServer.requests[1];
expect(req.url).toEqual(OC.webroot + '/index.php/core/ajax/share.php');
params = OC.parseQueryString(req.requestBody);
expect(params.action).toEqual('share');
expect(params.itemType).toEqual('folder');
expect(params.itemSource).toEqual('123');
expect(params.shareType).toEqual('' + OC.Share.SHARE_TYPE_LINK);
expect(params.shareWith).toEqual('');
expect(params.permissions).toEqual('7');
expect(params.itemSourceName).toEqual('name-folder');
expect(params.expirationDate).toEqual('');
});
});
});
describe('"sharesChanged" event', function() {
Expand Down
1 change: 1 addition & 0 deletions lib/private/share/share.php
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,7 @@ private static function put($itemType, $itemSource, $shareType, $shareWith, $uid

$preHookData['itemTarget'] = ($isGroupShare) ? $groupItemTarget : $itemTarget;
$preHookData['shareWith'] = ($isGroupShare) ? $shareWith['group'] : $shareWith;
$isLinkShare = $shareType === self::SHARE_TYPE_LINK;

\OC_Hook::emit('OCP\Share', 'pre_shared', $preHookData);

Expand Down

0 comments on commit c1efef5

Please sign in to comment.