Skip to content

Commit

Permalink
Merge pull request #26319 from nextcloud/files_external_app.php
Browse files Browse the repository at this point in the history
Move files_external to IBootstrap and remove some deprecated stuff in the process
  • Loading branch information
MorrisJobke authored Jun 16, 2021
2 parents cebfe4b + 47a8d0d commit 4c93249
Show file tree
Hide file tree
Showing 13 changed files with 239 additions and 263 deletions.
51 changes: 0 additions & 51 deletions apps/files_external/appinfo/app.php

This file was deleted.

59 changes: 29 additions & 30 deletions apps/files_external/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
\OCA\Files_External\MountConfig::$app->registerRoutes(
$this,
[
'resources' => [
'global_storages' => ['url' => '/globalstorages'],
'user_storages' => ['url' => '/userstorages'],
'user_global_storages' => ['url' => '/userglobalstorages'],
],
'routes' => [
[
'name' => 'Ajax#getSshKeys',
'url' => '/ajax/public_key.php',
'verb' => 'POST',
'requirements' => [],
],
[
'name' => 'Ajax#saveGlobalCredentials',
'url' => '/globalcredentials',
'verb' => 'POST',
],
],
'ocs' => [
[
'name' => 'Api#getUserMounts',
'url' => '/api/v1/mounts',
'verb' => 'GET',
],
],
]
);


$this->create('files_external_oauth1', 'apps/files_external/ajax/oauth1.php')
->actionInclude('files_external/ajax/oauth1.php');
Expand All @@ -65,3 +36,31 @@

$this->create('files_external_list_applicable', '/apps/files_external/applicable')
->actionInclude('files_external/ajax/applicable.php');

return [
'resources' => [
'global_storages' => ['url' => '/globalstorages'],
'user_storages' => ['url' => '/userstorages'],
'user_global_storages' => ['url' => '/userglobalstorages'],
],
'routes' => [
[
'name' => 'Ajax#getSshKeys',
'url' => '/ajax/public_key.php',
'verb' => 'POST',
'requirements' => [],
],
[
'name' => 'Ajax#saveGlobalCredentials',
'url' => '/globalcredentials',
'verb' => 'POST',
],
],
'ocs' => [
[
'name' => 'Api#getUserMounts',
'url' => '/api/v1/mounts',
'verb' => 'GET',
],
],
];
73 changes: 37 additions & 36 deletions apps/files_external/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
*/
namespace OCA\Files_External\AppInfo;

use OCA\Files_External\Config\ConfigAdapter;
use OCA\Files_External\Config\UserPlaceholderHandler;
use OCA\Files_External\Service\DBConfigService;
use OCA\Files_External\Listener\GroupDeletedListener;
use OCA\Files_External\Listener\UserDeletedListener;
use OCA\Files_External\Lib\Auth\AmazonS3\AccessKey;
use OCA\Files_External\Lib\Auth\Builtin;
use OCA\Files_External\Lib\Auth\NullMechanism;
Expand Down Expand Up @@ -62,14 +64,19 @@
use OCA\Files_External\Lib\Config\IBackendProvider;
use OCA\Files_External\Service\BackendService;
use OCP\AppFramework\App;
use OCP\IGroup;
use OCP\IUser;
use Symfony\Component\EventDispatcher\GenericEvent;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Files\Config\IMountProviderCollection;
use OCP\Group\Events\GroupDeletedEvent;
use OCP\User\Events\UserDeletedEvent;

require_once __DIR__ . '/../../3rdparty/autoload.php';

