Skip to content

Commit e1993fa

Browse files
committed
Admin: Remove elements of user_add and user_edit that filled the deprecated admin table (roles are entirely defined by the user.roles column now) + improve forms display (button colors + move phone field in advanced settings)
1 parent 98d30c4 commit e1993fa

4 files changed

Lines changed: 45 additions & 155 deletions

File tree

public/main/admin/user_add.php

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@
3131
//$htmlHeadXtra[] = api_get_asset('cropper/dist/cropper.min.js');
3232
$htmlHeadXtra[] = '
3333
<script>
34-
$("#status_select").ready(function() {
35-
if ($(this).attr("value") != '.STUDENT.') {
36-
$("#id_platform_admin").hide();
37-
}
38-
});
3934
function enable_expiration_date() { //v2.0
4035
document.user_add.radio_expiration_date[0].checked=false;
4136
document.user_add.radio_expiration_date[1].checked=true;
@@ -49,24 +44,6 @@ function password_switch_radio_button() {
4944
}
5045
}
5146
}
52-
53-
var is_platform_id = "'.$is_platform_admin.'";
54-
55-
function updateStatus(){
56-
if (document.getElementById("status_select").value=='.STUDENT.') {
57-
if (is_platform_id == 1)
58-
document.getElementById("id_platform_admin").style.display="none";
59-
60-
} else if (document.getElementById("status_select").value=='.COURSEMANAGER.') {
61-
62-
if (is_platform_id == 1)
63-
document.getElementById("id_platform_admin").style.display="block";
64-
} else {
65-
66-
if (is_platform_id == 1)
67-
document.getElementById("id_platform_admin").style.display="none";
68-
}
69-
}
7047
</script>';
7148

