-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactController.php
More file actions
111 lines (92 loc) · 3.66 KB
/
Copy pathContactController.php
File metadata and controls
111 lines (92 loc) · 3.66 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
<?php
// SPDX-FileCopyrightText: 2023 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Icinga\Module\Notifications\Controllers;
use Exception;
use Icinga\Application\Config;
use Icinga\Authentication\User\DomainAwareInterface;
use Icinga\Authentication\User\UserBackend;
use Icinga\Data\Selectable;
use Icinga\Module\Notifications\Common\Database;
use Icinga\Module\Notifications\Web\Form\ContactForm;
use Icinga\Repository\Repository;
use Icinga\Web\Notification;
use ipl\Web\Compat\CompatController;
use ipl\Web\FormElement\SearchSuggestions;
class ContactController extends CompatController
{
public function init(): void
{
$this->assertPermission('notifications/config/contacts');
}
public function indexAction(): void
{
$contactId = $this->params->getRequired('id');
$form = (new ContactForm(Database::get()))
->loadContact($contactId)
->on(ContactForm::ON_SUCCESS, function (ContactForm $form) {
$form->editContact();
Notification::success(sprintf(
t('Contact "%s" has successfully been saved'),
$form->getContactName()
));
$this->redirectNow('__CLOSE__');
})->on(ContactForm::ON_REMOVE, function (ContactForm $form) {
$form->removeContact();
Notification::success(sprintf(
t('Deleted contact "%s" successfully'),
$form->getContactName()
));
$this->redirectNow('__CLOSE__');
})->handleRequest($this->getServerRequest());
$this->addTitleTab(sprintf(t('Contact: %s'), $form->getContactName()));
$this->addContent($form);
}
public function suggestIcingaWebUserAction(): void
{
$suggestions = new SearchSuggestions((function () use (&$suggestions) {
$userBackends = [];
foreach (Config::app('authentication') as $backendName => $backendConfig) {
$candidate = UserBackend::create($backendName, $backendConfig);
if ($candidate instanceof Selectable) {
$userBackends[] = $candidate;
}
}
$limit = 10;
while ($limit > 0 && ! empty($userBackends)) {
/** @var Repository $backend */
$backend = array_shift($userBackends);
$query = $backend->select()
->from('user', ['user_name'])
->where('user_name', $suggestions->getSearchTerm())
->limit($limit);
try {
/** @var string[] $names */
$names = $query->fetchColumn();
} catch (Exception) {
continue;
}
if (empty($names)) {
continue;
}
$domain = null;
if ($backend instanceof DomainAwareInterface && $backend->getDomain()) {
$domain = '@' . $backend->getDomain();
}
foreach ($names as $name) {
yield [
'search' => $name . $domain,
'label' => $name . $domain,
'backend' => $backend->getName(),
];
}
$limit -= count($names);
}
})());
$suggestions->setGroupingCallback(function (array $data) {
return $data['backend'];
});
$suggestions->forRequest($this->getServerRequest());
$this->getDocument()->addHtml($suggestions);
}
}