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

Application::initLogger()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Application::register() 0 1 1
A Application::__construct() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
7
 *
8
 * @author Arthur Schiwon <[email protected]>
9
 * @author Bjoern Schiessle <[email protected]>
10
 * @author Christoph Wurst <[email protected]>
11
 * @author GrayFix <[email protected]>
12
 * @author Joas Schilling <[email protected]>
13
 * @author Morris Jobke <[email protected]>
14
 * @author Roeland Jago Douma <[email protected]>
15
 * @author Tiago Flores <[email protected]>
16
 *
17
 * @license GNU AGPL version 3 or any later version
18
 *
19
 * This program is free software: you can redistribute it and/or modify
20
 * it under the terms of the GNU Affero General Public License as
21
 * published by the Free Software Foundation, either version 3 of the
22
 * License, or (at your option) any later version.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 * GNU Affero General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU Affero General Public License
30
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31
 *
32
 */
33
34
namespace OCA\AdminAudit\AppInfo;
35
36
use OC\Files\Filesystem;
37
use OC\Files\Node\File;
38
use OC\Group\Manager;
39
use OC\User\Session;
40
use OCA\AdminAudit\Actions\AppManagement;
41
use OCA\AdminAudit\Actions\Auth;
42
use OCA\AdminAudit\Actions\Console;
43
use OCA\AdminAudit\Actions\Files;
44
use OCA\AdminAudit\Actions\GroupManagement;
45
use OCA\AdminAudit\Actions\Security;
46
use OCA\AdminAudit\Actions\Sharing;
47
use OCA\AdminAudit\Actions\Trashbin;
48
use OCA\AdminAudit\Actions\UserManagement;
49
use OCA\AdminAudit\Actions\Versions;
50
use OCP\App\ManagerEvent;
51
use OCP\AppFramework\App;
52
use OCP\AppFramework\Bootstrap\IBootContext;
53
use OCP\AppFramework\Bootstrap\IBootstrap;
54
use OCP\AppFramework\Bootstrap\IRegistrationContext;
55
use OCP\Authentication\TwoFactorAuth\IProvider;
56
use OCP\Console\ConsoleEvent;
57
use OCP\IConfig;
58
use OCP\IGroupManager;
59
use OCP\ILogger;
60
use OCP\IPreview;
61
use OCP\IServerContainer;
62
use OCP\IUserSession;
63
use OCP\Log\ILogFactory;
64
use OCP\Share;
65
use OCP\Util;
66
use Symfony\Component\EventDispatcher\GenericEvent;
67
68
class Application extends App implements IBootstrap {
69
70
	/** @var ILogger */
71
	protected $logger;
72
73
	public function __construct() {
74
		parent::__construct('admin_audit');
75
	}
76
77
	public function register(IRegistrationContext $context): void {
78
	}
79
80
	public function boot(IBootContext $context): void {
81
		$logger = $this->getLogger(
82
			$context->getAppContainer()->query(IConfig::class),
83
			$context->getAppContainer()->query(ILogger::class),
84
			$context->getAppContainer()->query(ILogFactory::class)
85
		);
86
87
		/*
88
		 * TODO: once the hooks are migrated to lazy events, this should be done
89
		 *       in \OCA\AdminAudit\AppInfo\Application::register
90
		 */
91
		$this->registerHooks($logger, $context->getServerContainer());
92
	}
93
94
	private function getLogger(IConfig $config,
95
							   ILogger $logger,
96
							   ILogFactory $logFactory): ILogger {
97
		$default = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log';
98
		$logFile = $config->getAppValue('admin_audit', 'logfile', $default);
99
100
		if ($logFile === null) {
0 ignored issues
show
introduced by
The condition $logFile === null is always false.
Loading history...
101
			return $logger;
102
		}
103
		return $logFactory->getCustomLogger($logFile);
104
	}
105
106
	/**
107
	 * Register hooks in order to log them
108
	 */
109
	private function registerHooks(ILogger $logger,
110
									 IServerContainer $serverContainer) {
111
		$this->userManagementHooks($logger, $serverContainer);
112
		$this->groupHooks($logger, $serverContainer);
113
		$this->authHooks($logger);
114
115
		$this->consoleHooks($logger, $serverContainer);
116
		$this->appHooks($logger, $serverContainer);
117
118
		$this->sharingHooks($logger);
119
120
		$this->fileHooks($logger, $serverContainer);
121
		$this->trashbinHooks($logger);
122
		$this->versionsHooks($logger);
123
124
		$this->securityHooks($logger, $serverContainer);
125
	}
126
127
	private function userManagementHooks(ILogger $logger,
128
										   IServerContainer $serverContainer) {
129
		$userActions = new UserManagement($logger);
130
131
		Util::connectHook('OC_User', 'post_createUser', $userActions, 'create');
132
		Util::connectHook('OC_User', 'post_deleteUser', $userActions, 'delete');
133
		Util::connectHook('OC_User', 'changeUser', $userActions, 'change');
134
135
		/** @var IUserSession|Session $userSession */
136
		$userSession = $serverContainer->getUserSession();
137
		$userSession->listen('\OC\User', 'postSetPassword', [$userActions, 'setPassword']);
0 ignored issues
show
Bug introduced by
The method listen() does not exist on OCP\IUserSession. It seems like you code against a sub-type of OCP\IUserSession such as OC\User\Session. ( Ignorable by Annotation )

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

137
		$userSession->/** @scrutinizer ignore-call */ 
138
                listen('\OC\User', 'postSetPassword', [$userActions, 'setPassword']);
Loading history...
138
		$userSession->listen('\OC\User', 'assignedUserId', [$userActions, 'assign']);
139
		$userSession->listen('\OC\User', 'postUnassignedUserId', [$userActions, 'unassign']);
140
	}
141
142
	private function groupHooks(ILogger $logger,
143
								  IServerContainer $serverContainer) {
144
		$groupActions = new GroupManagement($logger);
145
146
		/** @var IGroupManager|Manager $groupManager */
147
		$groupManager = $serverContainer->getGroupManager();
148
		$groupManager->listen('\OC\Group', 'postRemoveUser', [$groupActions, 'removeUser']);
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

148
		$groupManager->/** @scrutinizer ignore-call */ 
149
                 listen('\OC\Group', 'postRemoveUser', [$groupActions, 'removeUser']);
Loading history...
149
		$groupManager->listen('\OC\Group', 'postAddUser', [$groupActions, 'addUser']);
150
		$groupManager->listen('\OC\Group', 'postDelete', [$groupActions, 'deleteGroup']);
151
		$groupManager->listen('\OC\Group', 'postCreate', [$groupActions, 'createGroup']);
152
	}
153
154
	private function sharingHooks(ILogger $logger) {
155
		$shareActions = new Sharing($logger);
156
157
		Util::connectHook(Share::class, 'post_shared', $shareActions, 'shared');
158
		Util::connectHook(Share::class, 'post_unshare', $shareActions, 'unshare');
159
		Util::connectHook(Share::class, 'post_unshareFromSelf', $shareActions, 'unshare');
160
		Util::connectHook(Share::class, 'post_update_permissions', $shareActions, 'updatePermissions');
161
		Util::connectHook(Share::class, 'post_update_password', $shareActions, 'updatePassword');
162
		Util::connectHook(Share::class, 'post_set_expiration_date', $shareActions, 'updateExpirationDate');
163
		Util::connectHook(Share::class, 'share_link_access', $shareActions, 'shareAccessed');
164
	}
165
166
	private function authHooks(ILogger $logger) {
167
		$authActions = new Auth($logger);
168
169
		Util::connectHook('OC_User', 'pre_login', $authActions, 'loginAttempt');
170
		Util::connectHook('OC_User', 'post_login', $authActions, 'loginSuccessful');
171
		Util::connectHook('OC_User', 'logout', $authActions, 'logout');
172
	}
173
174
	private function appHooks(ILogger $logger,
175
								IServerContainer $serverContainer) {
176
		$eventDispatcher = $serverContainer->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

176
		$eventDispatcher = /** @scrutinizer ignore-deprecated */ $serverContainer->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...
177
		$eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function (ManagerEvent $event) use ($logger) {
178
			$appActions = new AppManagement($logger);
179
			$appActions->enableApp($event->getAppID());
180
		});
181
		$eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, function (ManagerEvent $event) use ($logger) {
182
			$appActions = new AppManagement($logger);
183
			$appActions->enableAppForGroups($event->getAppID(), $event->getGroups());
184
		});
