Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.2.1] set default expiration date if expiration date is enforced and it is new share #35593

Merged
merged 1 commit into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,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 @@ -1548,4 +1555,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 @@ -62,7 +62,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 @@ -934,19 +934,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