Merge pull request #8927 from owncloud/expire_only_link_shares

only expire link shares
This commit is contained in:
Björn Schießle 2014-06-06 19:24:15 +02:00
commit 3da47db33c
6 changed files with 138 additions and 113 deletions

View File

@ -84,6 +84,8 @@ if (isset($_POST['action']) && isset($_POST['itemType']) && isset($_POST['itemSo
$date = new \DateTime($_POST['date']); $date = new \DateTime($_POST['date']);
$today = new \DateTime('now'); $today = new \DateTime('now');
if ($date < $today) { if ($date < $today) {
OC_JSON::error(array('data' => array('message' => $l->t('Expiration date is in the past.')))); OC_JSON::error(array('data' => array('message' => $l->t('Expiration date is in the past.'))));
return; return;

View File

@ -25,11 +25,13 @@ foreach(OC_App::getEnabledApps() as $app) {
$apps_paths[$app] = OC_App::getAppWebPath($app); $apps_paths[$app] = OC_App::getAppWebPath($app);
} }
$defaultExpireDateEnabled = \OCP\Config::getAppValue('core', 'shareapi_default_expire_date', 'no'); $value = \OCP\Config::getAppValue('core', 'shareapi_default_expire_date', 'no');
$defaultExpireDateEnabled = ($value === 'yes') ? true :false;
$defaultExpireDate = $enforceDefaultExpireDate = null; $defaultExpireDate = $enforceDefaultExpireDate = null;
if ($defaultExpireDateEnabled === 'yes') { if ($defaultExpireDateEnabled) {
$defaultExpireDate = \OCP\Config::getAppValue('core', 'shareapi_expire_after_n_days', '7'); $defaultExpireDate = (int)\OCP\Config::getAppValue('core', 'shareapi_expire_after_n_days', '7');
$enforceDefaultExpireDate = \OCP\Config::getAppValue('core', 'shareapi_enforce_expire_date', 'no'); $value = \OCP\Config::getAppValue('core', 'shareapi_enforce_expire_date', 'no');
$enforceDefaultExpireDate = ($value === 'yes') ? true : false;
} }
$array = array( $array = array(

View File

@ -335,8 +335,8 @@ OC.Share={
html += '<br />'; html += '<br />';
var defaultExpireMessage = ''; var defaultExpireMessage = '';
if ((itemType === 'folder' || itemType === 'file') && oc_appconfig.core.defaultExpireDateEnabled === 'yes') { if ((itemType === 'folder' || itemType === 'file') && oc_appconfig.core.defaultExpireDateEnabled) {
if (oc_appconfig.core.defaultExpireDateEnforced === 'yes') { if (oc_appconfig.core.defaultExpireDateEnforced) {
defaultExpireMessage = t('core', 'The public link will expire no later than {days} days after it is created', {'days': oc_appconfig.core.defaultExpireDate}) + '<br/>'; defaultExpireMessage = t('core', 'The public link will expire no later than {days} days after it is created', {'days': oc_appconfig.core.defaultExpireDate}) + '<br/>';
} else { } else {
defaultExpireMessage = t('core', 'By default the public link will expire after {days} days', {'days': oc_appconfig.core.defaultExpireDate}) + '<br/>'; defaultExpireMessage = t('core', 'By default the public link will expire after {days} days', {'days': oc_appconfig.core.defaultExpireDate}) + '<br/>';
@ -597,7 +597,6 @@ OC.Share={
else{ else{
html.find('.cruds').before(showCrudsButton); html.find('.cruds').before(showCrudsButton);
} }
$('#expiration').show();
if (!OC.Share.currentShares[shareType]) { if (!OC.Share.currentShares[shareType]) {
OC.Share.currentShares[shareType] = []; OC.Share.currentShares[shareType] = [];
} }
@ -647,7 +646,6 @@ OC.Share={
$('#linkPassText').attr('placeholder', '**********'); $('#linkPassText').attr('placeholder', '**********');
} }
$('#expiration').show(); $('#expiration').show();
$('#defaultExpireMessage').show();
$('#emailPrivateLink #email').show(); $('#emailPrivateLink #email').show();
$('#emailPrivateLink #emailButton').show(); $('#emailPrivateLink #emailButton').show();
$('#allowPublicUploadWrapper').show(); $('#allowPublicUploadWrapper').show();
@ -673,6 +671,15 @@ OC.Share={
$('#expirationDate').datepicker({ $('#expirationDate').datepicker({
dateFormat : 'dd-mm-yy' dateFormat : 'dd-mm-yy'
}); });
if (oc_appconfig.core.defaultExpireDateEnforced) {
$('#expirationCheckbox').attr('disabled', true);
$.datepicker.setDefaults({
maxDate : new Date(date.replace(' 00:00:00', ''))
});
}
if(oc_appconfig.core.defaultExpireDateEnabled) {
$('#defaultExpireMessage').show('blind');
}
} }
}; };
@ -685,7 +692,8 @@ $(document).ready(function() {
dayNames: dayNames, dayNames: dayNames,
dayNamesMin: $.map(dayNames, function(v) { return v.slice(0,2); }), dayNamesMin: $.map(dayNames, function(v) { return v.slice(0,2); }),
dayNamesShort: $.map(dayNames, function(v) { return v.slice(0,3)+'.'; }), dayNamesShort: $.map(dayNames, function(v) { return v.slice(0,3)+'.'; }),
firstDay: firstDay firstDay: firstDay,
minDate : new Date()
}); });
} }
$(document).on('click', 'a.share', function(event) { $(document).on('click', 'a.share', function(event) {
@ -789,14 +797,21 @@ $(document).ready(function() {
var itemType = $('#dropdown').data('item-type'); var itemType = $('#dropdown').data('item-type');
var itemSource = $('#dropdown').data('item-source'); var itemSource = $('#dropdown').data('item-source');
var itemSourceName = $('#dropdown').data('item-source-name'); var itemSourceName = $('#dropdown').data('item-source-name');
var expirationDate = '';
if ($('#expirationCheckbox').is(':checked') === true) {
expirationDate = $( "#expirationDate" ).val();
}
if (this.checked) { if (this.checked) {
var expireDateString = '';
if (oc_appconfig.core.defaultExpireDateEnabled) {
var date = new Date().getTime();
var expireAfterMs = oc_appconfig.core.defaultExpireDate * 24 * 60 * 60 * 1000;
var expireDate = new Date(date + expireAfterMs);
var month = expireDate.getMonth() + 1;
var year = expireDate.getFullYear();
var day = expireDate.getDate();
expireDateString = year + "-" + month + '-' + day + ' 00:00:00';
}
// Create a link // Create a link
if (oc_appconfig.core.enforcePasswordForPublicLink === false) { if (oc_appconfig.core.enforcePasswordForPublicLink === false) {
OC.Share.share(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', OC.PERMISSION_READ, itemSourceName, expirationDate, function(data) { OC.Share.share(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', OC.PERMISSION_READ, itemSourceName, expireDateString, function(data) {
OC.Share.showLink(data.token, null, itemSource); OC.Share.showLink(data.token, null, itemSource);
$('#dropdown').trigger(new $.Event('sharesChanged', {shares: OC.Share.currentShares})); $('#dropdown').trigger(new $.Event('sharesChanged', {shares: OC.Share.currentShares}));
OC.Share.updateIcon(itemType, itemSource); OC.Share.updateIcon(itemType, itemSource);
@ -805,9 +820,13 @@ $(document).ready(function() {
$('#linkPass').toggle('blind'); $('#linkPass').toggle('blind');
$('#linkPassText').focus(); $('#linkPassText').focus();
} }
if (expireDateString !== '') {
OC.Share.showExpirationDate(expireDateString);
}
} else { } else {
// Delete private link // Delete private link
OC.Share.hideLink(); OC.Share.hideLink();
$('#expiration').hide('blind');
if ($('#linkText').val() !== '') { if ($('#linkText').val() !== '') {
OC.Share.unshare(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', function() { OC.Share.unshare(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', function() {
OC.Share.itemShares[OC.Share.SHARE_TYPE_LINK] = false; OC.Share.itemShares[OC.Share.SHARE_TYPE_LINK] = false;
@ -917,8 +936,8 @@ $(document).ready(function() {
OC.dialogs.alert(t('core', 'Error unsetting expiration date'), t('core', 'Error')); OC.dialogs.alert(t('core', 'Error unsetting expiration date'), t('core', 'Error'));
} }
$('#expirationDate').hide('blind'); $('#expirationDate').hide('blind');
if (oc_appconfig.core.defaultExpireDateEnforced === 'no') { if (oc_appconfig.core.defaultExpireDateEnforced === false) {
$('#defaultExpireMessage'). show('blind'); $('#defaultExpireMessage').show('blind');
} }
}); });
} }

View File

@ -200,6 +200,18 @@ class Helper extends \OC\Share\Constants {
return $defaultExpireSettings; return $defaultExpireSettings;
} }
public static function calcExpireDate() {
$expireAfter = \OC\Share\Share::getExpireInterval() * 24 * 60 * 60;
$expireAt = time() + $expireAfter;
$date = new \DateTime();
$date->setTimestamp($expireAt);
$date->setTime(0, 0, 0);
//$dateString = $date->format('Y-m-d') . ' 00:00:00';
return $date;
}
/** /**
* calculate expire date * calculate expire date
* @param array $defaultExpireSettings contains 'defaultExpireDateSet', 'enforceExpireDate', 'expireAfterDays' * @param array $defaultExpireSettings contains 'defaultExpireDateSet', 'enforceExpireDate', 'expireAfterDays'

View File

@ -606,6 +606,7 @@ class Share extends \OC\Share\Constants {
$oldPermissions = $checkExists['permissions']; $oldPermissions = $checkExists['permissions'];
//delete the old share //delete the old share
Helper::delete($checkExists['id']); Helper::delete($checkExists['id']);
$updateExistingShare = true;
} }
// Generate hash of password - same method as user passwords // Generate hash of password - same method as user passwords
@ -628,6 +629,12 @@ class Share extends \OC\Share\Constants {
throw new \Exception($message_t); throw new \Exception($message_t);
} }
if (!empty($updateExistingShare) &&
self::isDefaultExpireDateEnabled() &&
empty($expirationDate)) {
$expirationDate = Helper::calcExpireDate();
}
// Generate token // Generate token
if (isset($oldToken)) { if (isset($oldToken)) {
$token = $oldToken; $token = $oldToken;
@ -886,28 +893,33 @@ class Share extends \OC\Share\Constants {
*/ */
public static function setExpirationDate($itemType, $itemSource, $date) { public static function setExpirationDate($itemType, $itemSource, $date) {
$user = \OC_User::getUser(); $user = \OC_User::getUser();
$items = self::getItems($itemType, $itemSource, null, null, $user, self::FORMAT_NONE, null, -1, false);
if (!empty($items)) {
if ($date == '') { if ($date == '') {
$date = null; $date = null;
} else { } else {
$date = new \DateTime($date); $date = new \DateTime($date);
} }
$query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `expiration` = ? WHERE `id` = ?'); $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `expiration` = ? WHERE `item_type` = ? AND `item_source` = ? AND `uid_owner` = ? AND `share_type` = ?');
$query->bindValue(1, $date, 'datetime'); $query->bindValue(1, $date, 'datetime');
foreach ($items as $item) { $query->bindValue(2, $itemType);
$query->bindValue(2, (int) $item['id']); $query->bindValue(3, $itemSource);
$query->execute(); $query->bindValue(4, $user);
$query->bindValue(5, \OCP\Share::SHARE_TYPE_LINK);
$result = $query->execute();
if ($result === 1) {
\OC_Hook::emit('OCP\Share', 'post_set_expiration_date', array( \OC_Hook::emit('OCP\Share', 'post_set_expiration_date', array(
'itemType' => $itemType, 'itemType' => $itemType,
'itemSource' => $itemSource, 'itemSource' => $itemSource,
'date' => $date, 'date' => $date,
'uidOwner' => $user 'uidOwner' => $user
)); ));
} else {
\OCP\Util::writeLog('sharing', "Couldn't set expire date'", \OCP\Util::ERROR);
} }
return true;
} return ($result === 1) ? true : false;
return false;
} }
/** /**
@ -917,6 +929,11 @@ class Share extends \OC\Share\Constants {
*/ */
protected static function expireItem(array $item) { protected static function expireItem(array $item) {
$result = false;
// only use default expire date for link shares
if ((int) $item['share_type'] === self::SHARE_TYPE_LINK) {
// calculate expire date // calculate expire date
if (!empty($item['expiration'])) { if (!empty($item['expiration'])) {
$userDefinedExpire = new \DateTime($item['expiration']); $userDefinedExpire = new \DateTime($item['expiration']);
@ -925,21 +942,21 @@ class Share extends \OC\Share\Constants {
$expires = null; $expires = null;
} }
// only use default expire date for link shares
if((int)$item['share_type'] === self::SHARE_TYPE_LINK) {
// get default expire settings // get default expire settings
$defaultSettings = Helper::getDefaultExpireSetting(); $defaultSettings = Helper::getDefaultExpireSetting();
$expires = Helper::calculateExpireDate($defaultSettings, $item['stime'], $expires); $expires = Helper::calculateExpireDate($defaultSettings, $item['stime'], $expires);
}
if (is_int($expires)) { if (is_int($expires)) {
$now = time(); $now = time();
if ($now > $expires) { if ($now > $expires) {
self::unshareItem($item); self::unshareItem($item);
return true; $result = true;
} }
} }
return false; }
return $result;
} }
/** /**
@ -1865,4 +1882,18 @@ class Share extends \OC\Share\Constants {
return ($value === 'yes') ? true : false; return ($value === 'yes') ? true : false;
} }
public static function isDefaultExpireDateEnabled() {
$defaultExpireDateEnabled = \OCP\Config::getAppValue('core', 'shareapi_default_expire_date', 'no');
return ($defaultExpireDateEnabled === "yes") ? true : false;
}
public static function enforceDefaultExpireDate() {
$enforceDefaultExpireDate = \OCP\Config::getAppValue('core', 'shareapi_enforce_expire_date', 'no');
return ($enforceDefaultExpireDate === "yes") ? true : false;
}
public static function getExpireInterval() {
return (int)\OCP\Config::getAppValue('core', 'shareapi_expire_after_n_days', '7');
}
} }

View File

@ -151,6 +151,12 @@ class Test_Share extends PHPUnit_Framework_TestCase {
); );
} }
protected function shareUserTestFileAsLink() {
OC_User::setUserId($this->user1);
$result = OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_LINK, null, OCP\PERMISSION_READ);
$this->assertTrue(is_string($result));
}
/** /**
* @param string $sharer * @param string $sharer
* @param string $receiver * @param string $receiver
@ -316,36 +322,35 @@ class Test_Share extends PHPUnit_Framework_TestCase {
} }
public function testShareWithUserExpirationExpired() { public function testShareWithUserExpirationExpired() {
$this->shareUserOneTestFileWithUserTwo();
OC_User::setUserId($this->user1); OC_User::setUserId($this->user1);
$this->shareUserOneTestFileWithUserTwo();
$this->shareUserTestFileAsLink();
$this->assertTrue( $this->assertTrue(
OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInPast), OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInPast),
'Failed asserting that user 1 successfully set an expiration date for the test.txt share.' 'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
); );
OC_User::setUserId($this->user2); $shares = OCP\Share::getItemsShared('test');
$this->assertSame(array(), $this->assertSame(1, count($shares));
OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE), $share = reset($shares);
'Failed asserting that user 2 no longer has access to test.txt after expiration.' $this->assertSame(\OCP\Share::SHARE_TYPE_USER, $share['share_type']);
);
} }
public function testShareWithUserExpirationValid() { public function testShareWithUserExpirationValid() {
$this->shareUserOneTestFileWithUserTwo();
OC_User::setUserId($this->user1); OC_User::setUserId($this->user1);
$this->shareUserOneTestFileWithUserTwo();
$this->shareUserTestFileAsLink();
$this->assertTrue( $this->assertTrue(
OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInFuture), OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInFuture),
'Failed asserting that user 1 successfully set an expiration date for the test.txt share.' 'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
); );
OC_User::setUserId($this->user2); $shares = OCP\Share::getItemsShared('test');
$this->assertEquals( $this->assertSame(2, count($shares));
array('test.txt'),
OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that user 2 still has access to test.txt after expiration date has been set.'
);
} }
protected function shareUserOneTestFileWithGroupOne() { protected function shareUserOneTestFileWithGroupOne() {
@ -516,52 +521,6 @@ class Test_Share extends PHPUnit_Framework_TestCase {
$this->assertEquals(array(), OCP\Share::getItemsShared('test')); $this->assertEquals(array(), OCP\Share::getItemsShared('test'));
} }
public function testShareWithGroupExpirationExpired() {
$this->shareUserOneTestFileWithGroupOne();
OC_User::setUserId($this->user1);
$this->assertTrue(
OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInPast),
'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
);
OC_User::setUserId($this->user2);
$this->assertSame(array(),
OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that user 2 no longer has access to test.txt after expiration.'
);
OC_User::setUserId($this->user3);
$this->assertSame(array(),
OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that user 3 no longer has access to test.txt after expiration.'
);
}
public function testShareWithGroupExpirationValid() {
$this->shareUserOneTestFileWithGroupOne();
OC_User::setUserId($this->user1);
$this->assertTrue(
OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInFuture),
'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
);
OC_User::setUserId($this->user2);
$this->assertEquals(
array('test.txt'),
OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that user 2 still has access to test.txt after expiration date has been set.'
);
OC_User::setUserId($this->user3);
$this->assertEquals(
array('test.txt'),
OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that user 3 still has access to test.txt after expiration date has been set.'
);
}
/** /**
* @param boolean|string $token * @param boolean|string $token
*/ */