mirror of
https://github.com/nextcloud/server.git
synced 2026-02-19 02:38:40 -05:00
refactor(files_external): migrate Ajax with OC_JSON to proper controller
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
This commit is contained in:
parent
86ef7781c0
commit
31571476d3
8 changed files with 67 additions and 107 deletions
|
|
@ -1,42 +0,0 @@
|
|||
<?php
|
||||
|
||||
use OCP\IGroupManager;
|
||||
use OCP\IUserManager;
|
||||
use OCP\Server;
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
\OC_JSON::checkAppEnabled('files_external');
|
||||
\OC_JSON::callCheck();
|
||||
|
||||
\OC_JSON::checkAdminUser();
|
||||
|
||||
$pattern = '';
|
||||
$limit = null;
|
||||
$offset = null;
|
||||
if (isset($_GET['pattern'])) {
|
||||
$pattern = (string)$_GET['pattern'];
|
||||
}
|
||||
if (isset($_GET['limit'])) {
|
||||
$limit = (int)$_GET['limit'];
|
||||
}
|
||||
if (isset($_GET['offset'])) {
|
||||
$offset = (int)$_GET['offset'];
|
||||
}
|
||||
|
||||
$groups = [];
|
||||
foreach (Server::get(IGroupManager::class)->search($pattern, $limit, $offset) as $group) {
|
||||
$groups[$group->getGID()] = $group->getDisplayName();
|
||||
}
|
||||
|
||||
$users = [];
|
||||
foreach (Server::get(IUserManager::class)->searchDisplayName($pattern, $limit, $offset) as $user) {
|
||||
$users[$user->getUID()] = $user->getDisplayName();
|
||||
}
|
||||
|
||||
$results = ['groups' => $groups, 'users' => $users];
|
||||
|
||||
\OC_JSON::success($results);
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
\OC_JSON::checkAppEnabled('files_external');
|
||||
\OC_JSON::checkLoggedIn();
|
||||
\OC_JSON::callCheck();
|
||||
$l = \OC::$server->getL10N('files_external');
|
||||
|
||||
// TODO: implement redirect to which storage backend requested this
|
||||
|
|
@ -6,13 +6,6 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
|
||||
$this->create('files_external_oauth2', 'apps/files_external/ajax/oauth2.php')
|
||||
->actionInclude('files_external/ajax/oauth2.php');
|
||||
|
||||
$this->create('files_external_list_applicable', '/apps/files_external/applicable')
|
||||
->actionInclude('files_external/ajax/applicable.php');
|
||||
|
||||
return [
|
||||
'resources' => [
|
||||
'global_storages' => ['url' => '/globalstorages'],
|
||||
|
|
@ -20,11 +13,20 @@ return [
|
|||
'user_global_storages' => ['url' => '/userglobalstorages'],
|
||||
],
|
||||
'routes' => [
|
||||
[
|
||||
'name' => 'Ajax#getApplicableEntities',
|
||||
'url' => '/ajax/applicable',
|
||||
'verb' => 'GET',
|
||||
],
|
||||
[
|
||||
'name' => 'Ajax#oauth2Callback',
|
||||
'url' => '/ajax/oauth2.php',
|
||||
'verb' => 'GET',
|
||||
],
|
||||
[
|
||||
'name' => 'Ajax#getSshKeys',
|
||||
'url' => '/ajax/public_key.php',
|
||||
'verb' => 'POST',
|
||||
'requirements' => [],
|
||||
],
|
||||
[
|
||||
'name' => 'Ajax#saveGlobalCredentials',
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ use OCP\AppFramework\Http\JSONResponse;
|
|||
use OCP\IGroupManager;
|
||||
use OCP\IL10N;
|
||||
use OCP\IRequest;
|
||||
use OCP\IUserManager;
|
||||
use OCP\IUserSession;
|
||||
|
||||
class AjaxController extends Controller {
|
||||
|
|
@ -35,11 +36,45 @@ class AjaxController extends Controller {
|
|||
private GlobalAuth $globalAuth,
|
||||
private IUserSession $userSession,
|
||||
private IGroupManager $groupManager,
|
||||
private IUserManager $userManager,
|
||||
private IL10N $l10n,
|
||||
) {
|
||||
parent::__construct($appName, $request);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Legacy endpoint for oauth2 callback
|
||||
*/
|
||||
#[NoAdminRequired()]
|
||||
public function oauth2Callback(): JSONResponse {
|
||||
return new JSONResponse(['status' => 'success']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of users and groups that match the given pattern.
|
||||
* Used for user and group picker in the admin settings.
|
||||
*
|
||||
* @param string $pattern The search pattern
|
||||
* @param int|null $limit The maximum number of results to return
|
||||
* @param int|null $offset The offset from which to start returning results
|
||||
* @return JSONResponse
|
||||
*/
|
||||
public function getApplicableEntities(string $pattern = '', ?int $limit = null, ?int $offset = null): JSONResponse {
|
||||
$groups = [];
|
||||
foreach ($this->groupManager->search($pattern, $limit, $offset) as $group) {
|
||||
$groups[$group->getGID()] = $group->getDisplayName();
|
||||
}
|
||||
|
||||
$users = [];
|
||||
foreach ($this->userManager->searchDisplayName($pattern, $limit, $offset) as $user) {
|
||||
$users[$user->getUID()] = $user->getDisplayName();
|
||||
}
|
||||
|
||||
$results = ['groups' => $groups, 'users' => $users];
|
||||
return new JSONResponse($results);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $keyLength
|
||||
* @return array
|
||||
|
|
|
|||
|
|
@ -120,7 +120,7 @@ function initApplicableUsersMultiselect($elements, userListLimit) {
|
|||
dropdownCssClass: 'files-external-select2',
|
||||
// minimumInputLength: 1,
|
||||
ajax: {
|
||||
url: OC.generateUrl('apps/files_external/applicable'),
|
||||
url: OC.generateUrl('apps/files_external/ajax/applicable'),
|
||||
dataType: 'json',
|
||||
quietMillis: 100,
|
||||
data(term, page) { // page is the one-based page number tracked by Select2
|
||||
|
|
@ -131,26 +131,21 @@ function initApplicableUsersMultiselect($elements, userListLimit) {
|
|||
}
|
||||
},
|
||||
results(data) {
|
||||
if (data.status === 'success') {
|
||||
const results = []
|
||||
let userCount = 0 // users is an object
|
||||
|
||||
const results = []
|
||||
let userCount = 0 // users is an object
|
||||
// add groups
|
||||
$.each(data.groups, function(gid, group) {
|
||||
results.push({ name: gid + '(group)', displayname: group, type: 'group' })
|
||||
})
|
||||
// add users
|
||||
$.each(data.users, function(id, user) {
|
||||
userCount++
|
||||
results.push({ name: id, displayname: user, type: 'user' })
|
||||
})
|
||||
|
||||
// add groups
|
||||
$.each(data.groups, function(gid, group) {
|
||||
results.push({ name: gid + '(group)', displayname: group, type: 'group' })
|
||||
})
|
||||
// add users
|
||||
$.each(data.users, function(id, user) {
|
||||
userCount++
|
||||
results.push({ name: id, displayname: user, type: 'user' })
|
||||
})
|
||||
|
||||
const more = (userCount >= userListLimit) || (data.groups.length >= userListLimit)
|
||||
return { results, more }
|
||||
} else {
|
||||
// FIXME add error handling
|
||||
}
|
||||
const more = (userCount >= userListLimit) || (data.groups.length >= userListLimit)
|
||||
return { results, more }
|
||||
},
|
||||
},
|
||||
initSelection(element, callback) {
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ use OCP\IGroupManager;
|
|||
use OCP\IL10N;
|
||||
use OCP\IRequest;
|
||||
use OCP\IUser;
|
||||
use OCP\IUserManager;
|
||||
use OCP\IUserSession;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Test\TestCase;
|
||||
|
|
@ -25,6 +26,7 @@ class AjaxControllerTest extends TestCase {
|
|||
private GlobalAuth&MockObject $globalAuth;
|
||||
private IUserSession&MockObject $userSession;
|
||||
private IGroupManager&MockObject $groupManager;
|
||||
private IUserManager&MockObject $userManager;
|
||||
private IL10N&MockObject $l10n;
|
||||
private AjaxController $ajaxController;
|
||||
|
||||
|
|
@ -34,6 +36,7 @@ class AjaxControllerTest extends TestCase {
|
|||
$this->globalAuth = $this->createMock(GlobalAuth::class);
|
||||
$this->userSession = $this->createMock(IUserSession::class);
|
||||
$this->groupManager = $this->createMock(IGroupManager::class);
|
||||
$this->userManager = $this->createMock(IUserManager::class);
|
||||
$this->l10n = $this->createMock(IL10N::class);
|
||||
|
||||
$this->ajaxController = new AjaxController(
|
||||
|
|
@ -43,6 +46,7 @@ class AjaxControllerTest extends TestCase {
|
|||
$this->globalAuth,
|
||||
$this->userSession,
|
||||
$this->groupManager,
|
||||
$this->userManager,
|
||||
$this->l10n,
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -1339,27 +1339,6 @@
|
|||
<code><![CDATA[isReadyForUser]]></code>
|
||||
</UndefinedInterfaceMethod>
|
||||
</file>
|
||||
<file src="apps/files_external/ajax/applicable.php">
|
||||
<DeprecatedMethod>
|
||||
<code><![CDATA[\OC_JSON::callCheck()]]></code>
|
||||
<code><![CDATA[\OC_JSON::checkAdminUser()]]></code>
|
||||
<code><![CDATA[\OC_JSON::checkAppEnabled('files_external')]]></code>
|
||||
<code><![CDATA[\OC_JSON::success($results)]]></code>
|
||||
</DeprecatedMethod>
|
||||
</file>
|
||||
<file src="apps/files_external/ajax/oauth2.php">
|
||||
<DeprecatedMethod>
|
||||
<code><![CDATA[\OC_JSON::callCheck()]]></code>
|
||||
<code><![CDATA[\OC_JSON::checkAppEnabled('files_external')]]></code>
|
||||
<code><![CDATA[\OC_JSON::checkLoggedIn()]]></code>
|
||||
<code><![CDATA[getL10N]]></code>
|
||||
</DeprecatedMethod>
|
||||
</file>
|
||||
<file src="apps/files_external/appinfo/routes.php">
|
||||
<InvalidScope>
|
||||
<code><![CDATA[$this]]></code>
|
||||
</InvalidScope>
|
||||
</file>
|
||||
<file src="apps/files_external/lib/Command/Notify.php">
|
||||
<DeprecatedClass>
|
||||
<code><![CDATA[\OC_Util::normalizeUnicode($parent)]]></code>
|
||||
|
|
|
|||
|
|
@ -116,16 +116,16 @@ class UrlGeneratorTest extends \Test\TestCase {
|
|||
|
||||
public static function provideDocRootAppUrlParts(): array {
|
||||
return [
|
||||
['files_external', 'ajax/oauth2.php', [], '/index.php/apps/files_external/ajax/oauth2.php'],
|
||||
['files_external', 'ajax/oauth2.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php/apps/files_external/ajax/oauth2.php?trut=trat&dut=dat'],
|
||||
['user_ldap', 'ajax/wizard.php', [], '/index.php/apps/user_ldap/ajax/wizard.php'],
|
||||
['user_ldap', 'ajax/wizard.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php/apps/user_ldap/ajax/wizard.php?trut=trat&dut=dat'],
|
||||
['', 'index.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php?trut=trat&dut=dat'],
|
||||
];
|
||||
}
|
||||
|
||||
public static function provideSubDirAppUrlParts(): array {
|
||||
return [
|
||||
['files_external', 'ajax/oauth2.php', [], '/nextcloud/index.php/apps/files_external/ajax/oauth2.php'],
|
||||
['files_external', 'ajax/oauth2.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php/apps/files_external/ajax/oauth2.php?trut=trat&dut=dat'],
|
||||
['user_ldap', 'ajax/wizard.php', [], '/nextcloud/index.php/apps/user_ldap/ajax/wizard.php'],
|
||||
['user_ldap', 'ajax/wizard.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php/apps/user_ldap/ajax/wizard.php?trut=trat&dut=dat'],
|
||||
['', 'index.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php?trut=trat&dut=dat'],
|
||||
];
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue