Passed
Push — master ( a48043...a4b34a )
by Morris
38:55 queued 34s
created

Application   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 81
dl 0
loc 164
rs 10
c 0
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 78 4
A extendJsConfig() 0 9 2
A register() 0 18 1
A onChangeInfo() 0 8 2
A removeUserFromGroup() 0 4 1
A onChangePassword() 0 4 1
A addUserToGroup() 0 4 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Björn Schießle <[email protected]>
7
 * @author Christoph Wurst <[email protected]>
8
 * @author Joas Schilling <[email protected]>
9
 * @author Lukas Reschke <[email protected]>
10
 * @author Morris Jobke <[email protected]>
11
 * @author Robin Appelman <[email protected]>
12
 *
13
 * @license AGPL-3.0
14
 *
15
 * This code is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License, version 3,
17
 * as published by the Free Software Foundation.
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, version 3,
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
26
 *
27
 */
28
29
namespace OC\Settings;
30
31
use BadMethodCallException;
32
use OC\AppFramework\Utility\TimeFactory;
33
use OC\Authentication\Token\IProvider;
34
use OC\Authentication\Token\IToken;
35
use OC\Server;
36
use OC\Settings\Activity\GroupProvider;
37
use OC\Settings\Activity\GroupSetting;
38
use OC\Settings\Activity\Provider;
39
use OC\Settings\Activity\SecurityFilter;
40
use OC\Settings\Activity\SecurityProvider;
41
use OC\Settings\Activity\SecuritySetting;
42
use OC\Settings\Activity\Setting;
43
use OC\Settings\Mailer\NewUserMailHelper;
44
use OC\Settings\Middleware\SubadminMiddleware;
45
use OCP\Activity\IManager as IActivityManager;
46
use OCP\AppFramework\App;
47
use OCP\Defaults;
48
use OCP\IContainer;
49
use OCP\IGroup;
50
use OCP\ILogger;
51
use OCP\IUser;
52
use OCP\Settings\IManager;
53
use OCP\Util;
54
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
55
use Symfony\Component\EventDispatcher\GenericEvent;
56
57
/**
58
 * @package OC\Settings
59
 */
60
class Application extends App {
61
62
63
	/**
64
	 * @param array $urlParams
65
	 */
66
	public function __construct(array $urlParams=[]){
67
		parent::__construct('settings', $urlParams);
68
69
		$container = $this->getContainer();
70
71
		// Register Middleware
72
		$container->registerAlias('SubadminMiddleware', SubadminMiddleware::class);
73
		$container->registerMiddleWare('SubadminMiddleware');
74
75
		/**
76
		 * Core class wrappers
77
		 */
78
		/** FIXME: Remove once OC_User is non-static and mockable */
79
		$container->registerService('isAdmin', function() {
80
			return \OC_User::isAdminUser(\OC_User::getUser());
81
		});
82
		/** FIXME: Remove once OC_SubAdmin is non-static and mockable */
83
		$container->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

83
		$container->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...
84
			$userObject = \OC::$server->getUserSession()->getUser();
85
			$isSubAdmin = false;
86
			if($userObject !== null) {
87
				$isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
88
			}
89
			return $isSubAdmin;
90
		});
91
		$container->registerService('userCertificateManager', function(IContainer $c) {
92
			return $c->query('ServerContainer')->getCertificateManager();
93
		}, false);
94
		$container->registerService('systemCertificateManager', function (IContainer $c) {
95
			return $c->query('ServerContainer')->getCertificateManager(null);
96
		}, false);
97
		$container->registerService(IProvider::class, function (IContainer $c) {
98
			return $c->query('ServerContainer')->query(IProvider::class);
99
		});
100
		$container->registerService(IManager::class, function (IContainer $c) {
101
			return $c->query('ServerContainer')->getSettingsManager();
102
		});
103
104
		$container->registerService(NewUserMailHelper::class, function (IContainer $c) {
105
			/** @var Server $server */
106
			$server = $c->query('ServerContainer');
107
			/** @var Defaults $defaults */
108
			$defaults = $server->query(Defaults::class);
109
110
			return new NewUserMailHelper(
111
				$defaults,
112
				$server->getURLGenerator(),
113
				$server->getL10NFactory(),
114
				$server->getMailer(),
115
				$server->getSecureRandom(),
116
				new TimeFactory(),
117
				$server->getConfig(),
118
				$server->getCrypto(),
119
				Util::getDefaultEmailAddress('no-reply')
120
			);
121
		});
