only expire link shares

This commit is contained in:
Bjoern Schiessle 2014-06-03 15:15:04 +02:00
parent 933c05566e
commit f15d7dfec0
5 changed files with 131 additions and 112 deletions

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,12 @@ OC.Share={
$('#expirationDate').datepicker({ $('#expirationDate').datepicker({
dateFormat : 'dd-mm-yy' dateFormat : 'dd-mm-yy'
}); });
if (oc_appconfig.core.defaultExpireDateEnforced) {
$('#expirationCheckbox').attr('disabled', true);
}
if(oc_appconfig.core.defaultExpireDateEnabled) {
$('#defaultExpireMessage').show('blind');
}
} }
}; };
@ -789,14 +793,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 +816,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 +932,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->bindValue(1, $date, 'datetime');
foreach ($items as $item) {
$query->bindValue(2, (int) $item['id']);
$query->execute();
\OC_Hook::emit('OCP\Share', 'post_set_expiration_date', array(
'itemType' => $itemType,
'itemSource' => $itemSource,
'date' => $date,
'uidOwner' => $user
));
}
return true;
} }
return false; $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(2, $itemType);
$query->bindValue(3, $itemSource);
$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(
'itemType' => $itemType,
'itemSource' => $itemSource,
'date' => $date,
'uidOwner' => $user
));
} else {
\OCP\Util::writeLog('sharing', "Couldn't set expire date'", \OCP\Util::ERROR);
}
return ($result === 1) ? true : false;
} }
/** /**
@ -917,29 +929,34 @@ class Share extends \OC\Share\Constants {
*/ */
protected static function expireItem(array $item) { protected static function expireItem(array $item) {
// calculate expire date $result = false;
if (!empty($item['expiration'])) {
$userDefinedExpire = new \DateTime($item['expiration']);
$expires = $userDefinedExpire->getTimestamp();
} else {
$expires = null;
}
// only use default expire date for link shares // only use default expire date for link shares
if((int)$item['share_type'] === self::SHARE_TYPE_LINK) { if ((int) $item['share_type'] === self::SHARE_TYPE_LINK) {
// calculate expire date
if (!empty($item['expiration'])) {
$userDefinedExpire = new \DateTime($item['expiration']);
$expires = $userDefinedExpire->getTimestamp();
} else {
$expires = null;
}
// 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)) {
$now = time(); if (is_int($expires)) {
if ($now > $expires) { $now = time();
self::unshareItem($item); if ($now > $expires) {
return true; self::unshareItem($item);
$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
*/ */