Completed
Push — master ( 1c2ccd...167013 )
by Morris
14:20
created

Application::register()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Björn Schießle <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Georg Ehrke <[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
 * @author Roeland Jago Douma <[email protected]>
13
 * @author Thomas Müller <[email protected]>
14
 *
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
31
namespace OC\Settings;
32
33
use OC\AppFramework\Utility\TimeFactory;
34
use OC\Authentication\Token\IProvider;
35
use OC\Server;
36
use OC\Settings\Activity\Provider;
37
use OC\Settings\Activity\Setting;
38
use OC\Settings\Mailer\NewUserMailHelper;
39
use OC\Settings\Middleware\SubadminMiddleware;
40
use OCP\AppFramework\App;
41
use OCP\Defaults;
42
use OCP\IContainer;
43
use OCP\Settings\IManager;
44
use OCP\Util;
45
46
/**
47
 * @package OC\Settings
48
 */
49
class Application extends App {
50
51
52
	/**
53
	 * @param array $urlParams
54
	 */
55
	public function __construct(array $urlParams=[]){
56
		parent::__construct('settings', $urlParams);
57
58
		$container = $this->getContainer();
59
60
		// Register Middleware
61
		$container->registerAlias('SubadminMiddleware', SubadminMiddleware::class);
62
		$container->registerMiddleWare('SubadminMiddleware');
63
64
		/**
65
		 * Core class wrappers
66
		 */
67
		/** FIXME: Remove once OC_User is non-static and mockable */
68
		$container->registerService('isAdmin', function() {
69
			return \OC_User::isAdminUser(\OC_User::getUser());
70
		});
71
		/** FIXME: Remove once OC_SubAdmin is non-static and mockable */
72
		$container->registerService('isSubAdmin', function(IContainer $c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

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

Loading history...
73
			$userObject = \OC::$server->getUserSession()->getUser();
74
			$isSubAdmin = false;
75
			if($userObject !== null) {
76
				$isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
77
			}
78
			return $isSubAdmin;
79
		});
80
		$container->registerService('userCertificateManager', function(IContainer $c) {
81
			return $c->query('ServerContainer')->getCertificateManager();
82
		}, false);
83
		$container->registerService('systemCertificateManager', function (IContainer $c) {
84
			return $c->query('ServerContainer')->getCertificateManager(null);
85
		}, false);
86
		$container->registerService(IProvider::class, function (IContainer $c) {
87
			return $c->query('ServerContainer')->query(IProvider::class);
88
		});
89
		$container->registerService(IManager::class, function (IContainer $c) {
90
			return $c->query('ServerContainer')->getSettingsManager();
91
		});
92
93
		$container->registerService(NewUserMailHelper::class, function (IContainer $c) {
94
			/** @var Server $server */
95
			$server = $c->query('ServerContainer');
96
			/** @var Defaults $defaults */
97
			$defaults = $server->query(Defaults::class);
98
99
			return new NewUserMailHelper(
100
				$defaults,
101
				$server->getURLGenerator(),
102
				$server->getL10N('settings'),
103
				$server->getMailer(),
104
				$server->getSecureRandom(),
105
				new TimeFactory(),
106
				$server->getConfig(),
107
				$server->getCrypto(),
108
				Util::getDefaultEmailAddress('no-reply')
109
			);
110
		});
111
	}
112
113
	public function register() {
114
		$activityManager = $this->getContainer()->getServer()->getActivityManager();
115
		$activityManager->registerSetting(Setting::class); // FIXME move to info.xml
116
		$activityManager->registerProvider(Provider::class); // FIXME move to info.xml
117
118
		Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword');
119
		Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo');
120
	}
121
122
	/**
123
	 * @param array $parameters
124
	 * @throws \InvalidArgumentException
125
	 * @throws \BadMethodCallException
126
	 * @throws \Exception
127
	 * @throws \OCP\AppFramework\QueryException
128
	 */
129
	public function onChangePassword(array $parameters) {
130
		/** @var Hooks $hooks */
131
		$hooks = $this->getContainer()->query(Hooks::class);
132
		$hooks->onChangePassword($parameters['uid']);
133
	}
134
135
	/**
136
	 * @param array $parameters
137
	 * @throws \InvalidArgumentException
138
	 * @throws \BadMethodCallException
139
	 * @throws \Exception
140
	 * @throws \OCP\AppFramework\QueryException
141
	 */
142
	public function onChangeInfo(array $parameters) {
143
		if ($parameters['feature'] !== 'eMailAddress') {
144
			return;
145
		}
146
147
		/** @var Hooks $hooks */
148
		$hooks = $this->getContainer()->query(Hooks::class);
149
		$hooks->onChangeEmail($parameters['user'], $parameters['old_value']);
150
	}
151
}
152