Passed
Push — master ( fbf25e...f37e15 )
by Christoph
18:24 queued 11s
created

Application::extendJsConfig()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 1
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2016, ownCloud, Inc.
7
 *
8
 * @author Arthur Schiwon <[email protected]>
9
 * @author Björn Schießle <[email protected]>
10
 * @author Christoph Wurst <[email protected]>
11
 * @author Daniel Calviño Sánchez <[email protected]>
12
 * @author Daniel Kesselberg <[email protected]>
13
 * @author Joas Schilling <[email protected]>
14
 * @author Lukas Reschke <[email protected]>
15
 * @author Maxence Lange <[email protected]>
16
 * @author Morris Jobke <[email protected]>
17
 * @author Robin Appelman <[email protected]>
18
 * @author Roeland Jago Douma <[email protected]>
19
 * @author zulan <[email protected]>
20
 *
21
 * @license AGPL-3.0
22
 *
23
 * This code is free software: you can redistribute it and/or modify
24
 * it under the terms of the GNU Affero General Public License, version 3,
25
 * as published by the Free Software Foundation.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30
 * GNU Affero General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU Affero General Public License, version 3,
33
 * along with this program. If not, see <http://www.gnu.org/licenses/>
34
 *
35
 */
36
37
namespace OCA\Settings\AppInfo;
38
39
use BadMethodCallException;
40
use OC\AppFramework\Utility\TimeFactory;
41
use OC\Authentication\Token\IProvider;
42
use OC\Authentication\Token\IToken;
43
use OC\Group\Manager;
44
use OC\Server;
45
use OCA\Settings\Activity\Provider;
46
use OCA\Settings\Hooks;
47
use OCA\Settings\Mailer\NewUserMailHelper;
48
use OCA\Settings\Middleware\SubadminMiddleware;
49
use OCA\Settings\Search\AppSearch;
50
use OCA\Settings\Search\SectionSearch;
51
use OCP\Activity\IManager as IActivityManager;
52
use OCP\AppFramework\App;
53
use OCP\AppFramework\Bootstrap\IBootContext;
54
use OCP\AppFramework\Bootstrap\IBootstrap;
55
use OCP\AppFramework\Bootstrap\IRegistrationContext;
56
use OCP\AppFramework\IAppContainer;
57
use OCP\Defaults;
58
use OCP\IGroup;
59
use OCP\IGroupManager;
60
use OCP\ILogger;
61
use OCP\IServerContainer;
62
use OCP\IUser;
63
use OCP\Settings\IManager;
64
use OCP\Util;
65
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
66
use Symfony\Component\EventDispatcher\GenericEvent;
67
68
class Application extends App implements IBootstrap {
69
	public const APP_ID = 'settings';
70
71
	/**
72
	 * @param array $urlParams
73
	 */
74
	public function __construct(array $urlParams = []) {
75
		parent::__construct(self::APP_ID, $urlParams);
76
	}
77
78
	public function register(IRegistrationContext $context): void {
79
		// Register Middleware
80
		$context->registerServiceAlias('SubadminMiddleware', SubadminMiddleware::class);
81
		$context->registerMiddleware(SubadminMiddleware::class);
82
		$context->registerSearchProvider(SectionSearch::class);
83
		$context->registerSearchProvider(AppSearch::class);
84
85
		/**
86
		 * Core class wrappers
87
		 */
88
		/** FIXME: Remove once OC_User is non-static and mockable */
89
		$context->registerService('isAdmin', function () {
90
			return \OC_User::isAdminUser(\OC_User::getUser());
91
		});
92
		/** FIXME: Remove once OC_SubAdmin is non-static and mockable */
93
		$context->registerService('isSubAdmin', function () {
94
			$userObject = \OC::$server->getUserSession()->getUser();
95
			$isSubAdmin = false;
96
			if ($userObject !== null) {
97
				$isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
98
			}
99
			return $isSubAdmin;
100
		});
101
		$context->registerService(IProvider::class, function (IAppContainer $appContainer) {
102
			/** @var IServerContainer $serverContainer */
103
			$serverContainer = $appContainer->query(IServerContainer::class);
104
			return $serverContainer->query(IProvider::class);
105
		});
106
		$context->registerService(IManager::class, function (IAppContainer $appContainer) {
107
			/** @var IServerContainer $serverContainer */
108
			$serverContainer = $appContainer->query(IServerContainer::class);
109
			return $serverContainer->getSettingsManager();
0 ignored issues
show
Bug introduced by
The method getSettingsManager() does not exist on OCP\IServerContainer. Since it exists in all sub-types, consider adding an abstract or default implementation to OCP\IServerContainer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
			return $serverContainer->/** @scrutinizer ignore-call */ getSettingsManager();
Loading history...
110
		});
111
112
		$context->registerService(NewUserMailHelper::class, function (IAppContainer $appContainer) {
113
			/** @var Server $server */
114
			$server = $appContainer->query(IServerContainer::class);
115
			/** @var Defaults $defaults */
116
			$defaults = $server->query(Defaults::class);
117
118
			return new NewUserMailHelper(
119
				$defaults,
120
				$server->getURLGenerator(),
121
				$server->getL10NFactory(),
122
				$server->getMailer(),
123
				$server->getSecureRandom(),
124
				new TimeFactory(),
125
				$server->getConfig(),
126
				$server->getCrypto(),
127
				Util::getDefaultEmailAddress('no-reply')
128
			);
129
		});
130
	}
131
132
	public function boot(IBootContext $context): void {
133
		$context->injectFn(function (EventDispatcherInterface $dispatcher, IAppContainer $appContainer) {
134
			$dispatcher->addListener('app_password_created', function (GenericEvent $event) use ($appContainer) {
135
				if (($token = $event->getSubject()) instanceof IToken) {
136
					/** @var IActivityManager $activityManager */
137
					$activityManager = $appContainer->get(IActivityManager::class);
138
					/** @var ILogger $logger */
139
					$logger = $appContainer->get(ILogger::class);
140
141
					$activity = $activityManager->generateEvent();
142
					$activity->setApp('settings')
143
						->setType('security')
144
						->setAffectedUser($token->getUID())
145
						->setAuthor($token->getUID())
146
						->setSubject(Provider::APP_TOKEN_CREATED, ['name' => $token->getName()])
147
						->setObject('app_token', $token->getId());
148
149
					try {
150
						$activityManager->publish($activity);
151
					} catch (BadMethodCallException $e) {
152
						$logger->logException($e, ['message' => 'could not publish activity', 'level' => ILogger::WARN]);
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::WARN has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

152
						$logger->logException($e, ['message' => 'could not publish activity', 'level' => /** @scrutinizer ignore-deprecated */ ILogger::WARN]);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
153
					}
154
				}
155
			});
156
		});
157
158
		Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword');
159
		Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo');
160
161
		$context->injectFn(function (IGroupManager $groupManager) {
162
			/** @var IGroupManager|Manager $groupManager */
163
			$groupManager->listen('\OC\Group', 'postRemoveUser',  [$this, 'removeUserFromGroup']);
0 ignored issues
show
Bug introduced by
The method listen() does not exist on OCP\IGroupManager. Since it exists in all sub-types, consider adding an abstract or default implementation to OCP\IGroupManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

163
			$groupManager->/** @scrutinizer ignore-call */ 
164
                  listen('\OC\Group', 'postRemoveUser',  [$this, 'removeUserFromGroup']);
Loading history...
164
			$groupManager->listen('\OC\Group', 'postAddUser',  [$this, 'addUserToGroup']);
165
		});
166
	}
167
168
	public function addUserToGroup(IGroup $group, IUser $user): void {
169
		/** @var Hooks $hooks */
170
		$hooks = $this->getContainer()->query(Hooks::class);
171
		$hooks->addUserToGroup($group, $user);
172
	}
173
174
	public function removeUserFromGroup(IGroup $group, IUser $user): void {
175
		/** @var Hooks $hooks */
176
		$hooks = $this->getContainer()->query(Hooks::class);
177
		$hooks->removeUserFromGroup($group, $user);
178
	}
179
180
181
	/**
182
	 * @param array $parameters
183
	 * @throws \InvalidArgumentException
184
	 * @throws \BadMethodCallException
185
	 * @throws \Exception
186
	 * @throws \OCP\AppFramework\QueryException
187
	 */
188
	public function onChangePassword(array $parameters) {
189
		/** @var Hooks $hooks */
190
		$hooks = $this->getContainer()->query(Hooks::class);
191
		$hooks->onChangePassword($parameters['uid']);
192
	}
193
194
	/**
195
	 * @param array $parameters
196
	 * @throws \InvalidArgumentException
197
	 * @throws \BadMethodCallException
198
	 * @throws \Exception
199
	 * @throws \OCP\AppFramework\QueryException
200
	 */
201
	public function onChangeInfo(array $parameters) {
202
		if ($parameters['feature'] !== 'eMailAddress') {
203
			return;
204
		}
205
206
		/** @var Hooks $hooks */
207
		$hooks = $this->getContainer()->query(Hooks::class);
208
		$hooks->onChangeEmail($parameters['user'], $parameters['old_value']);
209
	}
210
}
211