Passed
Push — master ( e47bd8...f92d21 )
by Joas
14:59 queued 11s
created

Application::addUserToGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
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 Morris Jobke <[email protected]>
16
 * @author Robin Appelman <[email protected]>
17
 * @author Roeland Jago Douma <[email protected]>
18
 * @author zulan <[email protected]>
19
 *
20
 * @license AGPL-3.0
21
 *
22
 * This code is free software: you can redistribute it and/or modify
23
 * it under the terms of the GNU Affero General Public License, version 3,
24
 * as published by the Free Software Foundation.
25
 *
26
 * This program is distributed in the hope that it will be useful,
27
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29
 * GNU Affero General Public License for more details.
30
 *
31
 * You should have received a copy of the GNU Affero General Public License, version 3,
32
 * along with this program. If not, see <http://www.gnu.org/licenses/>
33
 *
34
 */
35
36
namespace OCA\Settings\AppInfo;
37
38
use OC\AppFramework\Utility\TimeFactory;
39
use OC\Authentication\Events\AppPasswordCreatedEvent;
40
use OC\Authentication\Token\IProvider;
41
use OC\Server;
42
use OCA\Settings\Hooks;
43
use OCA\Settings\Listener\AppPasswordCreatedActivityListener;
44
use OCA\Settings\Listener\UserAddedToGroupActivityListener;
45
use OCA\Settings\Listener\UserRemovedFromGroupActivityListener;
46
use OCA\Settings\Mailer\NewUserMailHelper;
47
use OCA\Settings\Middleware\SubadminMiddleware;
48
use OCA\Settings\Search\AppSearch;
49
use OCA\Settings\Search\SectionSearch;
50
use OCP\AppFramework\App;
51
use OCP\AppFramework\Bootstrap\IBootContext;
52
use OCP\AppFramework\Bootstrap\IBootstrap;
53
use OCP\AppFramework\Bootstrap\IRegistrationContext;
54
use OCP\AppFramework\IAppContainer;
55
use OCP\Defaults;
56
use OCP\Group\Events\UserAddedEvent;
57
use OCP\Group\Events\UserRemovedEvent;
58
use OCP\IServerContainer;
59
use OCP\Settings\IManager;
60
use OCP\Util;
61
62
class Application extends App implements IBootstrap {
63
	public const APP_ID = 'settings';
64
65
	/**
66
	 * @param array $urlParams
67
	 */
68
	public function __construct(array $urlParams = []) {
69
		parent::__construct(self::APP_ID, $urlParams);
70
	}
71
72
	public function register(IRegistrationContext $context): void {
73
		// Register Middleware
74
		$context->registerServiceAlias('SubadminMiddleware', SubadminMiddleware::class);
75
		$context->registerMiddleware(SubadminMiddleware::class);
76
		$context->registerSearchProvider(SectionSearch::class);
77
		$context->registerSearchProvider(AppSearch::class);
78
79
		// Register listeners
80
		$context->registerEventListener(AppPasswordCreatedEvent::class, AppPasswordCreatedActivityListener::class);
81
		$context->registerEventListener(UserAddedEvent::class, UserAddedToGroupActivityListener::class);
82
		$context->registerEventListener(UserRemovedEvent::class, UserRemovedFromGroupActivityListener::class);
83
84
		/**
85
		 * Core class wrappers
86
		 */
87
		/** FIXME: Remove once OC_User is non-static and mockable */
88
		$context->registerService('isAdmin', function () {
89
			return \OC_User::isAdminUser(\OC_User::getUser());
90
		});
91
		/** FIXME: Remove once OC_SubAdmin is non-static and mockable */
92
		$context->registerService('isSubAdmin', function () {
93
			$userObject = \OC::$server->getUserSession()->getUser();
94
			$isSubAdmin = false;
95
			if ($userObject !== null) {
96
				$isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
97
			}
98
			return $isSubAdmin;
99
		});
100
		$context->registerService(IProvider::class, function (IAppContainer $appContainer) {
101
			/** @var IServerContainer $serverContainer */
102
			$serverContainer = $appContainer->query(IServerContainer::class);
103
			return $serverContainer->query(IProvider::class);
104
		});
105
		$context->registerService(IManager::class, function (IAppContainer $appContainer) {
106
			/** @var IServerContainer $serverContainer */
107
			$serverContainer = $appContainer->query(IServerContainer::class);
108
			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

108
			return $serverContainer->/** @scrutinizer ignore-call */ getSettingsManager();
Loading history...
109
		});
110
111
		$context->registerService(NewUserMailHelper::class, function (IAppContainer $appContainer) {
112
			/** @var Server $server */
113
			$server = $appContainer->query(IServerContainer::class);
114
			/** @var Defaults $defaults */
115
			$defaults = $server->query(Defaults::class);
116
117
			return new NewUserMailHelper(
118
				$defaults,
119
				$server->getURLGenerator(),
120
				$server->getL10NFactory(),
121
				$server->getMailer(),
122
				$server->getSecureRandom(),
123
				new TimeFactory(),
124
				$server->getConfig(),
125
				$server->getCrypto(),
126
				Util::getDefaultEmailAddress('no-reply')
127
			);
128
		});
129
	}
130
131
	public function boot(IBootContext $context): void {
132
		Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword');
133
		Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo');
134
	}
135
136
	/**
137
	 * @param array $parameters
138
	 * @throws \InvalidArgumentException
139
	 * @throws \BadMethodCallException
140
	 * @throws \Exception
141
	 * @throws \OCP\AppFramework\QueryException
142
	 */
143
	public function onChangePassword(array $parameters) {
144
		/** @var Hooks $hooks */
145
		$hooks = $this->getContainer()->query(Hooks::class);
146
		$hooks->onChangePassword($parameters['uid']);
147
	}
148
149
	/**
150
	 * @param array $parameters
151
	 * @throws \InvalidArgumentException
152
	 * @throws \BadMethodCallException
153
	 * @throws \Exception
154
	 * @throws \OCP\AppFramework\QueryException
155
	 */
156
	public function onChangeInfo(array $parameters) {
157
		if ($parameters['feature'] !== 'eMailAddress') {
158
			return;
159
		}
160
161
		/** @var Hooks $hooks */
162
		$hooks = $this->getContainer()->query(Hooks::class);
163
		$hooks->onChangeEmail($parameters['user'], $parameters['old_value']);
164
	}
165
}
166