Debian Codegraph @

lib/modules/kolabGroup.inc

raw
1 <?php
2
3 use LAM\TYPES\ConfiguredType;
4 use LAM\TYPES\TypeManager;
5
6 /*
7
8 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
9 Copyright (C) 2013 - 2026 Roland Gruber
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (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 General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 /**
27 * Manages Kolab group accounts.
28 *
29 * @package modules
30 * @author Roland Gruber
31 */
32
33 /**
34 * Manages Kolab group accounts.
35 *
36 * @package modules
37 */
38 class kolabGroup extends baseModule {
39
40 /** @var string[]|null $mailCache cache for mail attribute */
41 private ?array $mailCache = null;
42
43 /**
44 * Creates a new kolabGroup object.
45 *
46 * @param string $scope account type (user, group, host)
47 */
48 public function __construct($scope) {
49 // call parent constructor
50 parent::__construct($scope);
51 $this->autoAddObjectClasses = false;
52 }
53
54 /**
55 * {@inheritDoc}
56 */
57 public function can_manage(): bool {
58 return $this->get_scope() === 'group';
59 }
60
61 /**
62 * {@inheritDoc}
63 */
64 public function get_metaData(): array {
65 $return = [];
66 // icon
67 $return['icon'] = 'kolab.png';
68 // alias name
69 $return["alias"] = _("Kolab");
70 // module dependencies
71 $return['dependencies'] = ['depends' => [], 'conflicts' => []];
72 // managed object classes
73 $return['objectClasses'] = ['kolabGroupOfUniqueNames'];
74 // managed attributes
75 $return['attributes'] = ['kolabAllowSMTPRecipient', 'kolabAllowSMTPSender'];
76 // help Entries
77 $return['help'] = [
78 'mail' => [
79 "Headline" => _("Email address"), 'attr' => 'mail',
80 "Text" => _("The list's email address.")
81 ],
82 'mailList' => [
83 "Headline" => _("Email address"), 'attr' => 'mail',
84 "Text" => _("The list's email address.") . ' ' . _("Multiple values are separated by semicolon.")
85 ],
86 'kolabAllowSMTPRecipient' => [
87 "Headline" => _('Allowed recipients'), 'attr' => 'kolabAllowSMTPRecipient',
88 "Text" => _('Describes the allowed or disallowed SMTP recipient addresses for mail sent by this account (e.g. "domain.tld" or "[email protected]").')
89 ],
90 'kolabAllowSMTPRecipientList' => [
91 "Headline" => _('Allowed recipients'), 'attr' => 'kolabAllowSMTPRecipient',
92 "Text" => _('Describes the allowed or disallowed SMTP recipient addresses for mail sent by this account (e.g. "domain.tld" or "[email protected]").')
93 . ' ' . _("Multiple values are separated by semicolon.")
94 ],
95 'kolabAllowSMTPSender' => [
96 "Headline" => _('Allowed senders'), 'attr' => 'kolabAllowSMTPSender',
97 "Text" => _('Describes the allowed or disallowed SMTP addresses sending mail to this account (e.g. "domain.tld" or "[email protected]").')
98 ],
99 'kolabAllowSMTPSenderList' => [
100 "Headline" => _('Allowed senders'), 'attr' => 'kolabAllowSMTPSender',
101 "Text" => _('Describes the allowed or disallowed SMTP addresses sending mail to this account (e.g. "domain.tld" or "[email protected]").')
102 . ' ' . _("Multiple values are separated by semicolon.")
103 ],
104 'autoAdd' => [
105 "Headline" => _("Automatically add this extension"),
106 "Text" => _("This will enable the extension automatically if this profile is loaded.")
107 ],
108 'addExtension' => [
109 "Headline" => _("Add Kolab extension"),
110 "Text" => _("Adds this Kolab extension.")
111 ],
112 ];
113 // profile options
114 $profileContainer = new htmlResponsiveRow();
115 $profileContainer->add(new htmlResponsiveInputCheckbox('kolabGroup_addExt', false, _('Automatically add this extension'), 'autoAdd'));
116 $return['profile_options'] = $profileContainer;
117 // upload fields
118 $return['upload_columns'] = [
119 [
120 'name' => 'kolabGroup_addExtension',
121 'description' => _('Add Kolab extension'),
122 'help' => 'addExtension',
123 'example' => 'false',
124 'default' => 'false',
125 'values' => 'true, false'
126 ],
127 [
128 'name' => 'kolabGroup_kolabAllowSMTPRecipient',
129 'description' => _('Allowed recipients'),
130 'help' => 'kolabAllowSMTPRecipientList',
131 'example' => '.com; -.net',
132 ],
133 [
134 'name' => 'kolabGroup_kolabAllowSMTPSender',
135 'description' => _('Allowed senders'),
136 'help' => 'kolabAllowSMTPSenderList',
137 'example' => '.com; -.net',
138 ],
139 ];
140 // available PDF fields
141 $return['PDF_fields'] = [
142 'kolabAllowSMTPRecipient' => _('Allowed recipients'),
143 'kolabAllowSMTPSender' => _('Allowed senders'),
144 ];
145 return $return;
146 }
147
148 /**
149 * {@inheritDoc}
150 */
151 protected function load_Messages(): void {
152 $this->messages['mail'][0] = ['ERROR', _('Email address'), _('Please enter a valid email address!')];
153 $this->messages['mail'][1] = ['ERROR', _('Account %s:') . ' kolabGroup_mail', _('Please enter a valid email address!')];
154 $this->messages['mail'][2] = ['ERROR', _('Email address'), _('Email address already exists.')];
155 $this->messages['mail'][3] = ['ERROR', _('Account %s:') . ' kolabGroup_mail', _('Email address already exists.')];
156 $this->messages['kolabAllowSMTPRecipient'][0] = ['ERROR', _('Allowed recipients'), _('Please enter a valid recipient expression.')];
157 $this->messages['kolabAllowSMTPRecipient'][1] = ['ERROR', _('Account %s:') . ' kolabGroup_kolabAllowSMTPRecipient', _('Please enter a valid recipient expression.')];
158 $this->messages['kolabAllowSMTPSender'][0] = ['ERROR', _('Allowed senders'), _('Please enter a valid sender expression.')];
159 $this->messages['kolabAllowSMTPSender'][1] = ['ERROR', _('Account %s:') . ' kolabGroup_kolabAllowSMTPSender', _('Please enter a valid sender expression.')];
160 }
161
162 /**
163 * {@inheritDoc}
164 */
165 public function display_html_attributes(): htmlElement|array {
166 $container = new htmlResponsiveRow();
167 if (isset($this->attributes['objectClass']) && in_array('kolabGroupOfUniqueNames', $this->attributes['objectClass'])) {
168 // mail
169 if ($this->manageMail($this->getAccountContainer()->get_type()->getModules())) {
170 $this->addSimpleInputTextField($container, 'mail', _('Email address'), true);
171 }
172 // allowed recipients
173 $this->addMultiValueInputTextField($container, 'kolabAllowSMTPRecipient', _('Allowed recipients'));
174 // allowed senders
175 $this->addMultiValueInputTextField($container, 'kolabAllowSMTPSender', _('Allowed senders'));
176 }
177 else {
178 // add button
179 $container->add(new htmlButton('addObjectClass', _('Add Kolab extension')));
180 }
181 return $container;
182 }
183
184 /**
185 * {@inheritDoc}
186 */
187 public function process_attributes(): array {
188 $errors = [];
189 if (isset($_POST['addObjectClass'])) {
190 $this->attributes['objectClass'][] = 'kolabGroupOfUniqueNames';
191 return $errors;
192 }
193 if (isset($_POST['remObjectClass'])) {
194 $this->attributes['objectClass'] = array_delete(['kolabGroupOfUniqueNames'], $this->attributes['objectClass']);
195 $attrs = $this->meta['attributes'];
196 foreach ($attrs as $name) {
197 if (isset($this->attributes[$name])) {
198 unset($this->attributes[$name]);
199 }
200 }
201 return $errors;
202 }
203 // skip processing if object class is not set
204 if (!isset($this->attributes['objectClass']) || !in_array('kolabGroupOfUniqueNames', $this->attributes['objectClass'])) {
205 return $errors;
206 }
207 // mail
208 if ($this->manageMail($this->getAccountContainer()->get_type()->getModules())) {
209 if (!empty($_POST['mail'])) {
210 $this->attributes['mail'][0] = $_POST['mail'];
211 // check format
212 if (!get_preg($_POST['mail'], 'email')) {
213 $errors[] = $this->messages['mail'][0];
214 }
215 // check if unique
216 if ($this->getAccountContainer()->isNewAccount || (!empty($this->orig['mail'][0]) && ($this->orig['mail'][0] != $this->attributes['mail'][0]))) {
217 $this->loadMailCache();
218 if (in_array_ignore_case($_POST['mail'], $this->mailCache)) {
219 $errors[] = $this->messages['mail'][2];
220 }
221 }
222 }
223 elseif (isset($this->attributes['mail'])) {
224 unset($this->attributes['mail']);
225 }
226 }
227 // allowed recipients
228 $this->processMultiValueInputTextField('kolabAllowSMTPRecipient', $errors, 'kolabEmailPrefix');
229 // allowed senders
230 $this->processMultiValueInputTextField('kolabAllowSMTPSender', $errors, 'kolabEmailPrefix');
231 return $errors;
232 }
233
234 /**
235 * {@inheritDoc}
236 */
237 public function save_attributes(): array {
238 if (!in_array('kolabGroupOfUniqueNames', $this->attributes['objectClass']) && !in_array('kolabGroupOfUniqueNames', $this->orig['objectClass'])) {
239 // skip saving if the extension was not added/modified
240 return [];
241 }
242 return parent::save_attributes();
243 }
244
245 /**
246 * {@inheritDoc}
247 */
248 public function load_profile(array $profile): void {
249 // profile mappings in meta data
250 parent::load_profile($profile);
251 // add extension
252 if (isset($profile['kolabGroup_addExt'][0])
253 && ($profile['kolabGroup_addExt'][0] == "true")
254 && !in_array('kolabGroupOfUniqueNames', $this->attributes['objectClass'])) {
255 $this->attributes['objectClass'][] = 'kolabGroupOfUniqueNames';
256 }
257 }
258
259 /**
260 * {@inheritDoc}
261 */
262 public function get_uploadColumns(array $selectedModules, ConfiguredType $type): array {
263 $return = parent::get_uploadColumns($selectedModules, $type);
264 if ($this->manageMail($selectedModules)) {
265 $return[] = [
266 'name' => 'kolabGroup_mail',
267 'description' => _('Email address'),
268 'help' => 'mailList',
269 'example' => '[email protected]',
270 'required' => true,
271 'unique' => true,
272 ];
273 }
274 return $return;
275 }
276
277 /**
278 * {@inheritDoc}
279 */
280 public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array {
281 $messages = [];
282 for ($i = 0; $i < count($rawAccounts); $i++) {
283 // check if extension should be added
284 if (isset($rawAccounts[$i][$ids['kolabGroup_addExtension']]) && ($rawAccounts[$i][$ids['kolabGroup_addExtension']] != 'true')) {
285 continue;
286 }
287 // add object classes
288 if (!in_array('kolabGroupOfUniqueNames', $partialAccounts[$i]['objectClass'])) {
289 $partialAccounts[$i]['objectClass'][] = 'kolabGroupOfUniqueNames';
290 }
291 // mail
292 if ($this->manageMail($selectedModules) && !empty($rawAccounts[$i][$ids['kolabGroup_mail']])) {
293 if (get_preg($rawAccounts[$i][$ids['kolabGroup_mail']], 'email')) {
294 $this->loadMailCache();
295 if (!in_array_ignore_case(trim($rawAccounts[$i][$ids['kolabGroup_mail']]), $this->mailCache)) {
296 $partialAccounts[$i]['mail'] = trim($rawAccounts[$i][$ids['kolabGroup_mail']]);
297 }
298 else {
299 $errMsg = $this->messages['mail'][3];
300 $errMsg[] = [$i];
301 $messages[] = $errMsg;
302 }
303 }
304 else {
305 $errMsg = $this->messages['mail'][1];
306 $errMsg[] = [$i];
307 $messages[] = $errMsg;
308 }
309 }
310 // allowed recipients
311 $this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'kolabGroup_kolabAllowSMTPRecipient', 'kolabAllowSMTPRecipient', 'kolabEmailPrefix', $this->messages['kolabAllowSMTPRecipient'][1], $messages, '/;[ ]*/');
312 // allowed senders
313 $this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'kolabGroup_kolabAllowSMTPSender', 'kolabAllowSMTPSender', 'kolabEmailPrefix', $this->messages['kolabAllowSMTPSender'][1], $messages, '/;[ ]*/');
314 }
315 return $messages;
316 }
317
318 /**
319 * {@inheritDoc}
320 */
321 public function get_pdfFields(string $typeId): array {
322 $fields = parent::get_pdfFields($typeId);
323 $typeManager = new TypeManager();
324 $modules = $typeManager->getConfiguredType($typeId)->getModules();
325 if ($this->manageMail($modules)) {
326 $fields['mail'] = _('Email address');
327 }
328 return $fields;
329 }
330
331 /**
332 * {@inheritDoc}
333 */
334 public function get_pdfEntries(array $pdfKeys, string $typeId): array {
335 $return = [];
336 $this->addSimplePDFField($return, 'mail', _('Email address'));
337 $this->addSimplePDFField($return, 'kolabAllowSMTPRecipient', _('Allowed recipients'));
338 $this->addSimplePDFField($return, 'kolabAllowSMTPSender', _('Allowed senders'));
339 return $return;
340 }
341
342 /**
343 * Returns if the mail attribute should be managed.
344 *
345 * @param string[] $modules account modules
346 * @return boolean manage mail attribute
347 */
348 private function manageMail($modules) {
349 return !in_array('qmailGroup', $modules);
350 }
351
352 /**
353 * Loads the list of email addresses into the cache.
354 */
355 private function loadMailCache(): void {
356 if ($this->mailCache !== null) {
357 return;
358 }
359 $results = searchLDAPByFilter('(mail=*)', ['mail'], [$this->get_scope()]);
360 $this->mailCache = [];
361 foreach ($results as $result) {
362 if (isset($result['mail'][0])) {
363 $this->mailCache[] = $result['mail'][0];
364 }
365 }
366 }
367
368 /**
369 * {@inheritDoc}
370 */
371 public function getManagedAttributes(string $typeId): array {
372 $attrs = parent::getManagedAttributes($typeId);
373 $typeManager = new TypeManager();
374 if ($this->manageMail($typeManager->getConfiguredType($typeId)->getModules())) {
375 $attrs[] = 'mail';
376 }
377 return $attrs;
378 }
379
380 }
381
382
383