Skip to content

Commit 7b60e3d

Browse files
Merge pull request #55370 from nextcloud/refactor/files_external-ajax
2 parents 86ef778 + d20d4d3 commit 7b60e3d

File tree

10 files changed

+70
-110
lines changed

10 files changed

+70
-110
lines changed

apps/files_external/ajax/applicable.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

apps/files_external/ajax/oauth2.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

apps/files_external/appinfo/routes.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,27 @@
66
* SPDX-License-Identifier: AGPL-3.0-only
77
*/
88

9-
10-
$this->create('files_external_oauth2', 'apps/files_external/ajax/oauth2.php')
11-
->actionInclude('files_external/ajax/oauth2.php');
12-
13-
$this->create('files_external_list_applicable', '/apps/files_external/applicable')
14-
->actionInclude('files_external/ajax/applicable.php');
15-
169
return [
1710
'resources' => [
1811
'global_storages' => ['url' => '/globalstorages'],
1912
'user_storages' => ['url' => '/userstorages'],
2013
'user_global_storages' => ['url' => '/userglobalstorages'],
2114
],
2215
'routes' => [
16+
[
17+
'name' => 'Ajax#getApplicableEntities',
18+
'url' => '/ajax/applicable',
19+
'verb' => 'GET',
20+
],
21+
[
22+
'name' => 'Ajax#oauth2Callback',
23+
'url' => '/ajax/oauth2.php',
24+
'verb' => 'GET',
25+
],
2326
[
2427
'name' => 'Ajax#getSshKeys',
2528
'url' => '/ajax/public_key.php',
2629
'verb' => 'POST',
27-
'requirements' => [],
2830
],
2931
[
3032
'name' => 'Ajax#saveGlobalCredentials',

apps/files_external/lib/Controller/AjaxController.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use OCP\IGroupManager;
1818
use OCP\IL10N;
1919
use OCP\IRequest;
20+
use OCP\IUserManager;
2021
use OCP\IUserSession;
2122

2223
class AjaxController extends Controller {
@@ -35,11 +36,45 @@ public function __construct(
3536
private GlobalAuth $globalAuth,
3637
private IUserSession $userSession,
3738
private IGroupManager $groupManager,
39+
private IUserManager $userManager,
3840
private IL10N $l10n,
3941
) {
4042
parent::__construct($appName, $request);
4143
}
4244

45+
46+
/**
47+
* Legacy endpoint for oauth2 callback
48+
*/
49+
#[NoAdminRequired()]
50+
public function oauth2Callback(): JSONResponse {
51+
return new JSONResponse(['status' => 'success']);
52+
}
53+
54+
/**
55+
* Returns a list of users and groups that match the given pattern.
56+
* Used for user and group picker in the admin settings.
57+
*
58+
* @param string $pattern The search pattern
59+
* @param int|null $limit The maximum number of results to return
60+
* @param int|null $offset The offset from which to start returning results
61+
* @return JSONResponse
62+
*/
63+
public function getApplicableEntities(string $pattern = '', ?int $limit = null, ?int $offset = null): JSONResponse {
64+
$groups = [];
65+
foreach ($this->groupManager->search($pattern, $limit, $offset) as $group) {
66+
$groups[$group->getGID()] = $group->getDisplayName();
67+
}
68+
69+
$users = [];
70+
foreach ($this->userManager->searchDisplayName($pattern, $limit, $offset) as $user) {
71+
$users[$user->getUID()] = $user->getDisplayName();
72+
}
73+
74+
$results = ['groups' => $groups, 'users' => $users];
75+
return new JSONResponse($results);
76+
}
77+
4378
/**
4479
* @param int $keyLength
4580
* @return array

apps/files_external/src/settings.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function initApplicableUsersMultiselect($elements, userListLimit) {
120120
dropdownCssClass: 'files-external-select2',
121121
// minimumInputLength: 1,
122122
ajax: {
123-
url: OC.generateUrl('apps/files_external/applicable'),
123+
url: OC.generateUrl('apps/files_external/ajax/applicable'),
124124
dataType: 'json',
125125
quietMillis: 100,
126126
data(term, page) { // page is the one-based page number tracked by Select2
@@ -131,26 +131,21 @@ function initApplicableUsersMultiselect($elements, userListLimit) {
131131
}
132132
},
133133
results(data) {
134-
if (data.status === 'success') {
135-
136-
const results = []
137-
let userCount = 0 // users is an object
134+
const results = []
135+
let userCount = 0 // users is an object
138136

139-
// add groups
140-
$.each(data.groups, function(gid, group) {
141-
results.push({ name: gid + '(group)', displayname: group, type: 'group' })
142-
})
143-
// add users
144-
$.each(data.users, function(id, user) {
145-
userCount++
146-
results.push({ name: id, displayname: user, type: 'user' })
147-
})
137+
// add groups
138+
$.each(data.groups, function(gid, group) {
139+
results.push({ name: gid + '(group)', displayname: group, type: 'group' })
140+
})
141+
// add users
142+
$.each(data.users, function(id, user) {
143+
userCount++
144+
results.push({ name: id, displayname: user, type: 'user' })
145+
})
148146

149-
const more = (userCount >= userListLimit) || (data.groups.length >= userListLimit)
150-
return { results, more }
151-
} else {
152-
// FIXME add error handling
153-
}
147+
const more = (userCount >= userListLimit) || (data.groups.length >= userListLimit)
148+
return { results, more }
154149
},
155150
},
156151
initSelection(element, callback) {

apps/files_external/tests/Controller/AjaxControllerTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use OCP\IL10N;
1616
use OCP\IRequest;
1717
use OCP\IUser;
18+
use OCP\IUserManager;
1819
use OCP\IUserSession;
1920
use PHPUnit\Framework\MockObject\MockObject;
2021
use Test\TestCase;
@@ -25,6 +26,7 @@ class AjaxControllerTest extends TestCase {
2526
private GlobalAuth&MockObject $globalAuth;
2627
private IUserSession&MockObject $userSession;
2728
private IGroupManager&MockObject $groupManager;
29+
private IUserManager&MockObject $userManager;
2830
private IL10N&MockObject $l10n;
2931
private AjaxController $ajaxController;
3032

@@ -34,6 +36,7 @@ protected function setUp(): void {
3436
$this->globalAuth = $this->createMock(GlobalAuth::class);
3537
$this->userSession = $this->createMock(IUserSession::class);
3638
$this->groupManager = $this->createMock(IGroupManager::class);
39+
$this->userManager = $this->createMock(IUserManager::class);
3740
$this->l10n = $this->createMock(IL10N::class);
3841

3942
$this->ajaxController = new AjaxController(
@@ -43,6 +46,7 @@ protected function setUp(): void {
4346
$this->globalAuth,
4447
$this->userSession,
4548
$this->groupManager,
49+
$this->userManager,
4650
$this->l10n,
4751
);
4852

build/psalm-baseline.xml

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,27 +1339,6 @@
13391339
<code><![CDATA[isReadyForUser]]></code>
13401340
</UndefinedInterfaceMethod>
13411341
</file>
1342-
<file src="apps/files_external/ajax/applicable.php">
1343-
<DeprecatedMethod>
1344-
<code><![CDATA[\OC_JSON::callCheck()]]></code>
1345-
<code><![CDATA[\OC_JSON::checkAdminUser()]]></code>
1346-
<code><![CDATA[\OC_JSON::checkAppEnabled('files_external')]]></code>
1347-
<code><![CDATA[\OC_JSON::success($results)]]></code>
1348-
</DeprecatedMethod>
1349-
</file>
1350-
<file src="apps/files_external/ajax/oauth2.php">
1351-
<DeprecatedMethod>
1352-
<code><![CDATA[\OC_JSON::callCheck()]]></code>
1353-
<code><![CDATA[\OC_JSON::checkAppEnabled('files_external')]]></code>
1354-
<code><![CDATA[\OC_JSON::checkLoggedIn()]]></code>
1355-
<code><![CDATA[getL10N]]></code>
1356-
</DeprecatedMethod>
1357-
</file>
1358-
<file src="apps/files_external/appinfo/routes.php">
1359-
<InvalidScope>
1360-
<code><![CDATA[$this]]></code>
1361-
</InvalidScope>
1362-
</file>
13631342
<file src="apps/files_external/lib/Command/Notify.php">
13641343
<DeprecatedClass>
13651344
<code><![CDATA[\OC_Util::normalizeUnicode($parent)]]></code>

dist/files_external-settings.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/files_external-settings.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/lib/UrlGeneratorTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,16 @@ public static function provideRoutes(): array {
116116

117117
public static function provideDocRootAppUrlParts(): array {
118118
return [
119-
['files_external', 'ajax/oauth2.php', [], '/index.php/apps/files_external/ajax/oauth2.php'],
120-
['files_external', 'ajax/oauth2.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php/apps/files_external/ajax/oauth2.php?trut=trat&dut=dat'],
119+
['user_ldap', 'ajax/wizard.php', [], '/index.php/apps/user_ldap/ajax/wizard.php'],
120+
['user_ldap', 'ajax/wizard.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php/apps/user_ldap/ajax/wizard.php?trut=trat&dut=dat'],
121121
['', 'index.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php?trut=trat&dut=dat'],
122122
];
123123
}
124124

125125
public static function provideSubDirAppUrlParts(): array {
126126
return [
127-
['files_external', 'ajax/oauth2.php', [], '/nextcloud/index.php/apps/files_external/ajax/oauth2.php'],
128-
['files_external', 'ajax/oauth2.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php/apps/files_external/ajax/oauth2.php?trut=trat&dut=dat'],
127+
['user_ldap', 'ajax/wizard.php', [], '/nextcloud/index.php/apps/user_ldap/ajax/wizard.php'],
128+
['user_ldap', 'ajax/wizard.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php/apps/user_ldap/ajax/wizard.php?trut=trat&dut=dat'],
129129
['', 'index.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php?trut=trat&dut=dat'],
130130
];
131131
}

0 commit comments

Comments
 (0)