Debian Codegraph @

lib/modules/takUser.inc

raw
1 <?php
2 /*
3
4 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
5 Copyright (C) 2025 Mark Halliday
6 2025 Roland Gruber
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 */
23
24 use LAM\TYPES\ConfiguredType;
25
26 /**
27 * Manages the object class "takUser" for users.
28 *
29 * @package modules
30 *
31 * @author Mark Halliday
32 * @author Roland Gruber
33 */
34 class takUser extends baseModule {
35
36 /**
37 * {@inheritDoc}
38 */
39 public function can_manage(): bool {
40 return $this->get_scope() === 'user';
41 }
42
43 /** possible roles */
44 private const ROLE_TYPES = ['Team Member', 'Team Leader', 'HQ', 'RTO', 'K9', 'Medic', 'Forward Observer', 'Sniper'];
45
46 /** possible colors */
47 private const COLOR_TYPES = ['Blue', 'Brown', 'Cyan', 'Dark Blue', 'Dark Green', 'Green', 'Magenta', 'Maroon', 'Orange', 'Purple', 'Red', 'Teal', 'White', 'Yellow'];
48
49
50
51 /**
52 * {@inheritDoc}
53 */
54 public function get_metaData(): array {
55 $return = [];
56 // icon
57 $return['icon'] = 'tak.png';
58 // alias name
59 $return["alias"] = 'TAK';
60 // LDAP filter
61 $return["ldap_filter"] = ['or' => "(objectClass=takUser)"];
62 // module dependencies
63 $return['dependencies'] = ['depends' => [], 'conflicts' => []];
64 // managed object classes
65 $return['objectClasses'] = ['takUser'];
66 // managed attributes
67 $return['attributes'] = ['takcallsign', 'takrole', 'takcolor'
68 ];
69 // help Entries
70 $return['help'] = [
71 'takcallsign' => [
72 "Headline" => _("Callsign"), 'attr' => 'takCallsign',
73 "Text" => _("The user's callsign to be displayed to other TAK users. It must be unique.")
74 ],
75 'takrole' => [
76 "Headline" => _("Team role"), 'attr' => 'takRole',
77 "Text" => _("The user's role to be displayed to other TAK users.")
78 ],
79 'takcolor' => [
80 "Headline" => _("Team color"), 'attr' => 'takColor',
81 "Text" => _("The user's team color to be displayed to other TAK users.")
82 ],
83 ];
84 // upload fields
85 $return['upload_columns'] = [
86 [
87 'name' => 'takuser_takcallsign',
88 'description' => _('Callsign'),
89 'help' => 'takcallsign',
90 'example' => 'UK-ORG-01',
91 'required' => true,
92 'unique' => true
93 ],
94 [
95 'name' => 'takuser_takrole',
96 'description' => _('Team role'),
97 'help' => 'takrole',
98 'default' => 'Team Member',
99 'values' => implode(", ", self::ROLE_TYPES),
100 'example' => 'Team Member',
101 'required' => true
102 ],
103 [
104 'name' => 'takuser_takcolor',
105 'description' => _('Team color'),
106 'help' => 'takcolor',
107 'default' => 'Cyan',
108 'values' => implode(", ", self::COLOR_TYPES),
109 'example' => 'Cyan',
110 'required' => true
111 ],
112 ];
113 // available PDF fields
114 $return['PDF_fields'] = [
115 'takcallsign' => _('Callsign'),
116 'takrole' => _('Team role'),
117 'takcolor' => _('Team color'),
118 ];
119 // profile options
120 $profileContainer = new htmlResponsiveRow();
121 $profileContainer->add(new htmlResponsiveInputField(_('Callsign'), 'takuser_takcallsign', null, 'takusercallsign'));
122 $profileContainer->add(new htmlResponsiveInputField(_('Team role'), 'takuser_takrole', null, 'takuserrole'));
123 $profileContainer->add(new htmlResponsiveInputField(_('Team color'), 'takuser_takcolor', null, 'takusercolor'));
124 $return['profile_options'] = $profileContainer;
125 // self-service field settings
126 $return['selfServiceFieldSettings'] = [
127 'takcallsign' => _('Callsign'),
128 'takrole' => _('Team role'),
129 'takcolor' => _('Team color'),
130 ];
131 return $return;
132 }
133
134 /**
135 * {@inheritDoc}
136 */
137 protected function load_Messages(): void {
138 $this->messages['takcallsign'][0] = ['ERROR', _('Callsign'), _('Callsign contains invalid characters. Valid characters are: a-z, A-Z, 0-9 and .-_.')];
139 $this->messages['takcallsign'][1] = ['ERROR', _('A TAK login with this callsign already exists. Please choose a different callsign.')];
140 $this->messages['takcallsign'][2] = ['ERROR', _('Callsign'), _('This field is required.')];
141 $this->messages['takcallsign'][3] = ['ERROR', _('Account %s:') . ' takuser_takcallsign', _('Callsign contains invalid characters. Valid characters are: a-z, A-Z, 0-9 and .-_.')];
142 $this->messages['takrole'][0] = ['ERROR', _('Account %s:') . ' takuser_takrole', _('Please enter a valid option:') . ' ' . implode(', ', self::ROLE_TYPES)];
143 $this->messages['takcolor'][0] = ['ERROR', _('Account %s:') . ' takuser_takcolor', _('Please enter a valid option:') . ' ' . implode(', ', self::COLOR_TYPES)];
144 }
145
146 /**
147 * {@inheritDoc}
148 */
149 public function display_html_attributes(): htmlElement|array {
150 $return = new htmlResponsiveRow();
151 // takCallsign
152 $this->addSimpleInputTextField($return, 'takcallsign', _('Callsign'), true);
153 // takRole
154 $takRole = $this->attributes['takrole'][0] ?? '';
155 $return->add(new htmlResponsiveSelect('takrole', self::ROLE_TYPES, [$takRole], _('Team role'), 'takrole'));
156 //takColor
157 $takColor = $this->attributes['takcolor'][0] ?? 'Cyan';
158 $return->add(new htmlResponsiveSelect('takcolor', self::COLOR_TYPES, [$takColor], _('Team color'), 'takcolor'));
159 return $return;
160 }
161
162 /**
163 * {@inheritDoc}
164 */
165 public function process_attributes(): array {
166 $return = [];
167 $this->attributes['takcallsign'][0] = $_POST['takcallsign'];
168 $this->attributes['takrole'][0] = $_POST['takrole'];
169 $this->attributes['takcolor'][0] = $_POST['takcolor'];
170 // check if callsign is filled
171 if ($_POST['takcallsign'] == '') {
172 $return[] = $this->messages['takcallsign'][2];
173 }
174 elseif (!get_preg($_POST['takcallsign'], 'username')) {
175 $return[] = $this->messages['takcallsign'][0];
176 }
177 // check if the callsign is unique
178 elseif (empty($this->orig['takcallsign'][0]) || ($this->attributes['takcallsign'][0] !== $this->orig['takcallsign'][0])) {
179 $suffix = $this->getAccountContainer()->get_type()->getSuffix();
180 $search = searchLDAP($suffix, 'takcallsign=' . ldap_escape($_POST['takcallsign'], '', LDAP_ESCAPE_FILTER), ['dn']);
181 if (!empty($search)) {
182 $return[] = $this->messages['takcallsign'][1];
183 }
184 }
185 return $return;
186 }
187
188 /**
189 * {@inheritDoc}
190 */
191 public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array {
192 $errors = [];
193 for ($i = 0; $i < count($rawAccounts); $i++) {
194 // add object class
195 if (!in_array('takUser', $partialAccounts[$i]['objectClass'])) {
196 $partialAccounts[$i]['objectClass'][] = 'takUser';
197 }
198 $this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'takuser_takcallsign',
199 'takcallsign', 'username', $this->messages['takcallsign'][3], $errors);
200 $this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'takuser_takrole',
201 'takrole', null, [], $errors);
202 if (!in_array($partialAccounts[$i]['takrole'][0], self::ROLE_TYPES)) {
203 $errors[] = array_merge($this->messages['takrole'][0], [[$i]]);
204 }
205 $this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'takuser_takcolor',
206 'takcolor', null, [], $errors);
207 if (!in_array($partialAccounts[$i]['takcolor'][0], self::COLOR_TYPES)) {
208 $errors[] = array_merge($this->messages['takcolor'][0], [[$i]]);
209 }
210 }
211 return $errors;
212 }
213
214 /**
215 * {@inheritDoc}
216 */
217 public function get_pdfEntries(array $pdfKeys, string $typeId): array {
218 $return = [];
219 $this->addSimplePDFField($return, 'takcallsign', _('Callsign'));
220 $this->addSimplePDFField($return, 'takrole', _('Team role'));
221 $this->addSimplePDFField($return, 'takcolor', _('Team color'));
222 return $return;
223 }
224
225 /**
226 * {@inheritDoc}
227 */
228 public function load_profile(array $profile): void {
229 // profile mappings in meta data
230 parent::load_profile($profile);
231 // special profile options
232 if (!empty($profile['takuser_takcallsign'][0])) {
233 $this->attributes['takcallsign'][0] = $profile['takuser_takcallsign'][0];
234 }
235 if (!empty($profile['takuser_takrole'][0])) {
236 $this->attributes['takrole'][0] = $profile['takuser_takrole'][0];
237 }
238 if (!empty($profile['takuser_takcolor'][0])) {
239 $this->attributes['takcolor'][0] = $profile['takuser_takcolor'][0];
240 }
241 }
242
243 /**
244 * {@inheritDoc}
245 */
246 public function getSelfServiceOptions(array $fields, array $attributes, bool $passwordChangeOnly, array $readOnlyFields): array {
247 $return = [];
248 if ($passwordChangeOnly) {
249 return $return; // only password fields as long no LDAP content can be read
250 }
251 if (!in_array_ignore_case('takUser', $attributes['objectClass'])) {
252 return $return;
253 }
254 $this->addSimpleSelfServiceTextField($return, 'takcallsign', _('Callsign'), $fields, $attributes, $readOnlyFields, true);
255 if (in_array('takrole', $fields)) {
256 $role = '';
257 if (isset($attributes['takrole'][0])) {
258 $role = $attributes['takrole'][0];
259 }
260 if (in_array('takrole', $readOnlyFields)) {
261 $field = new htmlOutputText($role);
262 }
263 else {
264 $selected = [];
265 if (!empty($attributes['takrole'][0])) {
266 $selected = [$attributes['takrole'][0]];
267 }
268 $field = new htmlSelect('takUser_takrole', self::ROLE_TYPES, $selected);
269 }
270 $return['takrole'] = new htmlResponsiveRow(
271 new htmlLabel('takUser_takrole', $this->getSelfServiceLabel('takrole', _('Team role'))), $field
272 );
273 }
274 if (in_array('takcolor', $fields)) {
275 $color = '';
276 if (isset($attributes['takcolor'][0])) {
277 $color = $attributes['takcolor'][0];
278 }
279 if (in_array('takcolor', $readOnlyFields)) {
280 $field = new htmlOutputText($color);
281 }
282 else {
283 $selected = [];
284 if (!empty($attributes['takcolor'][0])) {
285 $selected = [$attributes['takcolor'][0]];
286 }
287 $field = new htmlSelect('takUser_takcolor', self::COLOR_TYPES, $selected);
288 }
289 $return['takcolor'] = new htmlResponsiveRow(
290 new htmlLabel('takUser_takcolor', $this->getSelfServiceLabel('takcolor', _('Team color'))), $field
291 );
292 }
293 return $return;
294 }
295
296 /**
297 * @inheritDoc
298 */
299 public function checkSelfServiceOptions(array $fields, array $attributes, bool $passwordChangeOnly, array $readOnlyFields): array {
300 $return = ['messages' => [], 'add' => [], 'del' => [], 'mod' => [], 'info' => []];
301 if ($passwordChangeOnly) {
302 return $return; // skip processing if only a password change is done
303 }
304 if (!in_array_ignore_case('takUser', $attributes['objectClass'])) {
305 return $return;
306 }
307 $this->checkSimpleSelfServiceTextField($return, 'takcallsign', $attributes, $fields, $readOnlyFields, 'username', $this->messages['takcallsign'][0], $this->messages['takcallsign'][2]);
308 $this->checkSimpleSelfServiceTextField($return, 'takrole', $attributes, $fields, $readOnlyFields);
309 $this->checkSimpleSelfServiceTextField($return, 'takcolor', $attributes, $fields, $readOnlyFields);
310 return $return;
311 }
312
313 /**
314 * @inheritDoc
315 */
316 public function getListAttributeDescriptions(ConfiguredType $type): array {
317 return [
318 "takcallsign" => _("Callsign"),
319 "takrole" => _("Team role"),
320 "takcolor" => _("Team color"),
321 ];
322 }
323
324 }