7249
if (!empty($_GET['message'])) {
@@ -160,8 +137,6 @@ function updateStatus(){
160137
$form->addRule('email', get_lang('This login is already in use'), 'username_available');
161138
}
162139

163-
// Phone
164-
$form->addText('phone', get_lang('Phone number'), false, ['autocomplete' => 'off', 'id' => 'phone']);
165140
// Picture
166141
$form->addFile(
167142
'picture',
@@ -174,7 +149,7 @@ function updateStatus(){
174149

175150
// Username
176151
if ('true' !== api_get_setting('login_is_email')) {
177-
$form->addElement('text', 'username', get_lang('Login'), ['id' => 'username', 'maxlength' => User::USERNAME_MAX_LENGTH, 'autocomplete' => 'off']);
152+
$form->addElement('text', 'username', get_lang('Username'), ['id' => 'username', 'maxlength' => User::USERNAME_MAX_LENGTH, 'autocomplete' => 'off']);
178153
$form->addRule('username', get_lang('Required field'), 'required');
179154
$form->addRule('username', sprintf(get_lang('The login needs to be maximum %s characters long'), (string) User::USERNAME_MAX_LENGTH), 'maxlength', User::USERNAME_MAX_LENGTH);
180155
$form->addRule('username', get_lang('Only letters and numbers allowed'), 'username');
@@ -200,7 +175,7 @@ function updateStatus(){
200175
}
201176
if ($nb_ext_auth_source_added > 0) {
202177
$group[] = $form->createElement('radio', 'password_auto', null, get_lang('External authentification').' ', 2);
203-
$group[] = $form->createElement('select', 'auth_source', null, $auth_sources, ['multiple' => 'multiple']);
178+
$group[] = $form->createElement('select', 'auth_source', null, $auth_sources, ['multiple' => 'multiple', 'size' => 2]);;
204179
$group[] = $form->createElement('static', '', '', '<br />');
205180
}
206181
}
@@ -252,7 +227,7 @@ function updateStatus(){
252227
api_get_roles(),
253228
[
254229
'multiple' => 'multiple',
255-
'size' => 8,
230+
'size' => 9,
256231
]
257232
);
258233
$form->addRule('roles', get_lang('Required field'), 'required');
@@ -263,16 +238,6 @@ function updateStatus(){
263238
$display = in_array('ROLE_TEACHER', $_POST['roles']) || in_array('ROLE_SESSION_MANAGER', $_POST['roles']) ? 'block' : 'none';
264239
}
265240

266-
if (api_is_platform_admin()) {
267-
// Platform admin
268-
$group = [];
269-
$group[] = $form->createElement('radio', 'platform_admin', 'id="id_platform_admin"', get_lang('Yes'), 1);
270-
$group[] = $form->createElement('radio', 'platform_admin', 'id="id_platform_admin"', get_lang('No'), 0);
271-
$form->addElement('html', '<div id="id_platform_admin" style="display:'.$display.';">');
272-
$form->addGroup($group, 'admin', get_lang('Administration'));
273-
$form->addElement('html', '</div>');
274-
}
275-
276241
$form->addSelectLanguage('locale', get_lang('Language'));
277242

278243
// Send email
@@ -303,6 +268,10 @@ function updateStatus(){
303268
$advancedPanelOpen
304269
));
305270

271+
// Phone
272+
$form->addText('phone', get_lang('Phone number'), false, ['autocomplete' => 'off', 'id' => 'phone']);
273+
274+
306275
$extraField = new ExtraField('user');
307276
$returnParams = $extraField->addElements(
308277
$form,
@@ -337,7 +306,6 @@ function updateStatus(){
337306
</script>';
338307

339308
// Set default values
340-
$defaults['admin']['platform_admin'] = 0;
341309
$defaults['mail']['send_mail'] = 1;
342310
$defaults['password']['password_auto'] = 1;
343311
$defaults['active'] = 1;
@@ -352,8 +320,8 @@ function updateStatus(){
352320
$form->setDefaults($defaults);
353321

354322
// Submit button
355-
$html_results_enabled[] = $form->createElement('button', 'submit', get_lang('Add'), 'plus', 'primary');
356-
$html_results_enabled[] = $form->createElement('button', 'submit_plus', get_lang('Add').'+', 'plus', 'primary');
323+
$html_results_enabled[] = $form->createElement('button', 'submit', get_lang('Add'), 'plus', 'success');
324+
$html_results_enabled[] = $form->createElement('button', 'submit_plus', get_lang('Add').'+', 'plus', 'success');
357325
$form->addGroup($html_results_enabled);
358326

359327
// Validate form
@@ -369,10 +337,6 @@ function updateStatus(){
369337
$username = 'true' !== api_get_setting('login_is_email') ? $user['username'] : '';
370338
$language = $user['locale'];
371339
$picture = $_FILES['picture'];
372-
$platform_admin = 0;
373-
if (isset($user['admin']) && isset($user['admin']['platform_admin'])) {
374-
$platform_admin = (int) $user['admin']['platform_admin'];
375-
}
376340
$send_mail = 0;
377341
if (isset($user['mail']) && isset($user['mail']['send_mail'])) {
378342
$send_mail = (int) $user['mail']['send_mail'];
@@ -413,9 +377,6 @@ function updateStatus(){
413377
$roles = $user['roles'] ?? [];
414378
$roles = array_values(array_unique(array_map('api_normalize_role_code', $roles)));
415379
$status = api_status_from_roles($roles);
416-
if ((int) ($user['admin']['platform_admin'] ?? 0) === 1) {
417-
$status = COURSEMANAGER;
418-
}
419380

420381
$user_id = UserManager::create_user(
421382
$firstname,
@@ -435,7 +396,7 @@ function updateStatus(){
435396
$extra,
436397
null,
437398
$send_mail,
438-
$platform_admin,
399+
null,
439400
'',
440401
false,
441402
null,
@@ -484,12 +445,6 @@ function updateStatus(){
484445
$repo->updateUser($userEntity);
485446
}
486447

487-
if (api_has_admin_role($roles)) {
488-
UserManager::addUserAsAdmin($userEntity);
489-
} else {
490-
UserManager::removeUserAdmin($userEntity);
491-
}
492-
493448
$extraFieldValues = new ExtraFieldValue('user');
494449
$user['item_id'] = $user_id;
495450
$extraFieldValues->saveFieldValues($user);

public/main/admin/user_edit.php

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
$user_id = isset($_GET['user_id']) ? (int) $_GET['user_id'] : (int) $_POST['user_id'];
2222
api_protect_super_admin($user_id, null, true);
2323
$is_platform_admin = api_is_platform_admin() ? 1 : 0;
24-
$userInfo = api_get_user_info($user_id);
24+
$userInfo = api_get_user_info($user_id, null, true);
2525
$userObj = api_get_user_entity($user_id);
2626
$illustrationRepo = Container::getIllustrationRepository();
2727

@@ -32,8 +32,6 @@
3232

3333
$htmlHeadXtra[] = '
3434
<script>
35-
var is_platform_id = "'.$is_platform_admin.'";
36-
3735
function enable_expiration_date() {
3836
document.user_edit.radio_expiration_date[0].checked=false;
3937
document.user_edit.radio_expiration_date[1].checked=true;
@@ -48,22 +46,6 @@ function password_switch_radio_button(){
4846
}
4947
}
5048
51-
function display_drh_list(){
52-
var $radios = $("input:radio[name=platform_admin]");
53-
if (document.getElementById("status_select").value=='.COURSEMANAGER.') {
54-
if (is_platform_id == 1)
55-
document.getElementById("id_platform_admin").style.display="block";
56-
} else if (document.getElementById("status_select").value=='.STUDENT.') {
57-
if (is_platform_id == 1)
58-
document.getElementById("id_platform_admin").style.display="none";
59-
$radios.filter("[value=0]").attr("checked", true);
60-
} else {
61-
if (is_platform_id == 1)
62-
document.getElementById("id_platform_admin").style.display="none";
63-
$radios.filter("[value=0]").attr("checked", true);
64-
}
65-
}
66-
6749
function show_image(image,width,height) {
6850
width = parseInt(width) + 20;
6951
height = parseInt(height) + 20;
@@ -85,20 +67,12 @@ function confirmation(name) {
8567
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
8668
$interbreadcrumb[] = ['url' => 'user_list.php', 'name' => get_lang('User list')];
8769

88-
$table_user = Database::get_main_table(TABLE_MAIN_USER);
89-
$table_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
90-
$sql = "SELECT u.*, a.user_id AS is_admin FROM $table_user u
91-
LEFT JOIN $table_admin a ON a.user_id = u.id
92-
WHERE u.id = '".$user_id."'";
93-
$res = Database::query($sql);
94-
if (1 != Database::num_rows($res)) {
70+
if (false === $userInfo) {
9571
header('Location: user_list.php');
9672
exit;
9773
}
9874

99-
$user_data = Database::fetch_assoc($res);
100-
101-
$user_data['platform_admin'] = is_null($user_data['is_admin']) ? 0 : 1;
75+
$user_data = $userInfo;
10276
$user_data['send_mail'] = 0;
10377
$user_data['old_password'] = $user_data['password'];
10478
//Convert the registration date of the user
@@ -269,19 +243,6 @@ function confirmation(name) {
269243
$display = (in_array('ROLE_TEACHER', $norm, true) || in_array('ROLE_SESSION_MANAGER', $norm, true)) ? 'block' : 'none';
270244
}
271245

272-
// Platform admin
273-
if (api_is_platform_admin()) {
274-
$group = [];
275-
$group[] = $form->createElement('radio', 'platform_admin', null, get_lang('Yes'), 1);
276-
$group[] = $form->createElement('radio', 'platform_admin', null, get_lang('No'), 0);
277-
278-
1 == $user_data['status'] ? $display = 'block' : $display = 'none';
279-
280-
$form->addElement('html', '<div id="id_platform_admin" style="display:'.$display.'">');
281-
$form->addGroup($group, 'admin', get_lang('Administration'), null, false);
282-
$form->addElement('html', '</div>');
283-
}
284-
285246
//Language
286247
$form->addSelectLanguage('locale', get_lang('Language'));
287248

@@ -381,7 +342,7 @@ function confirmation(name) {
381342
});
382343
</script>';
383344

384-
// Freeze user conditions, admin cannot updated them
345+
// Freeze user conditions, admin cannot update them
385346
$extraConditions = api_get_setting('profile.show_conditions_to_user', true);
386347
if ($extraConditions && isset($extraConditions['conditions'])) {
387348
$extraConditions = $extraConditions['conditions'];
@@ -471,11 +432,6 @@ function confirmation(name) {
471432
$email = $user['email'];
472433
$phone = $user['phone'];
473434
$username = $user['username'] ?? $userInfo['username'];
474-
$platform_admin = 0;
475-
// Only platform admin can change user status to admin.
476-
if (api_is_platform_admin()) {
477-
$platform_admin = (int) $user['platform_admin'];
478-
}
479435
$send_mail = (int) $user['send_mail'];
480436
$reset_password = (int) $user['reset_password'];
481437
$hr_dept_id = isset($user['hr_dept_id']) ? intval($user['hr_dept_id']) : null;
@@ -492,9 +448,6 @@ function confirmation(name) {
492448
}
493449

494450
$template = $user['email_template_option'] ?? [];
495-
if ((int) ($user['platform_admin'] ?? 0) === 1) {
496-
$newStatus = COURSEMANAGER;
497-
}
498451

499452
$incompatible = false;
500453
$conflicts = [];
@@ -576,16 +529,6 @@ function confirmation(name) {
576529
true
577530
);
578531

579-
$currentUserId = api_get_user_id();
580-
581-
if ($user_id != $currentUserId) {
582-
if (1 == $platform_admin) {
583-
UserManager::addUserAsAdmin($userObj);
584-
} else {
585-
UserManager::removeUserAdmin($userObj);
586-
}
587-
}
588-
589532
$repo = Container::getUserRepository();
590533
/* @var User $userEntity */
591534
$userEntity = $repo->find($user_id);
@@ -594,12 +537,6 @@ function confirmation(name) {
594537
$repo->updateUser($userEntity);
595538
}
596539

597-
if (api_has_admin_role($roles)) {
598-
UserManager::addUserAsAdmin($userEntity);
599-
} else {
600-
UserManager::removeUserAdmin($userEntity);
601-
}
602-
603540
$extraFieldValue = new ExtraFieldValue('user');
604541
$extraFieldValue->saveFieldValues($user);
605542
$userInfo = api_get_user_info($user_id);

public/main/inc/lib/usermanager.lib.php

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131
class UserManager
3232
{
33-
// This constants are deprecated use the constants located in ExtraField
33+
// These constants are deprecated use the constants located in ExtraField
3434
public const USER_FIELD_TYPE_TEXT = 1;
3535
public const USER_FIELD_TYPE_TEXTAREA = 2;
3636
public const USER_FIELD_TYPE_RADIO = 3;
@@ -77,39 +77,43 @@ public static function updatePassword($userId, $password)
7777
/**
7878
* Creates a new user for the platform.
7979
*
80-
* @param string $firstName
81-
* @param string $lastName
82-
* @param int $status (1 for course tutor, 5 for student, 6 for anonymous)
83-
* @param string $email
84-
* @param string $loginName
85-
* @param string $password
86-
* @param string $officialCode Any official code (optional)
87-
* @param string $language User language (optional)
88-
* @param string $phone Phone number (optional)
89-
* @param string $pictureUri Picture URI (optional)
90-
* @param string $authSources Authentication source (defaults to 'platform', dependind on constant)
91-
* @param string $expirationDate Account expiration date (optional, defaults to null)
92-
* @param int $active Whether the account is enabled or disabled by default
93-
* @param int $hrDeptId The department of HR in which the user is registered (defaults to 0)
94-
* @param array $extra Extra fields (labels must be prefixed by "extra_")
95-
* @param string $encryptMethod Used if password is given encrypted. Set to an empty string by default
96-
* @param bool $sendMail
97-
* @param bool $isAdmin
98-
* @param string $address
99-
* @param bool $sendEmailToAllAdmins
100-
* @param FormValidator $form
101-
* @param int $creatorId
102-
* @param array $emailTemplate
103-
* @param string $redirectToURLAfterLogin
80+
* @param string $firstName
81+
* @param string $lastName
82+
* @param int $status (1 for course tutor, 5 for student, 6 for anonymous)
83+
* @param string $email
84+
* @param string $loginName
85+
* @param string $password
86+
* @param string $officialCode Any official code (optional)
87+
* @param string $language User language (optional)
88+
* @param string $phone Phone number (optional)
89+
* @param string $pictureUri Picture URI (optional)
90+
* @param ?array $authSources Authentication source (defaults to 'platform', dependind on constant)
91+
* @param string $expirationDate Account expiration date (optional, defaults to null)
92+
* @param int $active Whether the account is enabled or disabled by default
93+
* @param int $hrDeptId The department of HR in which the user is registered (defaults to 0)
94+
* @param array $extra Extra fields (labels must be prefixed by "extra_")
95+
* @param string $encryptMethod Used if password is given encrypted. Set to an empty string by default
96+
* @param bool $sendMail
97+
* @param bool $isAdmin
98+
* @param string $address
99+
* @param bool $sendEmailToAllAdmins
100+
* @param null $form
101+
* @param int $creatorId
102+
* @param array $emailTemplate
103+
* @param string $redirectToURLAfterLogin
104104
*
105105
* @return mixed new user id - if the new user creation succeeds, false otherwise
106+
* @throws \Doctrine\DBAL\Exception
107+
* @throws \Doctrine\ORM\Exception\ORMException
108+
* @throws \Twig\Error\LoaderError
109+
* @throws \Twig\Error\RuntimeError
110+
* @throws \Twig\Error\SyntaxError
106111
* @desc The function tries to retrieve user id from the session.
107112
* If it exists, the current user id is the creator id. If a problem arises,
108113
* @assert ('Sam','Gamegie',5,'sam@example.com','jo','jo') > 1
109114
* @assert ('Pippin','Took',null,null,'jo','jo') === false
110-
*@author Hugues Peeters <peeters@ipm.ucl.ac.be>,
115+
* @author Hugues Peeters <peeters@ipm.ucl.ac.be>,
111116
* @author Roan Embrechts <roan_embrechts@yahoo.com>
112-
*
113117
*/
114118
public static function create_user(
115119
$firstName,

public/main/install/install.lib.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,12 +1556,6 @@ function finishInstallationWithContainer(
15561556
$admin->setRoles($roles);
15571557
$repo->updateUser($admin);
15581558

1559-
if (in_array('ROLE_ADMIN', $roles, true) || in_array('ROLE_GLOBAL_ADMIN', $roles, true)) {
1560-
UserManager::addUserAsAdmin($admin);
1561-
} else {
1562-
UserManager::removeUserAdmin($admin);
1563-
}
1564-
15651559
/** @var User $anonUser */
15661560
$anonUser = $repo->findOneBy(['username' => 'anon']);
15671561
if ($anonUser) {

0 commit comments

Comments
 (0)