/**
* @package OCA\Files_External\AppInfo
*/
class Application extends App implements IBackendProvider, IAuthMechanismProvider {
class Application extends App implements IBackendProvider, IAuthMechanismProvider, IBootstrap {

/**
* Application constructor.
Expand All @@ -78,46 +85,40 @@ class Application extends App implements IBackendProvider, IAuthMechanismProvide
*/
public function __construct(array $urlParams = []) {
parent::__construct('files_external', $urlParams);
}

$container = $this->getContainer();
public function register(IRegistrationContext $context): void {
$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
$context->registerEventListener(GroupDeletedEvent::class, GroupDeletedListener::class);
}

/** @var BackendService $backendService */
$backendService = $container->query(BackendService::class);
$backendService->registerBackendProvider($this);
$backendService->registerAuthMechanismProvider($this);
$backendService->registerConfigHandler('user', function () use ($container) {
return $container->query(UserPlaceholderHandler::class);
public function boot(IBootContext $context): void {
$context->injectFn(function (IMountProviderCollection $mountProviderCollection, ConfigAdapter $configAdapter) {
$mountProviderCollection->registerProvider($configAdapter);
});
\OCA\Files\App::getNavigationManager()->add(function () {
$l = \OC::$server->getL10N('files_external');
return [
'id' => 'extstoragemounts',
'appname' => 'files_external',
'script' => 'list.php',
'order' => 30,
'name' => $l->t('External storage'),
];
});
$context->injectFn(function (BackendService $backendService, UserPlaceholderHandler $userConfigHandler) {
$backendService->registerBackendProvider($this);
$backendService->registerAuthMechanismProvider($this);
$backendService->registerConfigHandler('user', function () use ($userConfigHandler) {
return $userConfigHandler;
});
});

// force-load auth mechanisms since some will register hooks
// TODO: obsolete these and use the TokenProvider to get the user's password from the session
$this->getAuthMechanisms();
}

public function registerListeners() {
$dispatcher = $this->getContainer()->getServer()->getEventDispatcher();
$dispatcher->addListener(
IUser::class . '::postDelete',
function (GenericEvent $event) {
/** @var IUser $user */
$user = $event->getSubject();
/** @var DBConfigService $config */
$config = $this->getContainer()->query(DBConfigService::class);
$config->modifyMountsOnUserDelete($user->getUID());
}
);
$dispatcher->addListener(
IGroup::class . '::postDelete',
function (GenericEvent $event) {
/** @var IGroup $group */
$group = $event->getSubject();
/** @var DBConfigService $config */
$config = $this->getContainer()->query(DBConfigService::class);
$config->modifyMountsOnGroupDelete($group->getGID());
}
);
}

/**
* @{inheritdoc}
*/
Expand Down
2 changes: 1 addition & 1 deletion apps/files_external/lib/Command/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected function configure() {
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$user = $input->getOption('user');
$user = (string) $input->getOption('user');
$mountPoint = $input->getArgument('mount_point');
$storageIdentifier = $input->getArgument('storage_backend');
$authIdentifier = $input->getArgument('authentication_backend');
Expand Down
2 changes: 1 addition & 1 deletion apps/files_external/lib/Command/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected function configure() {
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$user = $input->getOption('user');
$user = (string) $input->getOption('user');
$path = $input->getArgument('path');
if ($path === '-') {
$json = file_get_contents('php://stdin');
Expand Down
51 changes: 34 additions & 17 deletions apps/files_external/lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
*/
namespace OCA\Files_External\Controller;

use OCA\Files_External\Lib\StorageConfig;
use OCA\Files_External\Service\UserGlobalStoragesService;
use OCA\Files_External\Service\UserStoragesService;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\IRequest;
Expand All @@ -38,35 +41,41 @@ class ApiController extends OCSController {

/** @var IUserSession */
private $userSession;
/** @var UserGlobalStoragesService */
private $userGlobalStoragesService;
/** @var UserStoragesService */
private $userStoragesService;

public function __construct(string $appName,
IRequest $request,
IUserSession $userSession) {
public function __construct(
string $appName,
IRequest $request,
IUserSession $userSession,
UserGlobalStoragesService $userGlobalStorageService,
UserStoragesService $userStorageService
) {
parent::__construct($appName, $request);

$this->userSession = $userSession;
$this->userGlobalStoragesService = $userGlobalStorageService;
$this->userStoragesService = $userStorageService;
}

/**
* Formats the given mount config to a mount entry.
*
* @param string $mountPoint mount point name, relative to the data dir
* @param array $mountConfig mount config to format
* @param StorageConfig $mountConfig mount config to format
*
* @return array entry
*/
private function formatMount(string $mountPoint, array $mountConfig): array {
// strip "/$user/files" from mount point
$mountPoint = explode('/', trim($mountPoint, '/'), 3);
$mountPoint = $mountPoint[2] ?? '';

private function formatMount(string $mountPoint, StorageConfig $mountConfig): array {
// split path from mount point
$path = \dirname($mountPoint);
if ($path === '.') {
if ($path === '.' || $path === '/') {
$path = '';
}

$isSystemMount = !$mountConfig['personal'];
$isSystemMount = $mountConfig->getType() === StorageConfig::MOUNT_TYPE_ADMIN;

$permissions = \OCP\Constants::PERMISSION_READ;
// personal mounts can be deleted
Expand All @@ -78,11 +87,11 @@ private function formatMount(string $mountPoint, array $mountConfig): array {
'name' => basename($mountPoint),
'path' => $path,
'type' => 'dir',
'backend' => $mountConfig['backend'],
'backend' => $mountConfig->getBackend()->getText(),
'scope' => $isSystemMount ? 'system' : 'personal',
'permissions' => $permissions,
'id' => $mountConfig['id'],
'class' => $mountConfig['class']
'id' => $mountConfig->getId(),
'class' => $mountConfig->getBackend()->getIdentifier(),
];
return $entry;
}
Expand All @@ -96,10 +105,18 @@ private function formatMount(string $mountPoint, array $mountConfig): array {
*/
public function getUserMounts(): DataResponse {
$entries = [];
$user = $this->userSession->getUser()->getUID();
$mountPoints = [];

foreach ($this->userGlobalStoragesService->getStorages() as $storage) {
$mountPoint = $storage->getMountPoint();
$mountPoints[$mountPoint] = $storage;
}

$mounts = \OCA\Files_External\MountConfig::getAbsoluteMountPoints($user);
foreach ($mounts as $mountPoint => $mount) {
foreach ($this->userStoragesService->getStorages() as $storage) {
$mountPoint = $storage->getMountPoint();
$mountPoints[$mountPoint] = $storage;
}
foreach ($mountPoints as $mountPoint => $mount) {
$entries[] = $this->formatMount($mountPoint, $mount);
}

Expand Down
Loading

0 comments on commit 4c93249

Please sign in to comment.