Skip to content

Commit 1d3e74b

Browse files
authored
Merge pull request #44228 from nextcloud/backport/44223/stable28
2 parents 92e4354 + ee02016 commit 1d3e74b

File tree

2 files changed

+83
-64
lines changed

2 files changed

+83
-64
lines changed

apps/files_sharing/lib/Controller/ShareesAPIController.php

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,6 @@
6464
*/
6565
class ShareesAPIController extends OCSController {
6666

67-
/** @var string */
68-
protected $userId;
69-
70-
/** @var IConfig */
71-
protected $config;
72-
73-
/** @var IURLGenerator */
74-
protected $urlGenerator;
75-
76-
/** @var IManager */
77-
protected $shareManager;
78-
7967
/** @var int */
8068
protected $offset = 0;
8169

@@ -105,8 +93,6 @@ class ShareesAPIController extends OCSController {
10593
];
10694

10795
protected $reachedEndFor = [];
108-
/** @var ISearch */
109-
private $collaboratorSearch;
11096

11197
/**
11298
* @param string $UserId
@@ -118,20 +104,15 @@ class ShareesAPIController extends OCSController {
118104
* @param ISearch $collaboratorSearch
119105
*/
120106
public function __construct(
121-
$UserId,
122107
string $appName,
123108
IRequest $request,
124-
IConfig $config,
125-
IURLGenerator $urlGenerator,
126-
IManager $shareManager,
127-
ISearch $collaboratorSearch
109+
protected string $userId,
110+
protected IConfig $config,
111+
protected IURLGenerator $urlGenerator,
112+
protected IManager $shareManager,
113+
protected ISearch $collaboratorSearch,
128114
) {
129115
parent::__construct($appName, $request);
130-
$this->userId = $UserId;
131-
$this->config = $config;
132-
$this->urlGenerator = $urlGenerator;
133-
$this->shareManager = $shareManager;
134-
$this->collaboratorSearch = $collaboratorSearch;
135116
}
136117

137118
/**
@@ -158,6 +139,10 @@ public function search(string $search = '', string $itemType = null, int $page =
158139
return new DataResponse($this->result);
159140
}
160141

142+
if ($this->shareManager->sharingDisabledForUser($this->userId)) {
143+
return new DataResponse($this->result);
144+
}
145+
161146
// never return more than the max. number of results configured in the config.php
162147
$maxResults = $this->config->getSystemValueInt('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT);
163148
if ($maxResults > 0) {

apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php

Lines changed: 74 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use OCA\Files_Sharing\Controller\ShareesAPIController;
3434
use OCA\Files_Sharing\Tests\TestCase;
3535
use OCP\AppFramework\Http;
36+
use OCP\AppFramework\Http\DataResponse;
3637
use OCP\AppFramework\OCS\OCSBadRequestException;
3738
use OCP\Collaboration\Collaborators\ISearch;
3839
use OCP\IConfig;
@@ -65,26 +66,27 @@ class ShareesAPIControllerTest extends TestCase {
6566
/** @var ISearch|MockObject */
6667
protected $collaboratorSearch;
6768

69+
/** @var IConfig|MockObject */
70+
protected $config;
71+
6872
protected function setUp(): void {
6973
parent::setUp();
7074

7175
$this->uid = 'test123';
7276
$this->request = $this->createMock(IRequest::class);
7377
$this->shareManager = $this->createMock(IManager::class);
74-
75-
/** @var IConfig|MockObject $configMock */
76-
$configMock = $this->createMock(IConfig::class);
78+
$this->config = $this->createMock(IConfig::class);
7779

7880
/** @var IURLGenerator|MockObject $urlGeneratorMock */
7981
$urlGeneratorMock = $this->createMock(IURLGenerator::class);
8082

8183
$this->collaboratorSearch = $this->createMock(ISearch::class);
8284

8385
$this->sharees = new ShareesAPIController(
84-
$this->uid,
8586
'files_sharing',
8687
$this->request,
87-
$configMock,
88+
$this->uid,
89+
$this->config,
8890
$urlGeneratorMock,
8991
$this->shareManager,
9092
$this->collaboratorSearch
@@ -96,124 +98,124 @@ public function dataSearch(): array {
9698
$allTypes = [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_REMOTE, IShare::TYPE_REMOTE_GROUP, IShare::TYPE_EMAIL];
9799

98100
return [
99-
[[], '', 'yes', true, true, true, $noRemote, false, true, true],
101+
[[], '', 'yes', false, true, true, true, $noRemote, false, true, true],
100102

101103
// Test itemType
102104
[[
103105
'search' => '',
104-
], '', 'yes', true, true, true, $noRemote, false, true, true],
106+
], '', 'yes', false, true, true, true, $noRemote, false, true, true],
105107
[[
106108
'search' => 'foobar',
107-
], '', 'yes', true, true, true, $noRemote, false, true, true],
109+
], '', 'yes', false, true, true, true, $noRemote, false, true, true],
108110
[[
109111
'search' => 0,
110-
], '', 'yes', true, true, true, $noRemote, false, true, true],
112+
], '', 'yes', false, true, true, true, $noRemote, false, true, true],
111113

112114
// Test itemType
113115
[[
114116
'itemType' => '',
115-
], '', 'yes', true, true, true, $noRemote, false, true, true],
117+
], '', 'yes', false, true, true, true, $noRemote, false, true, true],
116118
[[
117119
'itemType' => 'folder',
118-
], '', 'yes', true, true, true, $allTypes, false, true, true],
120+
], '', 'yes', false, true, true, true, $allTypes, false, true, true],
119121
[[
120122
'itemType' => 0,
121-
], '', 'yes', true, true , true, $noRemote, false, true, true],
123+
], '', 'yes', false, true, true , true, $noRemote, false, true, true],
122124
// Test shareType
123125
[[
124126
'itemType' => 'call',
125-
], '', 'yes', true, true, true, $noRemote, false, true, true],
127+
], '', 'yes', false, true, true, true, $noRemote, false, true, true],
126128
[[
127129
'itemType' => 'call',
128-
], '', 'yes', true, true, true, [0, 4], false, true, false],
130+
], '', 'yes', false, true, true, true, [0, 4], false, true, false],
129131
[[
130132
'itemType' => 'folder',
131-
], '', 'yes', true, true, true, $allTypes, false, true, true],
133+
], '', 'yes', false, true, true, true, $allTypes, false, true, true],
132134
[[
133135
'itemType' => 'folder',
134136
'shareType' => 0,
135-
], '', 'yes', true, true, false, [0], false, true, true],
137+
], '', 'yes', false, true, true, false, [0], false, true, true],
136138
[[
137139
'itemType' => 'folder',
138140
'shareType' => '0',
139-
], '', 'yes', true, true, false, [0], false, true, true],
141+
], '', 'yes', false, true, true, false, [0], false, true, true],
140142
[[
141143
'itemType' => 'folder',
142144
'shareType' => 1,
143-
], '', 'yes', true, true, false, [1], false, true, true],
145+
], '', 'yes', false, true, true, false, [1], false, true, true],
144146
[[
145147
'itemType' => 'folder',
146148
'shareType' => 12,
147-
], '', 'yes', true, true, false, [], false, true, true],
149+
], '', 'yes', false, true, true, false, [], false, true, true],
148150
[[
149151
'itemType' => 'folder',
150152
'shareType' => 'foobar',
151-
], '', 'yes', true, true, true, $allTypes, false, true, true],
153+
], '', 'yes', false, true, true, true, $allTypes, false, true, true],
152154

