Passed
Push — master ( ab9207...b291d9 )
by Morris
10:44 queued 11s
created

apps/admin_audit/lib/AppInfo/Application.php (3 issues)

1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
5
 *
6
 * @author Bjoern Schiessle <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
namespace OCA\AdminAudit\AppInfo;
27
28
use OC\Files\Filesystem;
29
use OC\Files\Node\File;
30
use OC\Group\Manager;
31
use OC\User\Session;
32
use OCA\AdminAudit\Actions\AppManagement;
33
use OCA\AdminAudit\Actions\Auth;
34
use OCA\AdminAudit\Actions\Console;
35
use OCA\AdminAudit\Actions\Files;
36
use OCA\AdminAudit\Actions\GroupManagement;
37
use OCA\AdminAudit\Actions\Security;
38
use OCA\AdminAudit\Actions\Sharing;
39
use OCA\AdminAudit\Actions\Trashbin;
40
use OCA\AdminAudit\Actions\UserManagement;
41
use OCA\AdminAudit\Actions\Versions;
42
use OCP\App\ManagerEvent;
43
use OCP\AppFramework\App;
44
use OCP\Authentication\TwoFactorAuth\IProvider;
45
use OCP\Console\ConsoleEvent;
46
use OCP\IGroupManager;
47
use OCP\ILogger;
48
use OCP\IPreview;
49
use OCP\IUserSession;
50
use OCP\Util;
51
use Symfony\Component\EventDispatcher\GenericEvent;
52
use OCP\Share;
53
54
class Application extends App {
55
56
	/** @var ILogger */
57
	protected $logger;
58
59
	public function __construct() {
60
		parent::__construct('admin_audit');
61
		$this->initLogger();
62
	}
63
64
	public function initLogger() {
65
		$c = $this->getContainer()->getServer();
66
		$config = $c->getConfig();
67
68
		$default = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log';
69
		$logFile = $config->getAppValue('admin_audit', 'logfile', $default);
70
		if($logFile === null) {
0 ignored issues
show
The condition $logFile === null is always false.
Loading history...
71
			$this->logger = $c->getLogger();
72
			return;
73
		}
74
		$this->logger = $c->getLogFactory()->getCustomLogger($logFile);
75
76
	}
77
78
	public function register() {
79
		$this->registerHooks();
80
	}
81
82
	/**
83
	 * Register hooks in order to log them
84
	 */
85
	protected function registerHooks() {
86
		$this->userManagementHooks();
87
		$this->groupHooks();
88
		$this->authHooks();
89
90
		$this->consoleHooks();
91
		$this->appHooks();
92
93
		$this->sharingHooks();
94
95
		$this->fileHooks();
96
		$this->trashbinHooks();
97
		$this->versionsHooks();
98
99
		$this->securityHooks();
100
	}
101
102
	protected function userManagementHooks() {
103
		$userActions = new UserManagement($this->logger);
104
105
		Util::connectHook('OC_User', 'post_createUser',	$userActions, 'create');
106
		Util::connectHook('OC_User', 'post_deleteUser',	$userActions, 'delete');
107
		Util::connectHook('OC_User', 'changeUser',	$userActions, 'change');
108
109
		/** @var IUserSession|Session $userSession */
110
		$userSession = $this->getContainer()->getServer()->getUserSession();
111
		$userSession->listen('\OC\User', 'postSetPassword', [$userActions, 'setPassword']);
0 ignored issues
show
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

111
		$userSession->/** @scrutinizer ignore-call */ 
112
                listen('\OC\User', 'postSetPassword', [$userActions, 'setPassword']);
Loading history...
112
		$userSession->listen('\OC\User', 'assignedUserId', [$userActions, 'assign']);
113
		$userSession->listen('\OC\User', 'postUnassignedUserId', [$userActions, 'unassign']);
114
	}
115
116
	protected function groupHooks()  {
117
		$groupActions = new GroupManagement($this->logger);
118
119
		/** @var IGroupManager|Manager $groupManager */
120
		$groupManager = $this->getContainer()->getServer()->getGroupManager();
121
		$groupManager->listen('\OC\Group', 'postRemoveUser',  [$groupActions, 'removeUser']);
0 ignored issues
show
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

121
		$groupManager->/** @scrutinizer ignore-call */ 
122
                 listen('\OC\Group', 'postRemoveUser',  [$groupActions, 'removeUser']);
Loading history...
122
		$groupManager->listen('\OC\Group', 'postAddUser',  [$groupActions, 'addUser']);
123
		$groupManager->listen('\OC\Group', 'postDelete',  [$groupActions, 'deleteGroup']);
124
		$groupManager->listen('\OC\Group', 'postCreate',  [$groupActions, 'createGroup']);
125
	}
126
127
	protected function sharingHooks() {
128
		$shareActions = new Sharing($this->logger);
129
130
		Util::connectHook(Share::class, 'post_shared', $shareActions, 'shared');
131
		Util::connectHook(Share::class, 'post_unshare', $shareActions, 'unshare');
132
		Util::connectHook(Share::class, 'post_update_permissions', $shareActions, 'updatePermissions');
133
		Util::connectHook(Share::class, 'post_update_password', $shareActions, 'updatePassword');
134
		Util::connectHook(Share::class, 'post_set_expiration_date', $shareActions, 'updateExpirationDate');
135
		Util::connectHook(Share::class, 'share_link_access', $shareActions, 'shareAccessed');
136
	}
137
138
	protected function authHooks() {
139
		$authActions = new Auth($this->logger);
140
141
		Util::connectHook('OC_User', 'pre_login', $authActions, 'loginAttempt');
142
		Util::connectHook('OC_User', 'post_login', $authActions, 'loginSuccessful');
143
		Util::connectHook('OC_User', 'logout', $authActions, 'logout');
144
	}
145
146
	protected function appHooks() {
147
148
		$eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
149
		$eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function(ManagerEvent $event) {
150
			$appActions = new AppManagement($this->logger);
151
			$appActions->enableApp($event->getAppID());
152
		});
153
		$eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, function(ManagerEvent $event) {
154
			$appActions = new AppManagement($this->logger);
155
			$appActions->enableAppForGroups($event->getAppID(), $event->getGroups());
156
		});