185
		$eventDispatcher->addListener(ManagerEvent::EVENT_APP_DISABLE, function (ManagerEvent $event) use ($logger) {
186
			$appActions = new AppManagement($logger);
187
			$appActions->disableApp($event->getAppID());
188
		});
189
	}
190
191
	private function consoleHooks(ILogger $logger,
192
									IServerContainer $serverContainer) {
193
		$eventDispatcher = $serverContainer->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

193
		$eventDispatcher = /** @scrutinizer ignore-deprecated */ $serverContainer->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...
194
		$eventDispatcher->addListener(ConsoleEvent::EVENT_RUN, function (ConsoleEvent $event) use ($logger) {
195
			$appActions = new Console($logger);
196
			$appActions->runCommand($event->getArguments());
197
		});
198
	}
199
200
	private function fileHooks(ILogger $logger,
201
								 IServerContainer $serverContainer) {
202
		$fileActions = new Files($logger);
203
		$eventDispatcher = $serverContainer->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

203
		$eventDispatcher = /** @scrutinizer ignore-deprecated */ $serverContainer->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...
204
		$eventDispatcher->addListener(
205
			IPreview::EVENT,
206
			function (GenericEvent $event) use ($fileActions) {
207
				/** @var File $file */
208
				$file = $event->getSubject();
209
				$fileActions->preview([
210
					'path' => mb_substr($file->getInternalPath(), 5),
211
					'width' => $event->getArguments()['width'],
212
					'height' => $event->getArguments()['height'],
213
					'crop' => $event->getArguments()['crop'],
214
					'mode' => $event->getArguments()['mode']
215
				]);
216
			}
217
		);
218
219
		Util::connectHook(
220
			Filesystem::CLASSNAME,
221
			Filesystem::signal_post_rename,
222
			$fileActions,
223
			'rename'
224
		);
225
		Util::connectHook(
226
			Filesystem::CLASSNAME,
227
			Filesystem::signal_post_create,
228
			$fileActions,
229
			'create'
230
		);
231
		Util::connectHook(
232
			Filesystem::CLASSNAME,
233
			Filesystem::signal_post_copy,
234
			$fileActions,
235
			'copy'
236
		);
237
		Util::connectHook(
238
			Filesystem::CLASSNAME,
239
			Filesystem::signal_post_write,
240
			$fileActions,
241
			'write'
242
		);
243
		Util::connectHook(
244
			Filesystem::CLASSNAME,
245
			Filesystem::signal_post_update,
246
			$fileActions,
247
			'update'
248
		);
249
		Util::connectHook(
250
			Filesystem::CLASSNAME,
251
			Filesystem::signal_read,
252
			$fileActions,
253
			'read'
254
		);
255
		Util::connectHook(
256
			Filesystem::CLASSNAME,
257
			Filesystem::signal_delete,
258
			$fileActions,
259
			'delete'
260
		);
261
	}
