Skip to content

Commit c2d00d5

Browse files
Merge pull request #8648 from christianbeeznest/fixes-updates485
Admin: Display single authentication method as read-only - refs #8540
2 parents 14d70ee + 387e588 commit c2d00d5

2 files changed

Lines changed: 98 additions & 53 deletions

File tree

public/main/admin/user_add.php

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ function update_user_add_password_section() {
211211
}
212212
}
213213

214+
$hasSingleAuthSource = 1 === count($authSources);
215+
214216
$form->addHtml(
215217
'<div class="mb-6 rounded-2xl border border-gray-25 bg-white p-6 shadow-sm">'.
216218
'<div class="mb-4">'.
@@ -219,41 +221,52 @@ function update_user_add_password_section() {
219221
'</div>'
220222
);
221223

222-
$form->addElement(
223-
'select',
224-
'auth_source',
225-
get_lang('Authentication methods'),
226-
$authSources,
227-
[
228-
'id' => 'auth_source',
229-
'multiple' => 'multiple',
230-
'size' => max(3, min(count($authSources), 8)),
231-
'class' => 'w-full rounded-lg border border-gray-25 bg-white px-3 py-2 text-body-2 text-gray-90 focus:border-primary focus:outline-none focus:ring-1 focus:ring-primary',
232-
]
233-
);
234-
$form->addRule('auth_source', get_lang('Required field'), 'required');
224+
if ($hasSingleAuthSource) {
225+
$singleAuthSourceLabel = (string) reset($authSources);
226+
$form->addElement(
227+
'static',
228+
null,
229+
get_lang('Authentication methods'),
230+
htmlspecialchars($singleAuthSourceLabel, \ENT_QUOTES, 'UTF-8')
231+
);
232+
} else {
233+
$form->addElement(
234+
'select',
235+
'auth_source',
236+
get_lang('Authentication methods'),
237+
$authSources,
238+
[
239+
'id' => 'auth_source',
240+
'multiple' => 'multiple',
241+
'size' => max(3, min(count($authSources), 8)),
242+
'class' => 'w-full rounded-lg border border-gray-25 bg-white px-3 py-2 text-body-2 text-gray-90 focus:border-primary focus:outline-none focus:ring-1 focus:ring-primary',
243+
]
244+
);
245+
$form->addRule('auth_source', get_lang('Required field'), 'required');
246+
}
235247

236248
$form->addHtml(
237249
'<div id="user-add-password-section" class="mt-6 rounded-xl border border-gray-25 bg-gray-5 p-4">'.
238250
'<h4 class="mb-3 text-body-1 font-semibold text-gray-90">'.get_lang('Password').'</h4>'
239251
);
240252

241-
$passwordGroup = [];
242-
$passwordGroup[] = $form->createElement(
243-
'radio',
244-
'password_auto',
245-
null,
246-
get_lang('Automatically generate a new password'),
247-
1
248-
);
249-
250253
if ($adminsCanSetUsersPass) {
254+
$passwordGroup = [];
255+
$passwordGroup[] = $form->createElement(
256+
'radio',
257+
'password_auto',
258+
null,
259+
get_lang('Automatically generate a new password'),
260+
1,
261+
['id' => 'radio_user_password_auto']
262+
);
251263
$passwordGroup[] = $form->createElement(
252264
'radio',
253265
'password_auto',
254-
'id="radio_user_password"',
266+
null,
255267
get_lang('Set password manually'),
256-
0
268+
0,
269+
['id' => 'radio_user_password']
257270
);
258271
$passwordGroup[] = $form->createElement(
259272
'password',
@@ -266,9 +279,16 @@ function update_user_add_password_section() {
266279
'show_hide' => true,
267280
]
268281
);
269-
}
270282

271-
$form->addGroup($passwordGroup, 'password', null, null, false);
283+
$form->addGroup($passwordGroup, 'password');
284+
} else {
285+
$form->addElement(
286+
'static',
287+
null,
288+
null,
289+
htmlspecialchars(get_lang('Automatically generate a new password'), \ENT_QUOTES, 'UTF-8')
290+
);
291+
}
272292
$form->addHtml('</div></div>');
273293