153155
[[
154156
'itemType' => 'folder',
155157
'shareType' => [0, 1, 2],
156-
], '', 'yes', false, false, false, [0, 1], false, true, true],
158+
], '', 'yes', false, false, false, false, [0, 1], false, true, true],
157159
[[
158160
'itemType' => 'folder',
159161
'shareType' => [0, 1],
160-
], '', 'yes', false, false, false, [0, 1], false, true, true],
162+
], '', 'yes', false, false, false, false, [0, 1], false, true, true],
161163
[[
162164
'itemType' => 'folder',
163165
'shareType' => $allTypes,
164-
], '', 'yes', true, true, true, $allTypes, false, true, true],
166+
], '', 'yes', false, true, true, true, $allTypes, false, true, true],
165167
[[
166168
'itemType' => 'folder',
167169
'shareType' => $allTypes,
168-
], '', 'yes', false, false, false, [0, 1], false, true, true],
170+
], '', 'yes', false, false, false, false, [0, 1], false, true, true],
169171
[[
170172
'itemType' => 'folder',
171173
'shareType' => $allTypes,
172-
], '', 'yes', true, false, false, [0, 6], false, true, false],
174+
], '', 'yes', false, true, false, false, [0, 6], false, true, false],
173175
[[
174176
'itemType' => 'folder',
175177
'shareType' => $allTypes,
176-
], '', 'yes', false, false, true, [0, 4], false, true, false],
178+
], '', 'yes', false, false, false, true, [0, 4], false, true, false],
177179
[[
178180
'itemType' => 'folder',
179181
'shareType' => $allTypes,
180-
], '', 'yes', true, true, false, [0, 6, 9], false, true, false],
182+
], '', 'yes', false, true, true, false, [0, 6, 9], false, true, false],
181183

182184
// Test pagination
183185
[[
184186
'itemType' => 'folder',
185187
'page' => 1,
186-
], '', 'yes', true, true, true, $allTypes, false, true, true],
188+
], '', 'yes', false, true, true, true, $allTypes, false, true, true],
187189
[[
188190
'itemType' => 'folder',
189191
'page' => 10,
190-
], '', 'yes', true, true, true, $allTypes, false, true, true],
192+
], '', 'yes', false, true, true, true, $allTypes, false, true, true],
191193

192194
// Test perPage
193195
[[
194196
'itemType' => 'folder',
195197
'perPage' => 1,
196-
], '', 'yes', true, true, true, $allTypes, false, true, true],
198+
], '', 'yes', false, true, true, true, $allTypes, false, true, true],
197199
[[
198200
'itemType' => 'folder',
199201
'perPage' => 10,
200-
], '', 'yes', true, true, true, $allTypes, false, true, true],
202+
], '', 'yes', false, true, true, true, $allTypes, false, true, true],
201203

202204
// Test $shareWithGroupOnly setting
203205
[[
204206
'itemType' => 'folder',
205-
], 'no', 'yes', true, true, true, $allTypes, false, true, true],
207+
], 'no', 'yes', false, true, true, true, $allTypes, false, true, true],
206208
[[
207209
'itemType' => 'folder',
208-
], 'yes', 'yes', true, true, true, $allTypes, true, true, true],
210+
], 'yes', 'yes', false, true, true, true, $allTypes, true, true, true],
209211

210212
// Test $shareeEnumeration setting
211213
[[
212214
'itemType' => 'folder',
213-
], 'no', 'yes', true, true, true, $allTypes, false, true, true],
215+
], 'no', 'yes', false, true, true, true, $allTypes, false, true, true],
214216
[[
215217
'itemType' => 'folder',
216-
], 'no', 'no', true, true, true, $allTypes, false, false, true],
218+
], 'no', 'no', false, true, true, true, $allTypes, false, false, true],
217219