122
123
		/** @var EventDispatcherInterface $eventDispatcher */
124
		$eventDispatcher = $container->getServer()->getEventDispatcher();
125
		$eventDispatcher->addListener('app_password_created', function (GenericEvent $event) use ($container) {
126
			if (($token = $event->getSubject()) instanceof IToken) {
127
				/** @var IActivityManager $activityManager */
128
				$activityManager = $container->query(IActivityManager::class);
129
				/** @var ILogger $logger */
130
				$logger = $container->query(ILogger::class);
131
132
				$activity = $activityManager->generateEvent();
133
				$activity->setApp('settings')
134
					->setType('security')
135
					->setAffectedUser($token->getUID())
136
					->setAuthor($token->getUID())
137
					->setSubject(Provider::APP_TOKEN_CREATED, ['name' => $token->getName()])
138
					->setObject('app_token', $token->getId());
139
140
				try {
141
					$activityManager->publish($activity);
142
				} catch (BadMethodCallException $e) {
143
					$logger->logException($e, ['message' => 'could not publish activity', 'level' => ILogger::WARN]);
144
				}
145
			}
146
		});
147
	}
148
149
	public function register() {
150
		$activityManager = $this->getContainer()->getServer()->getActivityManager();
151
		$activityManager->registerSetting(Setting::class); // FIXME move to info.xml
152
		$activityManager->registerProvider(Provider::class); // FIXME move to info.xml
153
		$activityManager->registerFilter(SecurityFilter::class); // FIXME move to info.xml
154
		$activityManager->registerSetting(SecuritySetting::class); // FIXME move to info.xml
155
		$activityManager->registerProvider(SecurityProvider::class); // FIXME move to info.xml
156
		$activityManager->registerSetting(GroupSetting::class); // FIXME move to info.xml
157
		$activityManager->registerProvider(GroupProvider::class); // FIXME move to info.xml
158
159
		Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword');
160
		Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo');
161
162
		$groupManager = $this->getContainer()->getServer()->getGroupManager();
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
		Util::connectHook('\OCP\Config', 'js', $this, 'extendJsConfig');
167
	}
168
169
	public function addUserToGroup(IGroup $group, IUser $user): void {
170
		/** @var Hooks $hooks */
171
		$hooks = $this->getContainer()->query(Hooks::class);
172
		$hooks->addUserToGroup($group, $user);
173
		
174
	}
175
176
	public function removeUserFromGroup(IGroup $group, IUser $user): void {
177
		/** @var Hooks $hooks */
178
		$hooks = $this->getContainer()->query(Hooks::class);
179
		$hooks->removeUserFromGroup($group, $user);
180
	}
181
182
	/**
183
	 * @param array $parameters
184
	 * @throws \InvalidArgumentException
185
	 * @throws \BadMethodCallException
186
	 * @throws \Exception
187
	 * @throws \OCP\AppFramework\QueryException
188
	 */
189
	public function onChangePassword(array $parameters) {
190
		/** @var Hooks $hooks */
191
		$hooks = $this->getContainer()->query(Hooks::class);
192
		$hooks->onChangePassword($parameters['uid']);
193
	}
194
195
	/**
196
	 * @param array $parameters
197
	 * @throws \InvalidArgumentException
198
	 * @throws \BadMethodCallException
199
	 * @throws \Exception
200
	 * @throws \OCP\AppFramework\QueryException
201
	 */
202
	public function onChangeInfo(array $parameters) {
203
		if ($parameters['feature'] !== 'eMailAddress') {
204
			return;
205
		}
206
207
		/** @var Hooks $hooks */
208
		$hooks = $this->getContainer()->query(Hooks::class);
209
		$hooks->onChangeEmail($parameters['user'], $parameters['old_value']);
210
	}
211
212
	/**
213
	 * @param array $settings
214
	 */
215
	public function extendJsConfig(array $settings) {
216
		$appConfig = json_decode($settings['array']['oc_appconfig'], true);
217
218
		$publicWebFinger = \OC::$server->getConfig()->getAppValue('core', 'public_webfinger', '');
219
		if (!empty($publicWebFinger)) {
220
			$appConfig['core']['public_webfinger'] = $publicWebFinger;
221
		}
222
223
		$settings['array']['oc_appconfig'] = json_encode($appConfig);
224
	}
225
}
226