274294
// Status
@@ -383,7 +403,9 @@ function update_user_add_password_section() {
383403

384404
// Set default values
385405
$defaults['mail']['send_mail'] = 1;
386-
$defaults['auth_source'] = [UserAuthSource::PLATFORM];
406+
if (!$hasSingleAuthSource) {
407+
$defaults['auth_source'] = [UserAuthSource::PLATFORM];
408+
}
387409
$defaults['password']['password_auto'] = 1;
388410
$defaults['active'] = 1;
389411
$days = api_get_setting('account_valid_duration');
@@ -430,8 +452,11 @@ function update_user_add_password_section() {
430452
$hr_dept_id = isset($user['hr_dept_id']) ? (int) $user['hr_dept_id'] : 0;
431453

432454
$allowedAuthSources = array_map('strval', array_keys($authSources));
455+
$submittedAuthSources = $hasSingleAuthSource
456+
? $allowedAuthSources
457+
: array_map('strval', (array) ($user['auth_source'] ?? []));
433458
$auth_source = array_values(array_intersect(
434-
array_map('strval', (array) ($user['auth_source'] ?? [])),
459+
$submittedAuthSources,
435460
$allowedAuthSources
436461
));
437462

public/main/admin/user_edit.php

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,10 @@ function update_user_edit_password_section() {
229229
}
230230
}
231231

232+
$hasSingleAuthSource = 1 === count($authSources);
232233
$currentAuthSources = (array) ($userInfo['auth_sources'] ?? []);
233-
if (empty($currentAuthSources)) {
234-
$currentAuthSources = [UserAuthSource::PLATFORM];
234+
if ($hasSingleAuthSource || empty($currentAuthSources)) {
235+
$currentAuthSources = array_keys($authSources);
235236
}
236237
$showPasswordSection = in_array(UserAuthSource::PLATFORM, $currentAuthSources, true);
237238

@@ -243,32 +244,48 @@ function update_user_edit_password_section() {
243244
'</div>'
244245
);
245246

246-
$form->addElement(
247-
'select',
248-
'auth_source',
249-
get_lang('Authentication methods'),
250-
$authSources,
251-
[
252-
'id' => 'auth_source',
253-
'multiple' => 'multiple',
254-
'size' => max(3, min(count($authSources), 8)),
255-
'class' => 'w-full rounded-lg border border-gray-25 bg-white px-3 py-2 text-body-2 text-gray-90 focus:border-primary focus:outline-none focus:ring-1 focus:ring-primary',
256-
]
257-
);
258-
$form->addRule('auth_source', get_lang('Required field'), 'required');
247+
if ($hasSingleAuthSource) {
248+
$singleAuthSourceLabel = (string) reset($authSources);
249+
$form->addElement(
250+
'static',
251+
null,
252+
get_lang('Authentication methods'),
253+
htmlspecialchars($singleAuthSourceLabel, \ENT_QUOTES, 'UTF-8')
254+
);
255+
} else {
256+
$form->addElement(
257+
'select',
258+
'auth_source',
259+
get_lang('Authentication methods'),
260+
$authSources,
261+
[
262+
'id' => 'auth_source',
263+
'multiple' => 'multiple',
264+
'size' => max(3, min(count($authSources), 8)),
265+
'class' => 'w-full rounded-lg border border-gray-25 bg-white px-3 py-2 text-body-2 text-gray-90 focus:border-primary focus:outline-none focus:ring-1 focus:ring-primary',
266+
]
267+
);
268+
$form->addRule('auth_source', get_lang('Required field'), 'required');
269+
}
259270

260271
$form->addHtml(
261272
'<div id="user-edit-password-section" class="mt-6 rounded-xl border border-gray-25 bg-gray-5 p-4 '.($showPasswordSection ? '' : 'hidden').'">'.
262273
'<h4 class="mb-3 text-body-1 font-semibold text-gray-90">'.get_lang('Password').'</h4>'
263274
);
264275

265-
$form->addElement('radio', 'reset_password', null, get_lang('Don\'t reset password'), 0);
266-
$form->addElement('radio', 'reset_password', null, get_lang('Automatically generate a new password'), 1);
276+
$resetPasswordOptions = [
277+
0 => get_lang('Don\'t reset password'),
278+
1 => get_lang('Automatically generate a new password'),
279+
];
280+
281+
if ($adminsCanSetUsersPass) {
282+
$resetPasswordOptions[2] = get_lang('Set password manually');
283+
}
284+
285+
$form->addRadio('reset_password', null, $resetPasswordOptions);
267286

268287
if ($adminsCanSetUsersPass) {
269-
$group = [];
270-
$group[] = $form->createElement('radio', 'reset_password', null, get_lang('Set password manually'), 2);
271-
$group[] = $form->createElement(
288+
$form->addElement(
272289
'password',
273290
'password',
274291
null,
@@ -279,9 +296,7 @@ function update_user_edit_password_section() {
279296
'autocomplete' => 'new-password',
280297
]
281298
);
282-
283-
$form->addGroup($group, 'password', null, null, false);
284-
$form->addPasswordRule('password', 'password');
299+
$form->addPasswordRule('password');
285300
}
286301

287302
$form->addHtml('</div></div>');
@@ -439,7 +454,9 @@ function update_user_edit_password_section() {
439454

440455
// Set default values
441456
$user_data['reset_password'] = 0;
442-
$user_data['auth_source'] = $currentAuthSources;
457+
if (!$hasSingleAuthSource) {
458+
$user_data['auth_source'] = $currentAuthSources;
459+
}
443460

444461
if (!$hideFields) {
445462
$expiration_date = $user_data['expiration_date'];
@@ -492,8 +509,11 @@ function update_user_edit_password_section() {
492509
}
493510
}
494511
$allowedAuthSources = array_map('strval', array_keys($authSources));
512+
$submittedAuthSources = $hasSingleAuthSource
513+
? $allowedAuthSources
514+
: array_map('strval', (array) ($user['auth_source'] ?? []));
495515
$authSource = array_values(array_intersect(
496-
array_map('strval', (array) ($user['auth_source'] ?? [])),
516+
$submittedAuthSources,
497517
$allowedAuthSources
498518
));
499519

0 commit comments

Comments
 (0)