Skip to content

Commit

Permalink
Merge pull request #702 from nextcloud/bugfix/noid/move-to-bootstrap
Browse files Browse the repository at this point in the history
Move to IBootstrap
  • Loading branch information
ChristophWurst committed Aug 9, 2021
2 parents faa0754 + c4a0834 commit 43380e5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 116 deletions.
25 changes: 0 additions & 25 deletions appinfo/app.php

This file was deleted.

6 changes: 0 additions & 6 deletions appinfo/routes.php
Expand Up @@ -19,12 +19,6 @@
*
*/

// routes are loaded after all apps are setup, thus we can abuse this file to do some delayed setup

use OCA\Guests\AppInfo\Application;

(\OC::$server->query(Application::class))->lateSetup();

return [
'routes' => [
[
Expand Down
114 changes: 29 additions & 85 deletions lib/AppInfo/Application.php
Expand Up @@ -21,110 +21,60 @@

namespace OCA\Guests\AppInfo;

use OC\Server;
use OCA\Guests\Config;
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\Guests\Listener\LoadAdditionalScriptsListener;
use OCA\Guests\GroupBackend;
use OCA\Guests\GuestManager;
use OCA\Guests\Hooks;
use OCA\Guests\Listener\ShareAutoAcceptListener;
use OCA\Guests\Notifications\Notifier;
use OCA\Guests\RestrictionManager;
use OCA\Guests\UserBackend;
use OCP\AppFramework\App;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\IAppContainer;
use OCP\IServerContainer;
use OCP\IUser;
use OCP\Notification\IManager as INotificationManager;
use OCP\Share\Events\ShareCreatedEvent;

class Application extends App {
class Application extends App implements IBootstrap {
public const APP_ID = 'guests';

public function __construct(array $urlParams = []) {
parent::__construct(self::APP_ID, $urlParams);
}

public function setup() {
$this->setupGuestManagement();
$this->setupGuestRestrictions();
$this->setupNotifications();
$this->setupShareAccepting();
}

public function lateSetup() {
$this->getRestrictionManager()->lateSetupRestrictions();
}

/**
* @return GuestManager
*/
private function getGuestManager() {
return $this->getContainer()->query(GuestManager::class);
;
}

/**
* @return RestrictionManager
*/
private function getRestrictionManager() {
return $this->getContainer()->query(RestrictionManager::class);
}

/**
* @return UserBackend
*/
private function getUserBackend() {
return $this->getContainer()->query(UserBackend::class);
}

/**
* @return Hooks
*/
private function getHookManager() {
return $this->getContainer()->query(Hooks::class);
}

/**
* @return Config
*/
private function getConfig() {
return $this->getContainer()->query(Config::class);
}
public function register(IRegistrationContext $context): void {
$context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadAdditionalScriptsListener::class);
$context->registerEventListener(ShareCreatedEvent::class, ShareAutoAcceptListener::class);

private function getNotificationManager(): INotificationManager {
return $this->getContainer()->query(INotificationManager::class);
}

private function setupGuestManagement() {
// need to cheat here since there's no way to register these in IRegistrationContext
$container = $this->getContainer();
/** @var Server $server */
$server = $container->getServer();

$server->getUserManager()->registerBackend($this->getUserBackend());

$server->getEventDispatcher()->addListener(
'OCA\Files::loadAdditionalScripts', function () {
if (!$this->getGuestManager()->isGuest() && $this->getConfig()->canCreateGuests()) {
\OCP\Util::addScript('guests', 'guests-main');
}
}
);

$server->getUserManager()->registerBackend($container->query(UserBackend::class));
$server->getGroupManager()->addBackend($container->query(GroupBackend::class));
/** @var Hooks $hooks */
$server->getEventDispatcher()->addListener('OCP\Share::postShare', [$this->getHookManager(), 'handlePostShare']);
$server->getEventDispatcher()->addListener(IUser::class . '::firstLogin', [$this->getHookManager(), 'handleFirstLogin']);
}

private function setupGuestRestrictions() {
$container = $this->getContainer();
/** @var Server $server */
$server = $container->getServer();
public function boot(IBootContext $context): void {
$this->setupGuestManagement($context->getAppContainer(), $context->getServerContainer());
$this->setupGuestRestrictions($context->getAppContainer(), $context->getServerContainer());
$this->setupNotifications($context->getAppContainer());
$context->getAppContainer()->query(RestrictionManager::class)->lateSetupRestrictions();
}

private function setupGuestManagement(IAppContainer $container, IServerContainer $server): void {
$hookManager = $container->query(Hooks::class);
$server->getEventDispatcher()->addListener('OCP\Share::postShare', [$hookManager, 'handlePostShare']);
$server->getEventDispatcher()->addListener(IUser::class . '::firstLogin', [$hookManager, 'handleFirstLogin']);
}

private function setupGuestRestrictions(IAppContainer $container, IServerContainer $server): void {
$userSession = $server->getUserSession();
$user = $userSession->getUser();
/** @var RestrictionManager $restrictionManager */
$restrictionManager = $this->getRestrictionManager();

$restrictionManager = $container->query(RestrictionManager::class);

if ($user) {
$restrictionManager->verifyAccess();
Expand All @@ -137,14 +87,8 @@ private function setupGuestRestrictions() {
}
}

private function setupNotifications(): void {
$notificationManager = $this->getNotificationManager();
private function setupNotifications(IAppContainer $container): void {
$notificationManager = $container->query(INotificationManager::class);
$notificationManager->registerNotifierService(Notifier::class);
}

private function setupShareAccepting(): void {
/** @var IEventDispatcher $dispatcher */
$dispatcher = $this->getContainer()->query(IEventDispatcher::class);
$dispatcher->addServiceListener(ShareCreatedEvent::class, ShareAutoAcceptListener::class);
}
}
36 changes: 36 additions & 0 deletions lib/Listener/LoadAdditionalScriptsListener.php
@@ -0,0 +1,36 @@
<?php
/*
* @copyright Copyright (c) 2021 Vincent Petry <vincent@nextcloud.com>
*
* @author Vincent Petry <vincent@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

declare(strict_types=1);


namespace OCA\Guests\Listener;

use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;

class LoadAdditionalScriptsListener implements IEventListener {
public function handle(Event $event): void {
\OCP\Util::addScript('guests', 'guests-main');
}
}

0 comments on commit 43380e5

Please sign in to comment.