Skip to content

Commit c9940b1

Browse files
committed
Adds a system config value to allow permanent share passwords
This commit is part of #31005 Signed-off-by: Cyrille Bollu <cyrpub@bollu.be>
1 parent 343d9e7 commit c9940b1

File tree

4 files changed

+54
-34
lines changed

4 files changed

+54
-34
lines changed

apps/files_sharing/lib/Controller/ShareAPIController.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ protected function formatShare(IShare $share, Node $recipientNode = null): array
278278
} elseif ($share->getShareType() === IShare::TYPE_EMAIL) {
279279
$result['share_with'] = $share->getSharedWith();
280280
$result['password'] = $share->getPassword();
281+
$result['password_expiration_time'] = $share->getPasswordExpirationTime();
281282
$result['send_password_by_talk'] = $share->getSendPasswordByTalk();
282283
$result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'EMAIL');
283284
$result['token'] = $share->getToken();
@@ -561,6 +562,10 @@ public function createShare(
561562
// Set password
562563
if ($password !== '') {
563564
$share->setPassword($password);
565+
// Shares shared by email have temporary passwords by default
566+
if ($shareType === IShare::TYPE_EMAIL) {
567+
$this->setSharePasswordExpirationTime($share);
568+
}
564569
}
565570

566571
// Only share by mail have a recipient
@@ -1168,6 +1173,7 @@ public function updateShare(
11681173
$share->setPassword(null);
11691174
} elseif ($password !== null) {
11701175
$share->setPassword($password);
1176+
$this->setSharePasswordExpirationTime($share);
11711177
}
11721178

11731179
if ($label !== null) {
@@ -1504,6 +1510,34 @@ private function parseDate(string $expireDate): \DateTime {
15041510
return $date;
15051511
}
15061512

1513+
/**
1514+
* Set the share's password expiration time
1515+
*
1516+
* @param IShare $share
1517+
*
1518+
*/
1519+
private function setSharePasswordExpirationTime($share) {
1520+
if ($this->config->getSystemValue('allow_mail_share_permanent_password')) {
1521+
// Sets password expiration date to NULL
1522+
$share->setPasswordExpirationTime();
1523+
} else {
1524+
// Sets password expiration date
1525+
try {
1526+
$now = new \DateTime();
1527+
$expirationInterval = $this->config->getSystemValue('share_temporary_password_expiration_interval');
1528+
if ($expirationInterval === '') {
1529+
$expirationInterval = 'P0DT15M';
1530+
}
1531+
$expirationTime = $now->add(new \DateInterval($expirationInterval));
1532+
} catch (\Exception $e) {
1533+
// Catches invalid format for system value 'share_temporary_password_expiration_interval'
1534+
$expirationTime = $now->add(new \DateInterval('P0DT15M'));
1535+
} finally {
1536+
$share->setPasswordExpirationTime($expirationTime);
1537+
}
1538+
}
1539+
}
1540+
15071541
/**
15081542
* Since we have multiple providers but the OCS Share API v1 does
15091543
* not support this we need to check all backends.

apps/sharebymail/lib/ShareByMailProvider.php

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -753,43 +753,29 @@ public function update(IShare $share, $plainTextPassword = null) {
753753
$this->sendPassword($share, $plainTextPassword);
754754
}
755755

756-
// Gets password expiration interval. Defaults to 15 minutes.
757-
$expirationInterval = $this->config->getSystemValue('share_temporary_password_expiration_interval');
758-
if ($expirationInterval === '') {
759-
$expirationInterval = 'P0DT15M';
760-
}
761-
762756
/*
763757
* We allow updating the permissions and password of mail shares
764758
*/
765-
$now = new \DateTime();
766-
try {
767-
$expirationTime = $now->add(new \DateInterval($expirationInterval));
768-
} catch (\Exception $e) {
769-
// Catches invalid format for system value 'share_temporary_password_expiration_interval'
770-
$expirationTime = $now->add(new \DateInterval('P0DT15M'));
771-
} finally {
772-
$qb = $this->dbConnection->getQueryBuilder();
773-
$qb->update('share')
774-
->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
775-
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
776-
->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
777-
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
778-
->set('password', $qb->createNamedParameter($share->getPassword()))
779-
->set('password_expiration_time', $qb->createNamedParameter($expirationTime, IQueryBuilder::PARAM_DATE))
780-
->set('label', $qb->createNamedParameter($share->getLabel()))
781-
->set('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL))
782-
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
783-
->set('note', $qb->createNamedParameter($share->getNote()))
784-
->set('hide_download', $qb->createNamedParameter((int)$share->getHideDownload(), IQueryBuilder::PARAM_INT))
785-
->executeStatement();
786-
787-
if ($originalShare->getNote() !== $share->getNote() && $share->getNote() !== '') {
788-
$this->sendNote($share);
789-
}
759+
$qb = $this->dbConnection->getQueryBuilder();
760+
$qb->update('share')
761+
->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
762+
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
763+
->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
764+
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
765+
->set('password', $qb->createNamedParameter($share->getPassword()))
766+
->set('password_expiration_time', $qb->createNamedParameter($share->getPasswordExpirationTime(), IQueryBuilder::PARAM_DATE))
767+
->set('label', $qb->createNamedParameter($share->getLabel()))
768+
->set('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL))
769+
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
770+
->set('note', $qb->createNamedParameter($share->getNote()))
771+
->set('hide_download', $qb->createNamedParameter((int)$share->getHideDownload(), IQueryBuilder::PARAM_INT))
772+
->executeStatement();
790773

791-
return $share;
774+
if ($originalShare->getNote() !== $share->getNote() && $share->getNote() !== '') {
775+
$this->sendNote($share);
792776
}
777+
778+
return $share;
793779
}
794780

795781
/**

lib/private/Share20/Share.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ public function getPassword() {
466466
/**
467467
* @inheritdoc
468468
*/
469-
public function setPasswordExpirationTime($passwordExpirationTime) {
469+
public function setPasswordExpirationTime($passwordExpirationTime = null) {
470470
$this->passwordExpirationTime = $passwordExpirationTime;
471471
return $this;
472472
}

lib/public/Share/IShare.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ public function getPassword();
453453
*
454454
* @return \OCP\Share\IShare The modified object
455455
*/
456-
public function setPasswordExpirationTime($passwordExpirationTime);
456+
public function setPasswordExpirationTime($passwordExpirationTime = null);
457457

458458
/**
459459
* Get the password's expiration time of this share.

0 commit comments

Comments
 (0)