Remove duplicated query for email shares

Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
Signed-off-by: npmbuildbot-nextcloud[bot] <npmbuildbot-nextcloud[bot]@users.noreply.github.com>
This commit is contained in:
Daniel Calviño Sánchez 2020-12-30 00:48:01 +01:00 committed by John Molakvoæ (skjnldsv)
parent 0f3f6a6c54
commit f99876997a
No known key found for this signature in database
GPG Key ID: 60C25B8C072916CF
6 changed files with 100 additions and 102 deletions

View File

@ -556,7 +556,7 @@ class ShareAPIController extends OCSController {
}
// Only share by mail have a recipient
if ($shareType === IShare::TYPE_EMAIL) {
if (is_string($shareWith) && $shareType === IShare::TYPE_EMAIL) {
$share->setSharedWith($shareWith);
}
@ -1591,7 +1591,6 @@ class ShareAPIController extends OCSController {
IShare::TYPE_GROUP,
IShare::TYPE_LINK,
IShare::TYPE_EMAIL,
IShare::TYPE_EMAIL,
IShare::TYPE_CIRCLE,
IShare::TYPE_ROOM,
IShare::TYPE_DECK

View File

@ -27,7 +27,6 @@ declare(strict_types=1);
namespace OCA\ShareByMail;
use OCA\ShareByMail\Settings\SettingsManager;
use OCP\Capabilities\ICapability;
use OCP\Share\IManager;

View File

@ -740,25 +740,23 @@ class Manager implements IManager {
}
try {
//Verify share type
// Verify share type
if ($share->getShareType() === IShare::TYPE_USER) {
$this->userCreateChecks($share);
//Verify the expiration date
// Verify the expiration date
$share = $this->validateExpirationDateInternal($share);
} elseif ($share->getShareType() === IShare::TYPE_GROUP) {
$this->groupCreateChecks($share);
//Verify the expiration date
// Verify the expiration date
$share = $this->validateExpirationDateInternal($share);
} elseif ($share->getShareType() === IShare::TYPE_LINK
|| $share->getShareType() === IShare::TYPE_EMAIL) {
$this->linkCreateChecks($share);
$this->setLinkParent($share);
/*
* For now ignore a set token.
*/
// For now ignore a set token.
$share->setToken(
$this->secureRandom->generate(
\OC\Share\Constants::TOKEN_LENGTH,
@ -767,7 +765,7 @@ class Manager implements IManager {
);
// Verify the expiration date
$share = $this->validateExpirationDateLink($share);
$share = $this->validateExpirationDate($share);
// Verify the password
$this->verifyPassword($share->getPassword());
@ -799,7 +797,8 @@ class Manager implements IManager {
$oldShare = $share;
$provider = $this->factory->getProviderForType($share->getShareType());
$share = $provider->create($share);
//reuse the node we already have
// Reuse the node we already have
$share->setNode($oldShare->getNode());
// Reset the target if it is null for the new share
@ -987,38 +986,23 @@ class Manager implements IManager {
|| $share->getShareType() === IShare::TYPE_EMAIL) {
$this->linkCreateChecks($share);
// The new password is not set again if it is the same as the old one.
$plainTextPassword = $share->getPassword();
$this->updateSharePasswordIfNeeded($share, $originalShare);
if (empty($plainTextPassword) && $share->getSendPasswordByTalk()) {
throw new \InvalidArgumentException('Cant enable sending the password by Talk with an empty password');
}
if ($share->getExpirationDate() != $originalShare->getExpirationDate()) {
//Verify the expiration date
$this->validateExpirationDateLink($share);
$expirationDateUpdated = true;
}
} elseif ($share->getShareType() === IShare::TYPE_EMAIL) {
// The new password is not set again if it is the same as the old
// one.
$plainTextPassword = $share->getPassword();
if (!empty($plainTextPassword) && !$this->updateSharePasswordIfNeeded($share, $originalShare)) {
$plainTextPassword = null;
if (empty($plainTextPassword)) {
if (!$originalShare->getSendPasswordByTalk() && $share->getSendPasswordByTalk()) {
// If the same password was already sent by mail the recipient
// would already have access to the share without having to call
// the sharer to verify her identity
throw new \InvalidArgumentException('Cant enable sending the password by Talk without setting a new password');
}
if ($originalShare->getSendPasswordByTalk() && !$share->getSendPasswordByTalk()) {
throw new \InvalidArgumentException('Cant disable sending the password by Talk without setting a new password');
}
}
$this->updateSharePasswordIfNeeded($share, $originalShare);
if (empty($plainTextPassword) && !$originalShare->getSendPasswordByTalk() && $share->getSendPasswordByTalk()) {
// If the same password was already sent by mail the recipient
// would already have access to the share without having to call
// the sharer to verify her identity
throw new \InvalidArgumentException('Cant enable sending the password by Talk without setting a new password');
} elseif (empty($plainTextPassword) && $originalShare->getSendPasswordByTalk() && !$share->getSendPasswordByTalk()) {
throw new \InvalidArgumentException('Cant disable sending the password by Talk without setting a new password');
}
if ($share->getExpirationDate() != $originalShare->getExpirationDate()) {
// Verify the expiration date
$this->validateExpirationDate($share);

View File

@ -40,7 +40,7 @@ use OCP\IUserManager;
use OCP\Share\Exceptions\IllegalIDChangeException;
use OCP\Share\IShare;
class Share implements \OCP\Share\IShare {
class Share implements IShare {
/** @var string */
private $id;

View File

@ -433,7 +433,7 @@ interface IShare {
* When the share is passed to the share manager to be created
* or updated the password will be hashed.
*
* @param string $password
* @param string|null $password
* @return \OCP\Share\IShare The modified object
* @since 9.0.0
*/

View File

@ -50,7 +50,6 @@ use OCP\Mail\IMailer;
use OCP\Security\Events\ValidatePasswordPolicyEvent;
use OCP\Security\IHasher;
use OCP\Security\ISecureRandom;
use OCP\Share\Exceptions\AlreadySharedException;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IProviderFactory;
use OCP\Share\IShare;
@ -1068,7 +1067,7 @@ class ManagerTest extends \Test\TestCase {
$share = $this->manager->newShare();
$share->setExpirationDate($past);
self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
}
public function testValidateExpirationDateEnforceButNotSet() {
@ -1084,7 +1083,7 @@ class ManagerTest extends \Test\TestCase {
['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
]);
self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
}
public function testValidateExpirationDateEnforceButNotEnabledAndNotSet() {
@ -1096,7 +1095,7 @@ class ManagerTest extends \Test\TestCase {
['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
]);
self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
$this->assertNull($share->getExpirationDate());
}
@ -1116,7 +1115,7 @@ class ManagerTest extends \Test\TestCase {
$expected->setTime(0,0,0);
$expected->add(new \DateInterval('P3D'));
self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
$this->assertNotNull($share->getExpirationDate());
$this->assertEquals($expected, $share->getExpirationDate());
@ -1137,7 +1136,7 @@ class ManagerTest extends \Test\TestCase {
$expected->setTime(0,0,0);
$expected->add(new \DateInterval('P1D'));
self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
$this->assertNotNull($share->getExpirationDate());
$this->assertEquals($expected, $share->getExpirationDate());
@ -1160,7 +1159,7 @@ class ManagerTest extends \Test\TestCase {
['core', 'shareapi_default_expire_date', 'no', 'yes'],
]);
self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
}
public function testValidateExpirationDateEnforceValid() {
@ -1187,7 +1186,7 @@ class ManagerTest extends \Test\TestCase {
return $data['expirationDate'] == $future;
}));
self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
$this->assertEquals($expected, $share->getExpirationDate());
}
@ -1209,7 +1208,7 @@ class ManagerTest extends \Test\TestCase {
return $data['expirationDate'] == $expected && $data['passwordSet'] === false;
}));
self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
$this->assertEquals($expected, $share->getExpirationDate());
}
@ -1224,7 +1223,7 @@ class ManagerTest extends \Test\TestCase {
$share = $this->manager->newShare();
$share->setPassword('password');
self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
$this->assertNull($share->getExpirationDate());
}
@ -1249,7 +1248,7 @@ class ManagerTest extends \Test\TestCase {
return $data['expirationDate'] == $expected;
}));
self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
$this->assertEquals($expected, $share->getExpirationDate());
}
@ -1278,7 +1277,7 @@ class ManagerTest extends \Test\TestCase {
return $data['expirationDate'] == $expected;
}));
self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
$this->assertEquals($expected, $share->getExpirationDate());
}
@ -1299,7 +1298,7 @@ class ManagerTest extends \Test\TestCase {
$share = $this->manager->newShare();
$share->setExpirationDate($nextWeek);
self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
$save->sub(new \DateInterval('P2D'));
$this->assertEquals($save, $share->getExpirationDate());
@ -1323,7 +1322,7 @@ class ManagerTest extends \Test\TestCase {
$data['message'] = 'Invalid date!';
});
self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
}
public function testValidateExpirationDateExistingShareNoDefault() {
@ -1337,7 +1336,7 @@ class ManagerTest extends \Test\TestCase {
['core', 'shareapi_expire_after_n_days', '7', '6'],
]);
self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
self::invokePrivate($this->manager, 'validateExpirationDate', [$share]);
$this->assertEquals(null, $share->getExpirationDate());
}
@ -2049,7 +2048,7 @@ class ManagerTest extends \Test\TestCase {
'generalCreateChecks',
'linkCreateChecks',
'pathCreateChecks',
'validateExpirationDateLink',
'validateExpirationDate',
'verifyPassword',
'setLinkParent',
])
@ -2091,7 +2090,7 @@ class ManagerTest extends \Test\TestCase {
->method('pathCreateChecks')
->with($path);
$manager->expects($this->once())
->method('validateExpirationDateLink')
->method('validateExpirationDate')
->with($share)
->willReturn($share);
$manager->expects($this->once())
@ -2174,7 +2173,7 @@ class ManagerTest extends \Test\TestCase {
'generalCreateChecks',
'linkCreateChecks',
'pathCreateChecks',
'validateExpirationDateLink',
'validateExpirationDate',
'verifyPassword',
'setLinkParent',
])
@ -2203,17 +2202,19 @@ class ManagerTest extends \Test\TestCase {
$manager->expects($this->once())
->method('generalCreateChecks')
->with($share);
;
$manager->expects($this->never())
$manager->expects($this->once())
->method('linkCreateChecks');
$manager->expects($this->once())
->method('pathCreateChecks')
->with($path);
$manager->expects($this->never())
->method('validateExpirationDateLink');
$manager->expects($this->never())
$manager->expects($this->once())
->method('validateExpirationDate')
->with($share)
->willReturn($share);
$manager->expects($this->once())
->method('verifyPassword');
$manager->expects($this->never())
$manager->expects($this->once())
->method('setLinkParent');
$this->secureRandom->method('generate')
@ -3024,7 +3025,7 @@ class ManagerTest extends \Test\TestCase {
'linkCreateChecks',
'pathCreateChecks',
'verifyPassword',
'validateExpirationDateLink',
'validateExpirationDate',
])
->getMock();
@ -3053,7 +3054,7 @@ class ManagerTest extends \Test\TestCase {
$manager->expects($this->once())->method('canShare')->willReturn(true);
$manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
$manager->expects($this->once())->method('validateExpirationDateLink')->with($share);
$manager->expects($this->once())->method('validateExpirationDate')->with($share);
$manager->expects($this->once())->method('verifyPassword')->with('password');
$this->hasher->expects($this->once())
@ -3095,7 +3096,7 @@ class ManagerTest extends \Test\TestCase {
public function testUpdateShareLinkEnableSendPasswordByTalkWithNoPassword() {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Cant enable sending the password by Talk with an empty password');
$this->expectExceptionMessage('Cant enable sending the password by Talk without setting a new password');
$manager = $this->createManagerMock()
->setMethods([
@ -3105,7 +3106,7 @@ class ManagerTest extends \Test\TestCase {
'linkCreateChecks',
'pathCreateChecks',
'verifyPassword',
'validateExpirationDateLink',
'validateExpirationDate',
])
->getMock();
@ -3139,7 +3140,7 @@ class ManagerTest extends \Test\TestCase {
$manager->expects($this->once())->method('linkCreateChecks')->with($share);
$manager->expects($this->never())->method('verifyPassword');
$manager->expects($this->never())->method('pathCreateChecks');
$manager->expects($this->never())->method('validateExpirationDateLink');
$manager->expects($this->never())->method('validateExpirationDate');
$this->hasher->expects($this->never())
->method('hash');
@ -3171,7 +3172,7 @@ class ManagerTest extends \Test\TestCase {
'verifyPassword',
'pathCreateChecks',
'linkCreateChecks',
'validateExpirationDateLink',
'validateExpirationDate',
])
->getMock();
@ -3203,8 +3204,8 @@ class ManagerTest extends \Test\TestCase {
$manager->expects($this->once())->method('generalCreateChecks')->with($share);
$manager->expects($this->once())->method('verifyPassword')->with('password');
$manager->expects($this->once())->method('pathCreateChecks')->with($file);
$manager->expects($this->never())->method('linkCreateChecks');
$manager->expects($this->never())->method('validateExpirationDateLink');
$manager->expects($this->once())->method('linkCreateChecks');
$manager->expects($this->once())->method('validateExpirationDate');
$this->hasher->expects($this->once())
->method('hash')
@ -3218,7 +3219,12 @@ class ManagerTest extends \Test\TestCase {
$hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
\OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
$hookListener->expects($this->never())->method('post');
$hookListener->expects($this->once())->method('post')->with([
'itemType' => 'file',
'itemSource' => 100,
'date' => $tomorrow,
'uidOwner' => 'owner',
]);
$hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
\OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
@ -3246,7 +3252,7 @@ class ManagerTest extends \Test\TestCase {
'verifyPassword',
'pathCreateChecks',
'linkCreateChecks',
'validateExpirationDateLink',
'validateExpirationDate',
])
->getMock();
@ -3281,8 +3287,8 @@ class ManagerTest extends \Test\TestCase {
$manager->expects($this->once())->method('generalCreateChecks')->with($share);
$manager->expects($this->once())->method('verifyPassword')->with('password');
$manager->expects($this->once())->method('pathCreateChecks')->with($file);
$manager->expects($this->never())->method('linkCreateChecks');
$manager->expects($this->never())->method('validateExpirationDateLink');
$manager->expects($this->once())->method('linkCreateChecks');
$manager->expects($this->once())->method('validateExpirationDate');
$this->hasher->expects($this->once())
->method('hash')
@ -3296,7 +3302,12 @@ class ManagerTest extends \Test\TestCase {
$hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
\OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
$hookListener->expects($this->never())->method('post');
$hookListener->expects($this->once())->method('post')->with([
'itemType' => 'file',
'itemSource' => 100,
'date' => $tomorrow,
'uidOwner' => 'owner',
]);
$hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
\OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
@ -3324,7 +3335,7 @@ class ManagerTest extends \Test\TestCase {
'verifyPassword',
'pathCreateChecks',
'linkCreateChecks',
'validateExpirationDateLink',
'validateExpirationDate',
])
->getMock();
@ -3359,8 +3370,8 @@ class ManagerTest extends \Test\TestCase {
$manager->expects($this->once())->method('generalCreateChecks')->with($share);
$manager->expects($this->once())->method('verifyPassword')->with('password');
$manager->expects($this->once())->method('pathCreateChecks')->with($file);
$manager->expects($this->never())->method('linkCreateChecks');
$manager->expects($this->never())->method('validateExpirationDateLink');
$manager->expects($this->once())->method('linkCreateChecks');
$manager->expects($this->once())->method('validateExpirationDate');
$this->hasher->expects($this->once())
->method('verify')
@ -3379,7 +3390,12 @@ class ManagerTest extends \Test\TestCase {
$hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
\OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
$hookListener->expects($this->never())->method('post');
$hookListener->expects($this->once())->method('post')->with([
'itemType' => 'file',
'itemSource' => 100,
'date' => $tomorrow,
'uidOwner' => 'owner',
]);
$hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
\OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
@ -3410,7 +3426,7 @@ class ManagerTest extends \Test\TestCase {
'verifyPassword',
'pathCreateChecks',
'linkCreateChecks',
'validateExpirationDateLink',
'validateExpirationDate',
])
->getMock();
@ -3445,8 +3461,8 @@ class ManagerTest extends \Test\TestCase {
$manager->expects($this->once())->method('generalCreateChecks')->with($share);
$manager->expects($this->never())->method('verifyPassword');
$manager->expects($this->never())->method('pathCreateChecks');
$manager->expects($this->never())->method('linkCreateChecks');
$manager->expects($this->never())->method('validateExpirationDateLink');
$manager->expects($this->once())->method('linkCreateChecks');
$manager->expects($this->never())->method('validateExpirationDate');
$this->hasher->expects($this->never())
->method('hash');
@ -3482,7 +3498,7 @@ class ManagerTest extends \Test\TestCase {
'verifyPassword',
'pathCreateChecks',
'linkCreateChecks',
'validateExpirationDateLink',
'validateExpirationDate',
])
->getMock();
@ -3517,8 +3533,8 @@ class ManagerTest extends \Test\TestCase {
$manager->expects($this->once())->method('generalCreateChecks')->with($share);
$manager->expects($this->never())->method('verifyPassword');
$manager->expects($this->never())->method('pathCreateChecks');
$manager->expects($this->never())->method('linkCreateChecks');
$manager->expects($this->never())->method('validateExpirationDateLink');
$manager->expects($this->once())->method('linkCreateChecks');
$manager->expects($this->never())->method('validateExpirationDate');
$this->hasher->expects($this->never())
->method('hash');
@ -3554,7 +3570,7 @@ class ManagerTest extends \Test\TestCase {
'verifyPassword',
'pathCreateChecks',
'linkCreateChecks',
'validateExpirationDateLink',
'validateExpirationDate',
])
->getMock();
@ -3589,8 +3605,8 @@ class ManagerTest extends \Test\TestCase {
$manager->expects($this->once())->method('generalCreateChecks')->with($share);
$manager->expects($this->never())->method('verifyPassword');
$manager->expects($this->never())->method('pathCreateChecks');
$manager->expects($this->never())->method('linkCreateChecks');
$manager->expects($this->never())->method('validateExpirationDateLink');
$manager->expects($this->once())->method('linkCreateChecks');
$manager->expects($this->never())->method('validateExpirationDate');
$this->hasher->expects($this->never())
->method('hash');
@ -3626,7 +3642,7 @@ class ManagerTest extends \Test\TestCase {
'verifyPassword',
'pathCreateChecks',
'linkCreateChecks',
'validateExpirationDateLink',
'validateExpirationDate',
])
->getMock();
@ -3661,8 +3677,8 @@ class ManagerTest extends \Test\TestCase {
$manager->expects($this->once())->method('generalCreateChecks')->with($share);
$manager->expects($this->never())->method('verifyPassword');
$manager->expects($this->never())->method('pathCreateChecks');
$manager->expects($this->never())->method('linkCreateChecks');
$manager->expects($this->never())->method('validateExpirationDateLink');
$manager->expects($this->once())->method('linkCreateChecks');
$manager->expects($this->never())->method('validateExpirationDate');
$this->hasher->expects($this->once())
->method('verify')
@ -3702,7 +3718,7 @@ class ManagerTest extends \Test\TestCase {
'verifyPassword',
'pathCreateChecks',
'linkCreateChecks',
'validateExpirationDateLink',
'validateExpirationDate',
])
->getMock();
@ -3735,10 +3751,10 @@ class ManagerTest extends \Test\TestCase {
$manager->expects($this->once())->method('canShare')->willReturn(true);
$manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
$manager->expects($this->once())->method('generalCreateChecks')->with($share);
$manager->expects($this->never())->method('verifyPassword');
$manager->expects($this->never())->method('pathCreateChecks');
$manager->expects($this->never())->method('linkCreateChecks');
$manager->expects($this->never())->method('validateExpirationDateLink');
$manager->expects($this->once())->method('verifyPassword');
$manager->expects($this->once())->method('pathCreateChecks');
$manager->expects($this->once())->method('linkCreateChecks');
$manager->expects($this->once())->method('validateExpirationDate');
$this->hasher->expects($this->once())
->method('verify')
@ -3778,7 +3794,7 @@ class ManagerTest extends \Test\TestCase {
'verifyPassword',
'pathCreateChecks',
'linkCreateChecks',
'validateExpirationDateLink',
'validateExpirationDate',
])
->getMock();
@ -3811,10 +3827,10 @@ class ManagerTest extends \Test\TestCase {
$manager->expects($this->once())->method('canShare')->willReturn(true);
$manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
$manager->expects($this->once())->method('generalCreateChecks')->with($share);
$manager->expects($this->never())->method('verifyPassword');
$manager->expects($this->never())->method('pathCreateChecks');
$manager->expects($this->never())->method('linkCreateChecks');
$manager->expects($this->never())->method('validateExpirationDateLink');
$manager->expects($this->once())->method('verifyPassword');
$manager->expects($this->once())->method('pathCreateChecks');
$manager->expects($this->once())->method('linkCreateChecks');
$manager->expects($this->once())->method('validateExpirationDate');
$this->hasher->expects($this->never())
->method('verify');