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']);
$today = new \DateTime('now');
if ($date < $today) {
OC_JSON::error(array('data' => array('message' => $l->t('Expiration date is in the past.'))));
return;

View File

@ -25,11 +25,13 @@ foreach(OC_App::getEnabledApps() as $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;
if ($defaultExpireDateEnabled === 'yes') {
$defaultExpireDate = \OCP\Config::getAppValue('core', 'shareapi_expire_after_n_days', '7');
$enforceDefaultExpireDate = \OCP\Config::getAppValue('core', 'shareapi_enforce_expire_date', 'no');
if ($defaultExpireDateEnabled) {
$defaultExpireDate = (int)\OCP\Config::getAppValue('core', 'shareapi_expire_after_n_days', '7');
$value = \OCP\Config::getAppValue('core', 'shareapi_enforce_expire_date', 'no');
$enforceDefaultExpireDate = ($value === 'yes') ? true : false;
}
$array = array(

View File

@ -335,8 +335,8 @@ OC.Share={
html += '<br />';
var defaultExpireMessage = '';
if ((itemType === 'folder' || itemType === 'file') && oc_appconfig.core.defaultExpireDateEnabled === 'yes') {
if (oc_appconfig.core.defaultExpireDateEnforced === 'yes') {
if ((itemType === 'folder' || itemType === 'file') && oc_appconfig.core.defaultExpireDateEnabled) {
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/>';
} else {
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{
html.find('.cruds').before(showCrudsButton);
}
$('#expiration').show();
if (!OC.Share.currentShares[shareType]) {
OC.Share.currentShares[shareType] = [];
}
@ -647,7 +646,6 @@ OC.Share={
$('#linkPassText').attr('placeholder', '**********');
}
$('#expiration').show();
$('#defaultExpireMessage').show();
$('#emailPrivateLink #email').show();
$('#emailPrivateLink #emailButton').show();
$('#allowPublicUploadWrapper').show();
@ -673,6 +671,15 @@ OC.Share={
$('#expirationDate').datepicker({
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,
dayNamesMin: $.map(dayNames, function(v) { return v.slice(0,2); }),
dayNamesShort: $.map(dayNames, function(v) { return v.slice(0,3)+'.'; }),
firstDay: firstDay
firstDay: firstDay,
minDate : new Date()
});
}
$(document).on('click', 'a.share', function(event) {
@ -789,14 +797,21 @@ $(document).ready(function() {
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();
}
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
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);
$('#dropdown').trigger(new $.Event('sharesChanged', {shares: OC.Share.currentShares}));
OC.Share.updateIcon(itemType, itemSource);
@ -805,9 +820,13 @@ $(document).ready(function() {
$('#linkPass').toggle('blind');
$('#linkPassText').focus();
}
if (expireDateString !== '') {
OC.Share.showExpirationDate(expireDateString);
}
} else {
// Delete private link
OC.Share.hideLink();
$('#expiration').hide('blind');
if ($('#linkText').val() !== '') {
OC.Share.unshare(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', function() {
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'));
}
$('#expirationDate').hide('blind');
if (oc_appconfig.core.defaultExpireDateEnforced === 'no') {
$('#defaultExpireMessage'). show('blind');
if (oc_appconfig.core.defaultExpireDateEnforced === false) {
$('#defaultExpireMessage').show('blind');
}
});
}

View File

@ -200,6 +200,18 @@ class Helper extends \OC\Share\Constants {
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
* @param array $defaultExpireSettings contains 'defaultExpireDateSet', 'enforceExpireDate', 'expireAfterDays'

View File

@ -606,6 +606,7 @@ class Share extends \OC\Share\Constants {
$oldPermissions = $checkExists['permissions'];
//delete the old share
Helper::delete($checkExists['id']);
$updateExistingShare = true;
}
// Generate hash of password - same method as user passwords
@ -628,6 +629,12 @@ class Share extends \OC\Share\Constants {
throw new \Exception($message_t);
}
if (!empty($updateExistingShare) &&
self::isDefaultExpireDateEnabled() &&
empty($expirationDate)) {
$expirationDate = Helper::calcExpireDate();
}
// Generate token
if (isset($oldToken)) {
$token = $oldToken;
@ -886,28 +893,33 @@ class Share extends \OC\Share\Constants {
*/
public static function setExpirationDate($itemType, $itemSource, $date) {
$user = \OC_User::getUser();
$items = self::getItems($itemType, $itemSource, null, null, $user, self::FORMAT_NONE, null, -1, false);
if (!empty($items)) {
if ($date == '') {
$date = null;
} else {
$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;
if ($date == '') {
$date = null;
} else {
$date = new \DateTime($date);
}
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) {
// calculate expire date
if (!empty($item['expiration'])) {
$userDefinedExpire = new \DateTime($item['expiration']);
$expires = $userDefinedExpire->getTimestamp();
} else {
$expires = null;
}
$result = false;
// 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
$defaultSettings = Helper::getDefaultExpireSetting();
$expires = Helper::calculateExpireDate($defaultSettings, $item['stime'], $expires);
}
if (is_int($expires)) {
$now = time();
if ($now > $expires) {
self::unshareItem($item);
return true;
if (is_int($expires)) {
$now = time();
if ($now > $expires) {
self::unshareItem($item);
$result = true;
}
}
}
return false;
return $result;
}
/**
@ -1865,4 +1882,18 @@ class Share extends \OC\Share\Constants {
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 $receiver
@ -316,36 +322,35 @@ class Test_Share extends PHPUnit_Framework_TestCase {
}
public function testShareWithUserExpirationExpired() {
$this->shareUserOneTestFileWithUserTwo();
OC_User::setUserId($this->user1);
$this->shareUserOneTestFileWithUserTwo();
$this->shareUserTestFileAsLink();
$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.'
);
$shares = OCP\Share::getItemsShared('test');
$this->assertSame(1, count($shares));
$share = reset($shares);
$this->assertSame(\OCP\Share::SHARE_TYPE_USER, $share['share_type']);
}
public function testShareWithUserExpirationValid() {
$this->shareUserOneTestFileWithUserTwo();
OC_User::setUserId($this->user1);
$this->shareUserOneTestFileWithUserTwo();
$this->shareUserTestFileAsLink();
$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.'
);
$shares = OCP\Share::getItemsShared('test');
$this->assertSame(2, count($shares));
}
protected function shareUserOneTestFileWithGroupOne() {
@ -516,52 +521,6 @@ class Test_Share extends PHPUnit_Framework_TestCase {
$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
*/