218220
];
219221
}
@@ -233,7 +235,19 @@ public function dataSearch(): array {
233235
* @param bool $allowGroupSharing
234236
* @throws OCSBadRequestException
235237
*/
236-
public function testSearch(array $getData, string $apiSetting, string $enumSetting, bool $remoteSharingEnabled, bool $isRemoteGroupSharingEnabled, bool $emailSharingEnabled, array $shareTypes, bool $shareWithGroupOnly, bool $shareeEnumeration, bool $allowGroupSharing) {
238+
public function testSearch(
239+
array $getData,
240+
string $apiSetting,
241+
string $enumSetting,
242+
bool $sharingDisabledForUser,
243+
bool $remoteSharingEnabled,
244+
bool $isRemoteGroupSharingEnabled,
245+
bool $emailSharingEnabled,
246+
array $shareTypes,
247+
bool $shareWithGroupOnly,
248+
bool $shareeEnumeration,
249+
bool $allowGroupSharing,
250+
) {
237251
$search = $getData['search'] ?? '';
238252
$itemType = $getData['itemType'] ?? 'irrelevant';
239253
$page = $getData['page'] ?? 1;
@@ -263,15 +277,15 @@ public function testSearch(array $getData, string $apiSetting, string $enumSetti
263277
/** @var MockObject|ShareesAPIController $sharees */
264278
$sharees = $this->getMockBuilder(ShareesAPIController::class)
265279
->setConstructorArgs([
266-
$uid,
267280
'files_sharing',
268281
$request,
282+
$uid,
269283
$config,
270284
$urlGenerator,
271285
$this->shareManager,
272286
$this->collaboratorSearch
273287
])
274-
->setMethods(['isRemoteSharingAllowed', 'shareProviderExists', 'isRemoteGroupSharingAllowed'])
288+
->onlyMethods(['isRemoteSharingAllowed', 'isRemoteGroupSharingAllowed'])
275289
->getMock();
276290

277291
$expectedShareTypes = $shareTypes;
@@ -293,6 +307,10 @@ public function testSearch(array $getData, string $apiSetting, string $enumSetti
293307
->with($itemType)
294308
->willReturn($isRemoteGroupSharingEnabled);
295309

310+
$this->shareManager->expects($this->any())
311+
->method('sharingDisabledForUser')
312+
->with($uid)
313+
->willReturn($sharingDisabledForUser);
296314

297315
$this->shareManager->expects($this->any())
298316
->method('shareProviderExists')
@@ -358,15 +376,15 @@ public function testSearchInvalid($getData, $message) {
358376
/** @var MockObject|ShareesAPIController $sharees */
359377
$sharees = $this->getMockBuilder('\OCA\Files_Sharing\Controller\ShareesAPIController')
360378
->setConstructorArgs([
361-
$uid,
362379
'files_sharing',
363380
$request,
381+
$uid,
364382
$config,
365383
$urlGenerator,
366384
$this->shareManager,
367385
$this->collaboratorSearch
368386
])
369-
->setMethods(['isRemoteSharingAllowed'])
387+
->onlyMethods(['isRemoteSharingAllowed'])
370388
->getMock();
371389
$sharees->expects($this->never())
372390
->method('isRemoteSharingAllowed');
@@ -401,6 +419,22 @@ public function testIsRemoteSharingAllowed($itemType, $expected) {
401419
$this->assertSame($expected, $this->invokePrivate($this->sharees, 'isRemoteSharingAllowed', [$itemType]));
402420
}
403421

422+
public function testSearchSharingDisabled() {
423+
$this->shareManager->expects($this->once())
424+
->method('sharingDisabledForUser')
425+
->with($this->uid)
426+
->willReturn(true);
427+
428+
$this->config->expects($this->once())
429+
->method('getSystemValueInt')
430+
->with('sharing.minSearchStringLength', 0)
431+
->willReturn(0);
432+
433+
$this->shareManager->expects($this->never())
434+
->method('allowGroupSharing');
435+
436+
$this->assertInstanceOf(DataResponse::class, $this->sharees->search('', null, 1, 10, [], false));
437+
}
404438

405439
public function testSearchNoItemType() {
406440
$this->expectException(\OCP\AppFramework\OCS\OCSBadRequestException::class);

0 commit comments

Comments
 (0)