]> source.dussan.org Git - nextcloud-server.git/commitdiff
only expire link shares
authorBjoern Schiessle <schiessle@owncloud.com>
Tue, 3 Jun 2014 13:15:04 +0000 (15:15 +0200)
committerBjoern Schiessle <schiessle@owncloud.com>
Fri, 6 Jun 2014 13:47:37 +0000 (15:47 +0200)
core/js/config.php
core/js/share.js
lib/private/share/helper.php
lib/private/share/share.php
tests/lib/share/share.php

index 80b1b6d242d6f776d335fcf9c911234b8ae55597..2f423111bda473c5c69067a6c300a3d10ce16731 100644 (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(
index 7e3b0d8c65dc4f2db73c9e113d1a14ce53e74b50..2add28411398af49cd3659068dc9cc2ea8851762 100644 (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,12 @@ OC.Share={
                $('#expirationDate').datepicker({
                        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 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 +816,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 +932,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');
                                }
                        });
                }
index c92aa15b4bfe88baf85b0e0569c60ecd80968d75..71c6d8517a9c5ec1467886ff7c46d16f7092e8e8 100644 (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'
index f02eacfed744f12ac3370225e887d68b84b81e0c..883fbd1b298fe80073838588a167dbb8dc34c3b7 100644 (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');
+       }
+
 }
index 95983ee70e62f5fcc75084f63ced0911ad7a8624..5920b44a8e032aced03c3d23c5ecd38a1264d107 100644 (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() {
+               OC_User::setUserId($this->user1);
                $this->shareUserOneTestFileWithUserTwo();
+               $this->shareUserTestFileAsLink();
 
-               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.'
-               );
+               $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() {
+               OC_User::setUserId($this->user1);
                $this->shareUserOneTestFileWithUserTwo();
+               $this->shareUserTestFileAsLink();
+
 
-               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.'
-               );
+               $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
         */