Skip to content

Commit c19ec67

Browse files
authored
Merge pull request #32719 from nextcloud/fix/noid/email-shares-not-shown
Fix email shares not being shown to other users
2 parents a94de5c + c1f054a commit c19ec67

File tree

2 files changed

+313
-1
lines changed

2 files changed

+313
-1
lines changed

apps/sharebymail/lib/ShareByMailProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offs
855855
$or1
856856
)
857857
);
858-
} else {
858+
} elseif ($node === null) {
859859
$qb->andWhere(
860860
$qb->expr()->orX(
861861
$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2022 Richard Steinmetz <[email protected]>
7+
*
8+
* @author Richard Steinmetz <[email protected]>
9+
*
10+
* @license AGPL-3.0-or-later
11+
*
12+
* This code is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License, version 3,
14+
* as published by the Free Software Foundation.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License, version 3,
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>
23+
*
24+
*/
25+
26+
namespace Test\Share20;
27+
28+
use OC\Files\Node\Node;
29+
use OCA\ShareByMail\Settings\SettingsManager;
30+
use OCA\ShareByMail\ShareByMailProvider;
31+
use OCP\DB\QueryBuilder\IQueryBuilder;
32+
use OCP\Defaults;
33+
use OCP\EventDispatcher\IEventDispatcher;
34+
use OCP\Files\IRootFolder;
35+
use OCP\IConfig;
36+
use OCP\IDBConnection;
37+
use OCP\IL10N;
38+
use OCP\ILogger;
39+
use OCP\IURLGenerator;
40+
use OCP\IUserManager;
41+
use OCP\Mail\IMailer;
42+
use OCP\Security\IHasher;
43+
use OCP\Security\ISecureRandom;
44+
use OCP\Share\IShare;
45+
use PHPUnit\Framework\MockObject\MockObject;
46+
use Test\TestCase;
47+
48+
/**
49+
* Class ShareByMailProviderTest
50+
*
51+
* @package Test\Share20
52+
* @group DB
53+
*/
54+
class ShareByMailProviderTest extends TestCase {
55+
56+
/** @var IDBConnection */
57+
protected $dbConn;
58+
59+
/** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject */
60+
protected $userManager;
61+
62+
/** @var IRootFolder | \PHPUnit\Framework\MockObject\MockObject */
63+
protected $rootFolder;
64+
65+
/** @var ShareByMailProvider */
66+
protected $provider;
67+
68+
/** @var \PHPUnit\Framework\MockObject\MockObject|IMailer */
69+
protected $mailer;
70+
71+
/** @var \PHPUnit\Framework\MockObject\MockObject|IL10N */
72+
protected $l10n;
73+
74+
/** @var \PHPUnit\Framework\MockObject\MockObject|Defaults */
75+
protected $defaults;
76+
77+
/** @var \PHPUnit\Framework\MockObject\MockObject|IURLGenerator */
78+
protected $urlGenerator;
79+
80+
/** @var IConfig|MockObject */
81+
protected $config;
82+
83+
/** @var ILogger|MockObject */
84+
private $logger;
85+
86+
/** @var IHasher|MockObject */
87+
private $hasher;
88+
89+
/** @var \OCP\Activity\IManager|MockObject */
90+
private $activityManager;
91+
92+
/** @var IEventDispatcher|MockObject */
93+
private $eventDispatcher;
94+
95+
/** @var \OCP\Share\IManager|MockObject */
96+
private $shareManager;
97+
98+
/** @var ISecureRandom|MockObject */
99+
private $secureRandom;
100+
101+
/** @var SettingsManager|MockObject */
102+
private $settingsManager;
103+
104+
protected function setUp(): void {
105+
$this->dbConn = \OC::$server->getDatabaseConnection();
106+
$this->userManager = $this->createMock(IUserManager::class);
107+
$this->rootFolder = $this->createMock(IRootFolder::class);
108+
$this->mailer = $this->createMock(IMailer::class);
109+
$this->l10n = $this->createMock(IL10N::class);
110+
$this->defaults = $this->getMockBuilder(Defaults::class)->disableOriginalConstructor()->getMock();
111+
$this->urlGenerator = $this->createMock(IURLGenerator::class);
112+
$this->logger = $this->createMock(ILogger::class);
113+
$this->activityManager = $this->createMock(\OCP\Activity\IManager::class);
114+
$this->settingsManager = $this->createMock(SettingsManager::class);
115+
$this->hasher = $this->createMock(IHasher::class);
116+
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
117+
$this->shareManager = $this->createMock(\OCP\Share\IManager::class);
118+
$this->secureRandom = $this->createMock(ISecureRandom::class);
119+
$this->config = $this->createMock(IConfig::class);
120+
121+
// Empty share table
122+
$this->dbConn->getQueryBuilder()->delete('share')->execute();
123+
124+
$this->provider = new ShareByMailProvider(
125+
$this->config,
126+
$this->dbConn,
127+
$this->secureRandom,
128+
$this->userManager,
129+
$this->rootFolder,
130+
$this->l10n,
131+
$this->logger,
132+
$this->mailer,
133+
$this->urlGenerator,
134+
$this->activityManager,
135+
$this->settingsManager,
136+
$this->defaults,
137+
$this->hasher,
138+
$this->eventDispatcher,
139+
$this->shareManager,
140+
);
141+
}
142+
143+
protected function tearDown(): void {
144+
$this->dbConn->getQueryBuilder()->delete('share')->execute();
145+
$this->dbConn->getQueryBuilder()->delete('filecache')->execute();
146+
$this->dbConn->getQueryBuilder()->delete('storages')->execute();
147+
}
148+
149+
/**
150+
* @param int $shareType
151+
* @param string $sharedWith
152+
* @param string $sharedBy
153+
* @param string $shareOwner
154+
* @param string $itemType
155+
* @param int $fileSource
156+
* @param string $fileTarget
157+
* @param int $permissions
158+
* @param $token
159+
* @param $expiration
160+
* @param $parent
161+
* @return int
162+
*
163+
* @throws \OCP\DB\Exception
164+
*/
165+
private function addShareToDB($shareType, $sharedWith, $sharedBy, $shareOwner,
166+
$itemType, $fileSource, $fileTarget, $permissions, $token, $expiration,
167+
$parent) {
168+
$qb = $this->dbConn->getQueryBuilder();
169+
$qb->insert('share');
170+
171+
if ($shareType) {
172+
$qb->setValue('share_type', $qb->expr()->literal($shareType));
173+
}
174+
if ($sharedWith) {
175+
$qb->setValue('share_with', $qb->expr()->literal($sharedWith));
176+
}
177+
if ($sharedBy) {
178+
$qb->setValue('uid_initiator', $qb->expr()->literal($sharedBy));
179+
}
180+
if ($shareOwner) {
181+
$qb->setValue('uid_owner', $qb->expr()->literal($shareOwner));
182+
}
183+
if ($itemType) {
184+
$qb->setValue('item_type', $qb->expr()->literal($itemType));
185+
}
186+
if ($fileSource) {
187+
$qb->setValue('file_source', $qb->expr()->literal($fileSource));
188+
}
189+
if ($fileTarget) {
190+
$qb->setValue('file_target', $qb->expr()->literal($fileTarget));
191+
}
192+
if ($permissions) {
193+
$qb->setValue('permissions', $qb->expr()->literal($permissions));
194+
}
195+
if ($token) {
196+
$qb->setValue('token', $qb->expr()->literal($token));
197+
}
198+
if ($expiration) {
199+
$qb->setValue('expiration', $qb->createNamedParameter($expiration, IQueryBuilder::PARAM_DATE));
200+
}
201+
if ($parent) {
202+
$qb->setValue('parent', $qb->expr()->literal($parent));
203+
}
204+
205+
$this->assertEquals(1, $qb->execute());
206+
return $qb->getLastInsertId();
207+
}
208+
209+
public function testGetSharesByWithResharesAndNoNode() {
210+
$this->addShareToDB(
211+
IShare::TYPE_EMAIL,
212+
213+
'user1',
214+
'user1',
215+
'folder',
216+
42,
217+
null,
218+
17,
219+
'foobar',
220+
null,
221+
null,
222+
);
223+
$this->addShareToDB(
224+
IShare::TYPE_EMAIL,
225+
226+
'user2',
227+
'user2',
228+
'folder',
229+
42,
230+
null,
231+
17,
232+
'barfoo',
233+
null,
234+
null,
235+
);
236+
237+
// Return own shares only if not asked for a specific node
238+
/** @var IShare[] $actual */
239+
$actual = $this->provider->getSharesBy(
240+
'user1',
241+
IShare::TYPE_EMAIL,
242+
null,
243+
true,
244+
-1,
245+
0,
246+
);
247+
248+
$this->assertCount(1, $actual);
249+
250+
$this->assertEquals(IShare::TYPE_EMAIL, $actual[0]->getShareType());
251+
$this->assertEquals('user1', $actual[0]->getSharedBy());
252+
$this->assertEquals('user1', $actual[0]->getShareOwner());
253+
$this->assertEquals('[email protected]', $actual[0]->getSharedWith());
254+
}
255+
256+
public function testGetSharesByWithResharesAndNode() {
257+
$this->addShareToDB(
258+
IShare::TYPE_EMAIL,
259+
260+
'user1',
261+
'user1',
262+
'folder',
263+
42,
264+
null,
265+
17,
266+
'foobar',
267+
null,
268+
null,
269+
);
270+
$this->addShareToDB(
271+
IShare::TYPE_EMAIL,
272+
273+
'user2',
274+
'user2',
275+
'folder',
276+
42,
277+
null,
278+
17,
279+
'barfoo',
280+
null,
281+
null,
282+
);
283+
284+
$node = $this->createMock(Node::class);
285+
$node->expects($this->once())
286+
->method('getId')
287+
->willReturn(42);
288+
289+
// Return all shares if asked for specific node
290+
/** @var IShare[] $actual */
291+
$actual = $this->provider->getSharesBy(
292+
'user1',
293+
IShare::TYPE_EMAIL,
294+
$node,
295+
true,
296+
-1,
297+
0,
298+
);
299+
300+
$this->assertCount(2, $actual);
301+
302+
$this->assertEquals(IShare::TYPE_EMAIL, $actual[0]->getShareType());
303+
$this->assertEquals('user1', $actual[0]->getSharedBy());
304+
$this->assertEquals('user1', $actual[0]->getShareOwner());
305+
$this->assertEquals('[email protected]', $actual[0]->getSharedWith());
306+
307+
$this->assertEquals(IShare::TYPE_EMAIL, $actual[1]->getShareType());
308+
$this->assertEquals('user2', $actual[1]->getSharedBy());
309+
$this->assertEquals('user2', $actual[1]->getShareOwner());
310+
$this->assertEquals('[email protected]', $actual[1]->getSharedWith());
311+
}
312+
}

0 commit comments

Comments
 (0)