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

UserAddedToGroupActivityListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 56
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A handle() 0 35 4
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2021 Roeland Jago Douma <[email protected]>
7
 *
8
 * @author Roeland Jago Douma <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Settings\Listener;
28
29
use OC\Group\Manager;
30
use OCA\Settings\Activity\GroupProvider;
31
use OCP\Activity\IManager;
32
use OCP\EventDispatcher\Event;
33
use OCP\EventDispatcher\IEventListener;
34
use OCP\Group\Events\UserAddedEvent;
35
use OCP\IUser;
36
use OCP\IUserSession;
37
38
class UserAddedToGroupActivityListener implements IEventListener {
39
40
	/** @var Manager */
41
	private $groupManager;
42
43
	/** @var IManager */
44
	private $activityManager;
45
46
	/** @var IUserSession */
47
	private $userSession;
48
49
	public function __construct(
50
		Manager $groupManager,
51
		IManager $activityManager,
52
		IUserSession $userSession
53
	) {
54
		$this->groupManager = $groupManager;
55
		$this->activityManager = $activityManager;
56
		$this->userSession = $userSession;
57
	}
58
59
	public function handle(Event $event): void {
60
		if (!($event instanceof UserAddedEvent)) {
61
			return;
62
		}
63
64
		$user = $event->getUser();
65
		$group = $event->getGroup();
66
67
		$subAdminManager = $this->groupManager->getSubAdmin();
68
		$usersToNotify = $subAdminManager->getGroupsSubAdmins($group);
69
		$usersToNotify[] = $user;
70
71
72
		$event = $this->activityManager->generateEvent();
73
		$event->setApp('settings')
74
			->setType('group_settings');
75
76
		$actor = $this->userSession->getUser();
77
		if ($actor instanceof IUser) {
78
			$event->setAuthor($actor->getUID())
79
				->setSubject(GroupProvider::ADDED_TO_GROUP, [
80
					'user' => $user->getUID(),
81
					'group' => $group->getGID(),
82
					'actor' => $actor->getUID(),
83
				]);
84
		} else {
85
			$event->setSubject(GroupProvider::ADDED_TO_GROUP, [
86
				'user' => $user->getUID(),
87
				'group' => $group->getGID(),
88
			]);
89
		}
90
91
		foreach ($usersToNotify as $userToNotify) {
92
			$event->setAffectedUser($userToNotify->getUID());
93
			$this->activityManager->publish($event);
94
		}
95
	}
96
}
97