Passed
Push — master ( d090f9...62929c )
by Daniel
14:38 queued 10s
created

Application::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
/**
3
 *
4
 *
5
 * @author Christoph Wurst <[email protected]>
6
 * @author Daniel Kesselberg <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 * @author zulan <[email protected]>
11
 *
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
namespace OCA\Provisioning_API\AppInfo;
30
31
use OC\Group\Manager as GroupManager;
32
use OCA\Provisioning_API\Middleware\ProvisioningApiMiddleware;
33
use OCA\Settings\Mailer\NewUserMailHelper;
34
use OCP\AppFramework\App;
35
use OCP\AppFramework\Bootstrap\IBootContext;
36
use OCP\AppFramework\Bootstrap\IBootstrap;
37
use OCP\AppFramework\Bootstrap\IRegistrationContext;
38
use OCP\AppFramework\Utility\IControllerMethodReflector;
39
use OCP\AppFramework\Utility\ITimeFactory;
40
use OCP\Defaults;
41
use OCP\IConfig;
42
use OCP\IGroupManager;
43
use OCP\IURLGenerator;
44
use OCP\IUser;
45
use OCP\IUserManager;
46
use OCP\L10N\IFactory;
47
use OCP\Mail\IMailer;
48
use OCP\Security\ICrypto;
49
use OCP\Security\ISecureRandom;
50
use OCP\Util;
51
use Psr\Container\ContainerInterface;
52
53
class Application extends App implements IBootstrap {
54
	public function __construct(array $urlParams = []) {
55
		parent::__construct('provisioning_api', $urlParams);
56
	}
57
58
	public function register(IRegistrationContext $context): void {
59
		$context->registerService(NewUserMailHelper::class, function (ContainerInterface $c) {
60
			return new NewUserMailHelper(
61
				$c->get(Defaults::class),
62
				$c->get(IURLGenerator::class),
63
				$c->get(IFactory::class),
64
				$c->get(IMailer::class),
65
				$c->get(ISecureRandom::class),
66
				$c->get(ITimeFactory::class),
67
				$c->get(IConfig::class),
68
				$c->get(ICrypto::class),
69
				Util::getDefaultEmailAddress('no-reply')
70
			);
71
		});
72
		$context->registerService(ProvisioningApiMiddleware::class, function (ContainerInterface $c) {
73
			$user = $c->get(IUserManager::class)->get($c->get('UserId'));
74
			$isAdmin = false;
75
			$isSubAdmin = false;
76
77
			if ($user instanceof IUser) {
78
				$groupManager = $c->get(IGroupManager::class);
79
				assert($groupManager instanceof GroupManager);
80
				$isAdmin = $groupManager->isAdmin($user->getUID());
81
				$isSubAdmin = $groupManager->getSubAdmin()->isSubAdmin($user);
82
			}
83
84
			return new ProvisioningApiMiddleware(
85
				$c->get(IControllerMethodReflector::class),
86
				$isAdmin,
87
				$isSubAdmin
88
			);
89
		});
90
		$context->registerMiddleware(ProvisioningApiMiddleware::class);
91
	}
92
93
	public function boot(IBootContext $context): void {
94
	}
95
}
96