262
263
	private function versionsHooks(ILogger $logger) {
264
		$versionsActions = new Versions($logger);
265
		Util::connectHook('\OCP\Versions', 'rollback', $versionsActions, 'rollback');
266
		Util::connectHook('\OCP\Versions', 'delete', $versionsActions, 'delete');
267
	}
268
269
	private function trashbinHooks(ILogger $logger) {
270
		$trashActions = new Trashbin($logger);
271
		Util::connectHook('\OCP\Trashbin', 'preDelete', $trashActions, 'delete');
272
		Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $trashActions, 'restore');
273
	}
274
275
	private function securityHooks(ILogger $logger,
276
									 IServerContainer $serverContainer) {
277
		$eventDispatcher = $serverContainer->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

277
		$eventDispatcher = /** @scrutinizer ignore-deprecated */ $serverContainer->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...
278
		$eventDispatcher->addListener(IProvider::EVENT_SUCCESS, function (GenericEvent $event) use ($logger) {
279
			$security = new Security($logger);
280
			$security->twofactorSuccess($event->getSubject(), $event->getArguments());
281
		});
282
		$eventDispatcher->addListener(IProvider::EVENT_FAILED, function (GenericEvent $event) use ($logger) {
283
			$security = new Security($logger);
284
			$security->twofactorFailed($event->getSubject(), $event->getArguments());
285
		});
286
	}
287
}
288