Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to IBootstrap #702

Merged
merged 6 commits into from Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
111 changes: 26 additions & 85 deletions lib/AppInfo/Application.php
Expand Up @@ -21,110 +21,57 @@

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 register(IRegistrationContext $context): void {
$context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadAdditionalScriptsListener::class);
$context->registerEventListener(ShareCreatedEvent::class, ShareAutoAcceptListener::class);
}

public function lateSetup() {
$this->getRestrictionManager()->lateSetupRestrictions();
public function boot(IBootContext $context): void {
$this->setupGuestManagement($context->getAppContainer(), $context->getServerContainer());
PVince81 marked this conversation as resolved.
Show resolved Hide resolved
$this->setupGuestRestrictions($context->getAppContainer(), $context->getServerContainer());
$this->setupNotifications($context->getAppContainer());
$context->getAppContainer()->query(RestrictionManager::class)->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);
}

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

private function setupGuestManagement() {
$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');
}
}
);

private function setupGuestManagement(IAppContainer $container, IServerContainer $server): void {
$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();
$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 +84,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');
}
}