Skip to content

Commit

Permalink
set default expiration date if expiration date is enforced and it is …
Browse files Browse the repository at this point in the history
…new share
  • Loading branch information
karakayasemi committed Jun 18, 2019
1 parent 67f7273 commit eb0446f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
21 changes: 21 additions & 0 deletions lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -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);
}
}
12 changes: 8 additions & 4 deletions tests/lib/Share20/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit eb0446f

Please sign in to comment.