157
		$eventDispatcher->addListener(ManagerEvent::EVENT_APP_DISABLE, function(ManagerEvent $event) {
158
			$appActions = new AppManagement($this->logger);
159
			$appActions->disableApp($event->getAppID());
160
		});
161
162
	}
163
164
	protected function consoleHooks() {
165
		$eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
166
		$eventDispatcher->addListener(ConsoleEvent::EVENT_RUN, function(ConsoleEvent $event) {
167
			$appActions = new Console($this->logger);
168
			$appActions->runCommand($event->getArguments());
169
		});
170
	}
171
172
	protected function fileHooks() {
173
		$fileActions = new Files($this->logger);
174
		$eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
175
		$eventDispatcher->addListener(
176
			IPreview::EVENT,
177
			function(GenericEvent $event) use ($fileActions) {
178
				/** @var File $file */
179
				$file = $event->getSubject();
180
				$fileActions->preview([
181
					'path' => substr($file->getInternalPath(), 5),
182
					'width' => $event->getArguments()['width'],
183
					'height' => $event->getArguments()['height'],
184
					'crop' => $event->getArguments()['crop'],
185
					'mode'  => $event->getArguments()['mode']
186
				]);
187
			}
188
		);
189
190
		Util::connectHook(
191
			Filesystem::CLASSNAME,
192
			Filesystem::signal_post_rename,
193
			$fileActions,
194
			'rename'
195
		);
196
		Util::connectHook(
197
			Filesystem::CLASSNAME,
198
			Filesystem::signal_post_create,
199
			$fileActions,
200
			'create'
201
		);
202
		Util::connectHook(
203
			Filesystem::CLASSNAME,
204
			Filesystem::signal_post_copy,
205
			$fileActions,
206
			'copy'
207
		);
208
		Util::connectHook(
209
			Filesystem::CLASSNAME,
210
			Filesystem::signal_post_write,
211
			$fileActions,
212
			'write'
213
		);
214
		Util::connectHook(
215
			Filesystem::CLASSNAME,
216
			Filesystem::signal_post_update,
217
			$fileActions,
218
			'update'
219
		);
220
		Util::connectHook(
221
			Filesystem::CLASSNAME,
222
			Filesystem::signal_read,
223
			$fileActions,
224
			'read'
225
		);
226
		Util::connectHook(
227
			Filesystem::CLASSNAME,
228
			Filesystem::signal_delete,
229
			$fileActions,
230
			'delete'
231
		);
232
	}
233
234
	protected function versionsHooks() {
235
		$versionsActions = new Versions($this->logger);
236
		Util::connectHook('\OCP\Versions', 'rollback', $versionsActions, 'rollback');
237
		Util::connectHook('\OCP\Versions', 'delete',$versionsActions, 'delete');
238
	}
239
240
	protected function trashbinHooks() {
241
		$trashActions = new Trashbin($this->logger);
242
		Util::connectHook('\OCP\Trashbin', 'preDelete', $trashActions, 'delete');
243
		Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $trashActions, 'restore');
244
	}
245
246
	protected function securityHooks() {
247
		$eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
248
		$eventDispatcher->addListener(IProvider::EVENT_SUCCESS, function(GenericEvent $event) {
249
			$security = new Security($this->logger);
250
			$security->twofactorSuccess($event->getSubject(), $event->getArguments());
251
		});
252
		$eventDispatcher->addListener(IProvider::EVENT_FAILED, function(GenericEvent $event) {
253
			$security = new Security($this->logger);
254
			$security->twofactorFailed($event->getSubject(), $event->getArguments());
255
		});
256
	}
257
}
258