Passed
Push — master ( d1b03f...c1183f )
by Christoph
11:01 queued 11s
created

Application   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 77
c 0
b 0
f 0
dl 0
loc 161
rs 10
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A onChangePassword() 0 4 1
A boot() 0 35 3
A onChangeInfo() 0 8 2
A __construct() 0 2 1
A register() 0 50 2
A extendJsConfig() 0 14 3
A addUserToGroup() 0 4 1
A removeUserFromGroup() 0 4 1
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\Server;
44
use OCA\Settings\Activity\Provider;
45
use OCA\Settings\Hooks;
46
use OCA\Settings\Mailer\NewUserMailHelper;
47
use OCA\Settings\Middleware\SubadminMiddleware;
48
use OCP\Activity\IManager as IActivityManager;
49
use OCP\AppFramework\App;
50
use OCP\AppFramework\Bootstrap\IBootContext;
51
use OCP\AppFramework\Bootstrap\IBootstrap;
52
use OCP\AppFramework\Bootstrap\IRegistrationContext;
53
use OCP\Defaults;
54
use OCP\IContainer;
55
use OCP\IGroup;
56
use OCP\ILogger;
57
use OCP\IUser;
58
use OCP\Settings\IManager;
59
use OCP\Util;
60
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
61
use Symfony\Component\EventDispatcher\GenericEvent;
62
63
class Application extends App implements IBootstrap {
64
	public const APP_ID = 'settings';
65
66
	/**
67
	 * @param array $urlParams
68
	 */
69
	public function __construct(array $urlParams=[]) {
70
		parent::__construct(self::APP_ID, $urlParams);
71
	}
72
73
	public function register(IRegistrationContext $context): void {
74
		// Register Middleware
75
		$context->registerServiceAlias('SubadminMiddleware', SubadminMiddleware::class);
76
		$context->registerMiddleware(SubadminMiddleware::class);
77
78
		/**
79
		 * Core class wrappers
80
		 */
81
		/** FIXME: Remove once OC_User is non-static and mockable */
82
		$context->registerService('isAdmin', function () {
83
			return \OC_User::isAdminUser(\OC_User::getUser());
84
		});
85
		/** FIXME: Remove once OC_SubAdmin is non-static and mockable */
86
		$context->registerService('isSubAdmin', function (IContainer $c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed. ( Ignorable by Annotation )

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

86
		$context->registerService('isSubAdmin', function (/** @scrutinizer ignore-unused */ IContainer $c) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
			$userObject = \OC::$server->getUserSession()->getUser();
88
			$isSubAdmin = false;
89
			if ($userObject !== null) {
90
				$isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
91
			}
92
			return $isSubAdmin;
93
		});
94
		$context->registerService('userCertificateManager', function (IContainer $c) {
95
			return $c->query('ServerContainer')->getCertificateManager();
96
		}, false);
97
		$context->registerService('systemCertificateManager', function (IContainer $c) {
98
			return $c->query('ServerContainer')->getCertificateManager(null);
99
		}, false);
100
		$context->registerService(IProvider::class, function (IContainer $c) {
101
			return $c->query('ServerContainer')->query(IProvider::class);
102
		});
103
		$context->registerService(IManager::class, function (IContainer $c) {
104
			return $c->query('ServerContainer')->getSettingsManager();
105
		});
106
107
		$context->registerService(NewUserMailHelper::class, function (IContainer $c) {
108
			/** @var Server $server */
109
			$server = $c->query('ServerContainer');
110
			/** @var Defaults $defaults */
111
			$defaults = $server->query(Defaults::class);
112
113
			return new NewUserMailHelper(
114
				$defaults,
115
				$server->getURLGenerator(),
116
				$server->getL10NFactory(),
117
				$server->getMailer(),
118
				$server->getSecureRandom(),
119
				new TimeFactory(),
120
				$server->getConfig(),
121
				$server->getCrypto(),
122
				Util::getDefaultEmailAddress('no-reply')
123
			);
124
		});
125
	}
126
127
	public function boot(IBootContext $context): void {
128
		/** @var EventDispatcherInterface $eventDispatcher */
129
		$eventDispatcher = $context->getServerContainer()->getEventDispatcher();
0 ignored issues
show
Deprecated Code introduced by
The function OCP\IServerContainer::getEventDispatcher() has been deprecated: 20.0.0 use \OCP\EventDispatcher\IEventDispatcher ( Ignorable by Annotation )

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

129
		$eventDispatcher = /** @scrutinizer ignore-deprecated */ $context->getServerContainer()->getEventDispatcher();

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

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

Loading history...
130
		$container = $context->getAppContainer();
131
		$eventDispatcher->addListener('app_password_created', function (GenericEvent $event) use ($container) {
132
			if (($token = $event->getSubject()) instanceof IToken) {
133
				/** @var IActivityManager $activityManager */
134
				$activityManager = $container->query(IActivityManager::class);
135
				/** @var ILogger $logger */
136
				$logger = $container->query(ILogger::class);
137
138
				$activity = $activityManager->generateEvent();
139
				$activity->setApp('settings')
140
					->setType('security')
141
					->setAffectedUser($token->getUID())
142
					->setAuthor($token->getUID())
143
					->setSubject(Provider::APP_TOKEN_CREATED, ['name' => $token->getName()])
144
					->setObject('app_token', $token->getId());
145
146
				try {
147
					$activityManager->publish($activity);
148
				} catch (BadMethodCallException $e) {
149
					$logger->logException($e, ['message' => 'could not publish activity', 'level' => ILogger::WARN]);
150
				}
151
			}
152
		});
153
154
		Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword');
155
		Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo');
156
157
		$groupManager = $context->getServerContainer()->getGroupManager();
158
		$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

158
		$groupManager->/** @scrutinizer ignore-call */ 
159
                 listen('\OC\Group', 'postRemoveUser',  [$this, 'removeUserFromGroup']);
Loading history...
159
		$groupManager->listen('\OC\Group', 'postAddUser',  [$this, 'addUserToGroup']);
160
161
		Util::connectHook('\OCP\Config', 'js', $this, 'extendJsConfig');
162
	}
163
164
	public function addUserToGroup(IGroup $group, IUser $user): void {
165
		/** @var Hooks $hooks */
166
		$hooks = $this->getContainer()->query(Hooks::class);
167
		$hooks->addUserToGroup($group, $user);
168
	}
169
170
	public function removeUserFromGroup(IGroup $group, IUser $user): void {
171
		/** @var Hooks $hooks */
172
		$hooks = $this->getContainer()->query(Hooks::class);
173
		$hooks->removeUserFromGroup($group, $user);
174
	}
175
176
177
	/**
178
	 * @param array $parameters
179
	 * @throws \InvalidArgumentException
180
	 * @throws \BadMethodCallException
181
	 * @throws \Exception
182
	 * @throws \OCP\AppFramework\QueryException
183
	 */
184
	public function onChangePassword(array $parameters) {
185
		/** @var Hooks $hooks */
186
		$hooks = $this->getContainer()->query(Hooks::class);
187
		$hooks->onChangePassword($parameters['uid']);
188
	}
189
190
	/**
191
	 * @param array $parameters
192
	 * @throws \InvalidArgumentException
193
	 * @throws \BadMethodCallException
194
	 * @throws \Exception
195
	 * @throws \OCP\AppFramework\QueryException
196
	 */
197
	public function onChangeInfo(array $parameters) {
198
		if ($parameters['feature'] !== 'eMailAddress') {
199
			return;
200
		}
201
202
		/** @var Hooks $hooks */
203
		$hooks = $this->getContainer()->query(Hooks::class);
204
		$hooks->onChangeEmail($parameters['user'], $parameters['old_value']);
205
	}
206
207
	/**
208
	 * @param array $settings
209
	 */
210
	public function extendJsConfig(array $settings) {
211
		$appConfig = json_decode($settings['array']['oc_appconfig'], true);
212
213
		$publicWebFinger = \OC::$server->getConfig()->getAppValue('core', 'public_webfinger', '');
214
		if (!empty($publicWebFinger)) {
215
			$appConfig['core']['public_webfinger'] = $publicWebFinger;
216
		}
217
218
		$publicNodeInfo = \OC::$server->getConfig()->getAppValue('core', 'public_nodeinfo', '');
219
		if (!empty($publicNodeInfo)) {
220
			$appConfig['core']['public_nodeinfo'] = $publicNodeInfo;
221
		}
222
223
		$settings['array']['oc_appconfig'] = json_encode($appConfig);
224
	}
225
}
226