aboutsummaryrefslogtreecommitdiffstats
path: root/apps/testing/lib/AlternativeHomeUserBackend.php
blob: b6d39cf6b99227e7b0fa40b999d90d2f840804ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
 * @copyright Copyright (c) 2016, ownCloud GmbH.
 *
 * @author Vincent Petry <vincent@nextcloud.com>
 *
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * 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, version 3,
 * along with this program. If not, see <http://www.gnu.org/licenses/>
 *
 */
namespace OCA\Testing;

/**
 * Alternative home user backend.
 *
 * It returns a md5 of the home folder instead of the user id.
 * To configure, need to add this in config.php:
 *	'user_backends' => [
 *			'default' => false, [
 *				'class' => '\\OCA\\Testing\\AlternativeHomeUserBackend',
 *				'arguments' => [],
 *			],
 *	]
 */
class AlternativeHomeUserBackend extends \OC\User\Database {
	public function __construct() {
		parent::__construct();
	}
	/**
	 * get the user's home directory
	 * @param string $uid the username
	 * @return string|false
	 */
	public function getHome($uid) {
		if ($this->userExists($uid)) {
			// workaround to avoid killing the admin
			if ($uid !== 'admin') {
				$uid = md5($uid);
			}
			return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $uid;
		}

		return false;
	}
}
348'>348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
<?php

declare(strict_types=1);

/**
 * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
 * @author Joas Schilling <coding@schilljs.com>
 * @author Julius Härtl <jus@bitgrid.net>
 *
 * @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/>.
 *
 */

namespace OC\AppFramework\Bootstrap;

use Closure;
use OC\Support\CrashReport\Registry;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Dashboard\IManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ILogger;
use Throwable;

class RegistrationContext {

	/** @var array[] */
	private $capabilities = [];

	/** @var array[] */
	private $crashReporters = [];

	/** @var array[] */
	private $dashboardPanels = [];

	/** @var array[] */
	private $services = [];

	/** @var array[] */
	private $aliases = [];

	/** @var array[] */
	private $parameters = [];

	/** @var array[] */
	private $eventListeners = [];

	/** @var array[] */
	private $middlewares = [];

	/** @var array[] */
	private $searchProviders = [];

	/** @var array[] */
	private $alternativeLogins = [];

	/** @var array[] */
	private $initialStates = [];

	/** @var ILogger */
	private $logger;

	public function __construct(ILogger $logger) {
		$this->logger = $logger;
	}

	public function for(string $appId): IRegistrationContext {
		return new class($appId, $this) implements IRegistrationContext {
			/** @var string */
			private $appId;

			/** @var RegistrationContext */
			private $context;

			public function __construct(string $appId, RegistrationContext $context) {
				$this->appId = $appId;
				$this->context = $context;
			}

			public function registerCapability(string $capability): void {
				$this->context->registerCapability(
					$this->appId,
					$capability
				);
			}

			public function registerCrashReporter(string $reporterClass): void {
				$this->context->registerCrashReporter(
					$this->appId,
					$reporterClass
				);
			}

			public function registerDashboardWidget(string $widgetClass): void {
				$this->context->registerDashboardPanel(
					$this->appId,
					$widgetClass
				);
			}

			public function registerService(string $name, callable $factory, bool $shared = true): void {
				$this->context->registerService(
					$this->appId,
					$name,
					$factory,
					$shared
				);
			}

			public function registerServiceAlias(string $alias, string $target): void {
				$this->context->registerServiceAlias(
					$this->appId,
					$alias,
					$target
				);
			}

			public function registerParameter(string $name, $value): void {
				$this->context->registerParameter(
					$this->appId,
					$name,
					$value
				);
			}

			public function registerEventListener(string $event, string $listener, int $priority = 0): void {
				$this->context->registerEventListener(
					$this->appId,
					$event,
					$listener,
					$priority
				);
			}

			public function registerMiddleware(string $class): void {
				$this->context->registerMiddleware(
					$this->appId,
					$class
				);
			}

			public function registerSearchProvider(string $class): void {
				$this->context->registerSearchProvider(
					$this->appId,
					$class
				);
			}

			public function registerAlternativeLogin(string $class): void {
				$this->context->registerAlternativeLogin(
					$this->appId,
					$class
				);
			}

			public function registerInitialStateProvider(string $class): void {
				$this->context->registerInitialState(
					$this->appId,
					$class
				);
			}
		};
	}

	public function registerCapability(string $appId, string $capability): void {
		$this->capabilities[] = [
			'appId' => $appId,
			'capability' => $capability
		];
	}

	public function registerCrashReporter(string $appId, string $reporterClass): void {
		$this->crashReporters[] = [
			'appId' => $appId,
			'class' => $reporterClass,
		];
	}

	public function registerDashboardPanel(string $appId, string $panelClass): void {
		$this->dashboardPanels[] = [
			'appId' => $appId,
			'class' => $panelClass
		];
	}

	public function registerService(string $appId, string $name, callable $factory, bool $shared = true): void {
		$this->services[] = [
			"appId" => $appId,
			"name" => $name,
			"factory" => $factory,
			"shared" => $shared,
		];
	}

	public function registerServiceAlias(string $appId, string $alias, string $target): void {
		$this->aliases[] = [
			"appId" => $appId,
			"alias" => $alias,
			"target" => $target,
		];
	}

	public function registerParameter(string $appId, string $name, $value): void {
		$this->parameters[] = [
			"appId" => $appId,
			"name" => $name,
			"value" => $value,
		];
	}

