Skip to content

Commit

Permalink
fix(shareManager): Respect empty expireDate in server
Browse files Browse the repository at this point in the history
If `expireDate` is an empty string and not `null` then the server should not set a default.

Signed-off-by: fenn-cs <fenn25.fn@gmail.com>
  • Loading branch information
Fenn-CS committed May 1, 2024
1 parent d82fb01 commit 8ef8bf4
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 80 deletions.
19 changes: 12 additions & 7 deletions apps/files_sharing/lib/Controller/ShareAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ public function createShare(
string $publicUpload = 'false',
string $password = '',
?string $sendPasswordByTalk = null,
string $expireDate = '',
?string $expireDate = null,
string $note = '',
string $label = '',
?string $attributes = null
Expand Down Expand Up @@ -646,12 +646,17 @@ public function createShare(
}

//Expire date
if ($expireDate !== '') {
try {
$expireDateTime = $this->parseDate($expireDate);
$share->setExpirationDate($expireDateTime);
} catch (\Exception $e) {
throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD'));
if ($expireDate !== null) {
if ($expireDate !== '') {
try {
$expireDateTime = $this->parseDate($expireDate);
$share->setExpirationDate($expireDateTime);
} catch (\Exception $e) {
throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD'));
}
} else {
// Client sent empty string for expire date. Do not overwrite.
$share->setOverwriteFalsyExpirationDate(false);
}
}

Expand Down
3 changes: 1 addition & 2 deletions apps/files_sharing/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1783,8 +1783,7 @@
"in": "query",
"description": "Expiry date of the share using user timezone at 00:00. It means date in UTC timezone will be used.",
"schema": {
"type": "string",
"default": ""
"type": "string"
}
},
{
Expand Down
40 changes: 27 additions & 13 deletions lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,25 @@ protected function validateExpirationDateInternal(IShare $share) {

$expirationDate = $share->getExpirationDate();

if ($expirationDate !== null) {
if ($isRemote) {
$defaultExpireDate = $this->shareApiRemoteDefaultExpireDate();
$defaultExpireDays = $this->shareApiRemoteDefaultExpireDays();
$configProp = 'remote_defaultExpDays';
$isEnforced = $this->shareApiRemoteDefaultExpireDateEnforced();
} else {
$defaultExpireDate = $this->shareApiInternalDefaultExpireDate();
$defaultExpireDays = $this->shareApiInternalDefaultExpireDays();
$configProp = 'internal_defaultExpDays';
$isEnforced = $this->shareApiInternalDefaultExpireDateEnforced();
}

// If $expirationDate is falsy, overwrite flag is false, no expiration is set
if(empty($expirationDate) && !$share->getOverwriteFalsyExpirationDate() && !$isEnforced) {
$share->setExpirationDate(null);
return $share;
}

if ($expirationDate != null) {
$expirationDate->setTimezone($this->dateTimeZone->getTimeZone());
$expirationDate->setTime(0, 0, 0);

Expand All @@ -409,17 +427,6 @@ protected function validateExpirationDateInternal(IShare $share) {
// This is a new share
}

if ($isRemote) {
$defaultExpireDate = $this->shareApiRemoteDefaultExpireDate();
$defaultExpireDays = $this->shareApiRemoteDefaultExpireDays();
$configProp = 'remote_defaultExpDays';
$isEnforced = $this->shareApiRemoteDefaultExpireDateEnforced();
} else {
$defaultExpireDate = $this->shareApiInternalDefaultExpireDate();
$defaultExpireDays = $this->shareApiInternalDefaultExpireDays();
$configProp = 'internal_defaultExpDays';
$isEnforced = $this->shareApiInternalDefaultExpireDateEnforced();
}
if ($fullId === null && $expirationDate === null && $defaultExpireDate) {
$expirationDate = new \DateTime('now', $this->dateTimeZone->getTimeZone());
$expirationDate->setTime(0, 0, 0);
Expand Down Expand Up @@ -474,6 +481,13 @@ protected function validateExpirationDateInternal(IShare $share) {
*/
protected function validateExpirationDateLink(IShare $share) {
$expirationDate = $share->getExpirationDate();
$isEnforced = $this->shareApiLinkDefaultExpireDateEnforced();

// If $expirationDate is falsy, overwrite flag is false, no expiration is set
if(empty($expirationDate) && !$share->getOverwriteFalsyExpirationDate() && !$isEnforced) {
$share->setExpirationDate(null);
return $share;
}

if ($expirationDate !== null) {
$expirationDate->setTimezone($this->dateTimeZone->getTimeZone());
Expand Down Expand Up @@ -507,7 +521,7 @@ protected function validateExpirationDateLink(IShare $share) {
}

// If we enforce the expiration date check that is does not exceed
if ($this->shareApiLinkDefaultExpireDateEnforced()) {
if ($isEnforced) {
if ($expirationDate === null) {
throw new \InvalidArgumentException('Expiration date is enforced');
}
Expand Down
98 changes: 43 additions & 55 deletions lib/private/Share20/Share.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,61 +41,34 @@
use OCP\Share\IShare;

class Share implements IShare {
/** @var string */
private $id;
/** @var string */
private $providerId;
/** @var Node */
private $node;
/** @var int */
private $fileId;
/** @var string */
private $nodeType;
/** @var int */
private $shareType;
/** @var string */
private $sharedWith;
/** @var string */
private $sharedWithDisplayName;
/** @var string */
private $sharedWithAvatar;
/** @var string */
private $sharedBy;
/** @var string */
private $shareOwner;
/** @var int */
private $permissions;
/** @var IAttributes */
private $attributes;
/** @var int */
private $status;
/** @var string */
private $note = '';
/** @var \DateTime */
private $expireDate;
/** @var string */
private $password;
private ?\DateTimeInterface $passwordExpirationTime = null;
/** @var bool */
private $sendPasswordByTalk = false;
/** @var string */
private $token;
/** @var int */
private $parent;
/** @var string */
private $target;
/** @var \DateTime */
private $shareTime;
/** @var bool */
private $mailSend;
/** @var string */
private $label = '';

/** @var ICacheEntry|null */
private $nodeCacheEntry;

/** @var bool */
private $hideDownload = false;
private string $id;
private string $providerId;
private Node $node;
private int $fileId;
private string $nodeType;
private int $shareType;
private string $sharedWith;
private string $sharedWithDisplayName;
private string $sharedWithAvatar;
private string $sharedBy;
private string $shareOwner;
private int $permissions;
private IAttributes $attributes;
private int $status;
private string $note = '';
private \DateTime $expireDate;
private bool $overwriteFalsyExpirationDate = true;
private string $password;
private ?\DateTimeInterface $passwordExpirationTime = null;
private bool $sendPasswordByTalk = false;
private string $token;
private int $parent;
private string $target;
private \DateTime $shareTime;
private bool $mailSend;
private string $label = '';
private ?ICacheEntry $nodeCacheEntry = null;
private bool $hideDownload = false;

public function __construct(
private IRootFolder $rootFolder,
Expand Down Expand Up @@ -421,6 +394,21 @@ public function getExpirationDate() {
return $this->expireDate;
}

/**
* @inheritdoc
*/
public function setOverwriteFalsyExpirationDate(bool $overwriteFalsyExpirationDate) {
$this->overwriteFalsyExpirationDate = $overwriteFalsyExpirationDate;
return $this;
}

/**
* @inheritdoc
*/
public function getOverwriteFalsyExpirationDate(): bool {
return $this->overwriteFalsyExpirationDate;
}

/**
* @inheritdoc
*/
Expand Down
24 changes: 21 additions & 3 deletions lib/public/Share/IShare.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,20 +385,38 @@ public function getNote();
/**
* Set the expiration date
*
* @param null|\DateTime $expireDate
* @param \DateTime|null $expireDate
* @return \OCP\Share\IShare The modified object
* @since 9.0.0
*/
public function setExpirationDate($expireDate);
public function setExpirationDate(\DateTime|null $expireDate);

/**
* Get the expiration date
*
* @return null|\DateTime
* @return \DateTime|null
* @since 9.0.0
*/
public function getExpirationDate();

/**
* Set overwrite flag for falsy expiry date vavlues
*
* @param bool $overwriteFalsyExpirationDate
* @return \OCP\Share\IShare The modified object
* @since 27.0.0
*/
public function setOverwriteFalsyExpirationDate(bool $overwriteFalsyExpirationDate);


/**
* Get value of overwrite falsy expiry date flag
*
* @return bool
* @since 27.0.0
*/
public function getOverwriteFalsyExpirationDate();

/**
* Is the share expired ?
*
Expand Down

0 comments on commit 8ef8bf4

Please sign in to comment.