-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotifyAdminsOrGroupsProfileFieldChangeOperation.php
More file actions
186 lines (159 loc) · 5.17 KB
/
NotifyAdminsOrGroupsProfileFieldChangeOperation.php
File metadata and controls
186 lines (159 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and LibreCode contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
declare(strict_types=1);
namespace OCA\ProfileFields\Workflow;
use OCA\ProfileFields\AppInfo\Application;
use OCA\ProfileFields\Workflow\Event\AbstractProfileFieldValueEvent;
use OCP\EventDispatcher\Event;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Notification\IManager as INotificationManager;
use OCP\WorkflowEngine\IManager;
use OCP\WorkflowEngine\IOperation;
use OCP\WorkflowEngine\IRuleMatcher;
class NotifyAdminsOrGroupsProfileFieldChangeOperation implements IOperation {
public function __construct(
private INotificationManager $notificationManager,
private IGroupManager $groupManager,
private IUserManager $userManager,
private IL10N $l10n,
private IURLGenerator $urlGenerator,
private ProfileFieldValueSubjectContext $workflowSubjectContext,
) {
}
#[\Override]
public function getDisplayName(): string {
return $this->l10n->t('Notify admins or groups');
}
#[\Override]
public function getDescription(): string {
return $this->l10n->t('Send an internal notification to configured admins, groups, or users when a profile field change matches the workflow rule.');
}
#[\Override]
public function getIcon(): string {
return $this->urlGenerator->imagePath('core', 'actions/user-admin.svg');
}
#[\Override]
public function isAvailableForScope(int $scope): bool {
return $scope === IManager::SCOPE_ADMIN;
}
#[\Override]
public function validateOperation(string $name, array $checks, string $operation): void {
$config = $this->parseConfig($operation);
if ($config === null || $this->resolveRecipientUids($config['targets']) === []) {
throw new \UnexpectedValueException($this->l10n->t('A valid recipient list is required.'));
}
}
#[\Override]
public function onEvent(string $eventName, Event $event, IRuleMatcher $ruleMatcher): void {
if (!$event instanceof AbstractProfileFieldValueEvent) {
return;
}
try {
$matches = $ruleMatcher->getFlows(false);
if ($matches === []) {
return;
}
$subject = $event->getWorkflowSubject();
$fieldDefinition = $subject->getFieldDefinition();
$fieldLabel = trim($fieldDefinition->getLabel()) !== '' ? $fieldDefinition->getLabel() : $fieldDefinition->getFieldKey();
foreach ($matches as $match) {
$config = $this->parseConfig((string)($match['operation'] ?? ''));
if ($config === null) {
continue;
}
foreach ($this->resolveRecipientUids($config['targets']) as $recipientUid) {
$subjectText = $this->l10n->t('Profile field updated');
// TRANSLATORS %1$s is the actor user ID, %2$s is the affected user ID, %3$s is the profile field label.
$messageText = $this->l10n->t(
'%1$s changed profile field "%3$s" for user %2$s.',
[
$subject->getActorUid(),
$subject->getUserUid(),
$fieldLabel,
],
);
$notification = $this->notificationManager->createNotification();
$notification
->setApp(Application::APP_ID)
->setUser($recipientUid)
->setObject('profile-field-admin-change', sprintf('%s:%s:%s', (string)($match['id'] ?? 'workflow'), $recipientUid, $fieldDefinition->getFieldKey()))
->setDateTime(new \DateTime())
->setSubject('profile_field_updated')
->setMessage('profile_field_updated_message', [
$subject->getActorUid(),
$subject->getUserUid(),
$fieldLabel,
])
->setParsedSubject($subjectText)
->setParsedMessage($messageText)
->setIcon($this->urlGenerator->getAbsoluteURL($this->getIcon()));
$this->notificationManager->notify($notification);
}
}
} finally {
$this->workflowSubjectContext->clear();
}
}
/**
* @return array{targets: string}|null
*/
private function parseConfig(string $operation): ?array {
$config = trim($operation);
if ($config === '') {
return ['targets' => 'admin'];
}
try {
$decoded = json_decode($config, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException) {
return null;
}
if (!is_array($decoded)) {
return null;
}
$targets = trim((string)($decoded['targets'] ?? ''));
if ($targets === '') {
return null;
}
return ['targets' => $targets];
}
/**
* @return list<string>
*/
private function resolveRecipientUids(string $targets): array {
$resolved = [];
foreach (preg_split('/\s*,\s*/', trim($targets)) ?: [] as $target) {
if ($target === '' || $target === 'invalid') {
continue;
}
if ($target === 'admin') {
$adminGroup = $this->groupManager->get('admin');
foreach ($adminGroup?->getUsers() ?? [] as $admin) {
$resolved[$admin->getUID()] = $admin->getUID();
}
continue;
}
if (str_starts_with($target, 'user:')) {
$uid = substr($target, 5);
$user = $this->userManager->get($uid);
if ($user instanceof IUser) {
$resolved[$uid] = $uid;
}
continue;
}
if (str_starts_with($target, 'group:')) {
$group = $this->groupManager->get(substr($target, 6));
foreach ($group?->getUsers() ?? [] as $user) {
$resolved[$user->getUID()] = $user->getUID();
}
}
}
return array_values($resolved);
}
}