From eb0446fdc58c40a477a812456ec7e1de9ddfcd21 Mon Sep 17 00:00:00 2001 From: karakayasemi Date: Mon, 17 Jun 2019 21:27:07 +0300 Subject: [PATCH] set default expiration date if expiration date is enforced and it is new share --- lib/private/Share20/Manager.php | 21 +++++++++++++++++++++ tests/lib/Share20/ManagerTest.php | 12 ++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php index b694023c3948..42c58fd427c9 100644 --- a/lib/private/Share20/Manager.php +++ b/lib/private/Share20/Manager.php @@ -331,6 +331,13 @@ protected function validateExpirationDate(\OCP\Share\IShare $share) { // If we enforce the expiration date check that is does not exceed if ($this->shareApiLinkDefaultExpireDateEnforced()) { + // If expiredate is empty and it is a new share, set a default one if there is a default + if ($this->isNewShare($share) && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) { + $expirationDate = new \DateTime(); + $expirationDate->setTime(0, 0, 0); + $expirationDate->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D')); + } + if ($expirationDate === null) { throw new \InvalidArgumentException('Expiration date is enforced'); } @@ -1549,4 +1556,18 @@ private function hashAttributes($perms) { return \md5(\json_encode($perms->toArray())); } + + /** + * @param IShare $share + * @return boolean + */ + private function isNewShare(IShare $share) { + $fullId = null; + try { + $fullId = $share->getFullId(); + } catch (\UnexpectedValueException $e) { + // This is a new share + } + return ($fullId === null); + } } diff --git a/tests/lib/Share20/ManagerTest.php b/tests/lib/Share20/ManagerTest.php index 2600bde59c9a..2e04f54ecd26 100644 --- a/tests/lib/Share20/ManagerTest.php +++ b/tests/lib/Share20/ManagerTest.php @@ -67,7 +67,7 @@ class ManagerTest extends \Test\TestCase { protected $manager; /** @var ILogger | \PHPUnit\Framework\MockObject\MockObject */ protected $logger; - /** @var IConfig */ + /** @var IConfig | \PHPUnit\Framework\MockObject\MockObject */ protected $config; /** @var ISecureRandom */ protected $secureRandom; @@ -939,19 +939,23 @@ public function testvalidateExpirationDateEnforceButNotEnabledAndNotSet() { $this->assertNull($share->getExpirationDate()); } - /** - * @expectedException \InvalidArgumentException - */ public function testvalidateExpirationDateEnforceButNotSetNewShare() { $share = $this->manager->newShare(); $this->config->method('getAppValue') ->will($this->returnValueMap([ + ['core', 'shareapi_enforce_expire_date', 'no', 'yes'], + ['core', 'shareapi_expire_after_n_days', '7', '3'], ['core', 'shareapi_default_expire_date', 'no', 'yes'], ['core', 'shareapi_enforce_expire_date', 'no', 'yes'], ])); + $expected = new \DateTime(); + $expected->setTime(0, 0, 0); + $expected->add(new \DateInterval('P3D')); $this->invokePrivate($this->manager, 'validateExpirationDate', [$share]); + $this->assertNotNull($share->getExpirationDate()); + $this->assertEquals($expected, $share->getExpirationDate()); } public function testvalidateExpirationDateEnforceToFarIntoFuture() {