Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly send expiration date with all share.php calls #11674

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
119 changes: 66 additions & 53 deletions core/js/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,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 @@ -956,7 +983,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 @@ -1029,35 +1056,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 @@ -1067,22 +1087,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 @@ -1094,34 +1111,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
115 changes: 104 additions & 11 deletions core/js/tests/specs/shareSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe('OC.Share tests', function() {
oldEnableAvatars = oc_config.enable_avatars;
oc_config.enable_avatars = false;
avatarStub = sinon.stub($.fn, 'avatar');
$('#testArea').append('<table id="filestable" data-allow-public-upload="yes"></table>');
});
afterEach(function() {
/* jshint camelcase:false */
Expand Down Expand Up @@ -100,6 +101,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 @@ -271,23 +286,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 @@ -411,6 +432,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('check for avatar', 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 @@ -1859,6 +1859,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