	public function registerEventListener(string $appId, string $event, string $listener, int $priority = 0): void {
		$this->eventListeners[] = [
			"appId" => $appId,
			"event" => $event,
			"listener" => $listener,
			"priority" => $priority,
		];
	}

	public function registerMiddleware(string $appId, string $class): void {
		$this->middlewares[] = [
			"appId" => $appId,
			"class" => $class,
		];
	}

	public function registerSearchProvider(string $appId, string $class) {
		$this->searchProviders[] = [
			'appId' => $appId,
			'class' => $class,
		];
	}

	public function registerAlternativeLogin(string $appId, string $class): void {
		$this->alternativeLogins[] = [
			'appId' => $appId,
			'class' => $class,
		];
	}

	public function registerInitialState(string $appId, string $class): void {
		$this->initialStates[] = [
			'appId' => $appId,
			'class' => $class,
		];
	}

	/**
	 * @param App[] $apps
	 */
	public function delegateCapabilityRegistrations(array $apps): void {
		while (($registration = array_shift($this->capabilities)) !== null) {
			try {
				$apps[$registration['appId']]
					->getContainer()
					->registerCapability($registration['capability']);
			} catch (Throwable $e) {
				$appId = $registration['appId'];
				$this->logger->logException($e, [
					'message' => "Error during capability registration of $appId: " . $e->getMessage(),
					'level' => ILogger::ERROR,
				]);
			}
		}
	}

	/**
	 * @param App[] $apps
	 */
	public function delegateCrashReporterRegistrations(array $apps, Registry $registry): void {
		while (($registration = array_shift($this->crashReporters)) !== null) {
			try {
				$registry->registerLazy($registration['class']);
			} catch (Throwable $e) {
				$appId = $registration['appId'];
				$this->logger->logException($e, [
					'message' => "Error during crash reporter registration of $appId: " . $e->getMessage(),
					'level' => ILogger::ERROR,
				]);
			}
		}
	}

	/**
	 * @param App[] $apps
	 */
	public function delegateDashboardPanelRegistrations(array $apps, IManager $dashboardManager): void {
		while (($panel = array_shift($this->dashboardPanels)) !== null) {
			try {
				$dashboardManager->lazyRegisterWidget($panel['class']);
			} catch (Throwable $e) {
				$appId = $panel['appId'];
				$this->logger->logException($e, [
					'message' => "Error during dashboard registration of $appId: " . $e->getMessage(),
					'level' => ILogger::ERROR,
				]);
			}
		}
	}

	public function delegateEventListenerRegistrations(IEventDispatcher $eventDispatcher): void {
		while (($registration = array_shift($this->eventListeners)) !== null) {
			try {
				if (isset($registration['priority'])) {
					$eventDispatcher->addServiceListener(
						$registration['event'],
						$registration['listener'],
						$registration['priority']
					);
				} else {
					$eventDispatcher->addServiceListener(
						$registration['event'],
						$registration['listener']
					);
				}
			} catch (Throwable $e) {
				$appId = $registration['appId'];
				$this->logger->logException($e, [
					'message' => "Error during event listener registration of $appId: " . $e->getMessage(),
					'level' => ILogger::ERROR,
				]);
			}
		}
	}

	/**
	 * @param App[] $apps
	 */
	public function delegateContainerRegistrations(array $apps): void {
		while (($registration = array_shift($this->services)) !== null) {
			try {
				/**
				 * Register the service and convert the callable into a \Closure if necessary
				 */
				$apps[$registration['appId']]
					->getContainer()
					->registerService(
						$registration['name'],
						Closure::fromCallable($registration['factory']),
						$registration['shared'] ?? true
					);
			} catch (Throwable $e) {
				$appId = $registration['appId'];
				$this->logger->logException($e, [
					'message' => "Error during service registration of $appId: " . $e->getMessage(),
					'level' => ILogger::ERROR,
				]);
			}
		}

		foreach ($this->aliases as $registration) {
			try {
				$apps[$registration['appId']]
					->getContainer()
					->registerAlias(
						$registration['alias'],
						$registration['target']
					);
			} catch (Throwable $e) {
				$appId = $registration['appId'];
				$this->logger->logException($e, [
					'message' => "Error during service alias registration of $appId: " . $e->getMessage(),
					'level' => ILogger::ERROR,
				]);
			}
		}

		foreach ($this->parameters as $registration) {
			try {
				$apps[$registration['appId']]
					->getContainer()
					->registerParameter(
						$registration['name'],
						$registration['value']
					);
			} catch (Throwable $e) {
				$appId = $registration['appId'];
				$this->logger->logException($e, [
					'message' => "Error during service alias registration of $appId: " . $e->getMessage(),
					'level' => ILogger::ERROR,
				]);
			}
		}
	}

	/**
	 * @param App[] $apps
	 */
	public function delegateMiddlewareRegistrations(array $apps): void {
		while (($middleware = array_shift($this->middlewares)) !== null) {
			try {
				$apps[$middleware['appId']]
					->getContainer()
					->registerMiddleWare($middleware['class']);
			} catch (Throwable $e) {
				$appId = $middleware['appId'];
				$this->logger->logException($e, [
					'message' => "Error during capability registration of $appId: " . $e->getMessage(),
					'level' => ILogger::ERROR,
				]);
			}
		}
	}

	/**
	 * @return array[]
	 */
	public function getSearchProviders(): array {
		return $this->searchProviders;
	}

	/**
	 * @return array[]
	 */
	public function getAlternativeLogins(): array {
		return $this->alternativeLogins;
	}

	/**
	 * @erturn array[]
	 */
	public function getInitialStates(): array {
		return $this->initialStates;
	}
}