Skip to content

User: Enable roles combination - refs #5648 #6206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions public/main/admin/user_add.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,22 @@ function updateStatus(){
$status[STUDENT_BOSS] = get_lang('Student\'s superior');
$status[INVITEE] = get_lang('Invitee');

$form->addSelect(
'status',
get_lang('Profile'),
$status,
$form->addElement(
'select',
'roles',
get_lang('Roles'),
api_get_roles(),
[
'id' => 'status_select',
'onchange' => 'javascript: updateStatus();',
'multiple' => 'multiple',
'size' => 8,
]
);

//drh list (display only if student)
$display = (isset($_POST['status']) && STUDENT == $_POST['status']) || !isset($_POST['status']) ? 'block' : 'none';
$display = 'none';
if (isset($_POST['roles']) && is_array($_POST['roles'])) {
$display = in_array('ROLE_TEACHER', $_POST['roles']) || in_array('ROLE_SESSION_MANAGER', $_POST['roles']) ? 'block' : 'none';
}

if (api_is_platform_admin()) {
// Platform admin
Expand Down Expand Up @@ -354,7 +358,6 @@ function updateStatus(){
$email = $user['email'];
$phone = $user['phone'];
$username = 'true' !== api_get_setting('login_is_email') ? $user['username'] : '';
$status = (int) $user['status'];
$language = $user['locale'];
$picture = $_FILES['picture'];
$platform_admin = 0;
Expand Down Expand Up @@ -398,6 +401,24 @@ function updateStatus(){

$template = isset($user['email_template_option']) ? $user['email_template_option'] : [];

$roles = $user['roles'] ?? [];
$priorityRoles = [
'ROLE_SESSION_MANAGER' => SESSIONADMIN,
'ROLE_HR' => DRH,
'ROLE_TEACHER' => COURSEMANAGER,
'ROLE_STUDENT_BOSS' => STUDENT_BOSS,
'ROLE_INVITEE' => INVITEE,
'ROLE_STUDENT' => STUDENT,
];

$status = STUDENT;
foreach ($priorityRoles as $role => $roleStatus) {
if (in_array($role, $roles, true)) {
$status = $roleStatus;
break;
}
}

$user_id = UserManager::create_user(
$firstname,
$lastname,
Expand Down Expand Up @@ -456,6 +477,14 @@ function updateStatus(){
);
}

$repo = Container::getUserRepository();
$userEntity = $repo->find($user_id);

if ($userEntity) {
$userEntity->setRoles($roles);
$repo->updateUser($userEntity);
}

$extraFieldValues = new ExtraFieldValue('user');
$user['item_id'] = $user_id;
$extraFieldValues->saveFieldValues($user);
Expand Down
76 changes: 50 additions & 26 deletions public/main/admin/user_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,26 +250,21 @@ function confirmation(name) {
$form->addGroup($group, 'password', null, null, false);
$form->addPasswordRule('password', 'password');

// Status
$status = [];
$status[COURSEMANAGER] = get_lang('Trainer');
$status[STUDENT] = get_lang('Learner');
$status[DRH] = get_lang('Human Resources Manager');
$status[SESSIONADMIN] = get_lang('Sessions administrator');
$status[STUDENT_BOSS] = get_lang('Student\'s superior');
$status[INVITEE] = get_lang('Invitee');

$form->addSelect(
'status',
get_lang('Profile'),
$status,
$form->addElement(
'select',
'roles',
get_lang('Roles'),
api_get_roles(),
[
'id' => 'status_select',
'onchange' => 'javascript: display_drh_list();',
'multiple' => 'multiple',
'size' => 8,
]
);

$display = isset($user_data['status']) && (STUDENT == $user_data['status'] || (isset($_POST['status']) && STUDENT == $_POST['status'])) ? 'block' : 'none';
$display = 'none';
if (isset($_POST['roles']) && is_array($_POST['roles'])) {
$display = in_array('ROLE_TEACHER', $_POST['roles']) || in_array('ROLE_SESSION_MANAGER', $_POST['roles']) ? 'block' : 'none';
}

// Platform admin
if (api_is_platform_admin()) {
Expand Down Expand Up @@ -403,6 +398,9 @@ function confirmation(name) {
$user_data['expiration_date'] = api_get_local_time($expiration_date);
}
}
$availableRoles = array_keys(api_get_roles());
$userRoles = array_intersect($userObj->getRoles(), $availableRoles);
$user_data['roles'] = $userRoles;
$form->setDefaults($user_data);

$error_drh = false;
Expand All @@ -417,8 +415,8 @@ function confirmation(name) {
}

$is_user_subscribed_in_course = CourseManager::is_user_subscribed_in_course($user['user_id']);

if (DRH == $user['status'] && $is_user_subscribed_in_course) {
$roles = $user['roles'] ?? [];
if (in_array('ROLE_HR', $roles, true) && $is_user_subscribed_in_course) {
$error_drh = true;
} else {
$picture_element = $form->getElement('picture');
Expand All @@ -441,12 +439,14 @@ function confirmation(name) {
$lastname = $user['lastname'];
$firstname = $user['firstname'];
$password = $user['password'];
$auth_source = $user['auth_source'] ?? $userInfo['auth_source'];
$auth_source = $user['auth_source'] ?? $userInfo['auth_source'] ?? [];
if (!is_array($auth_source)) {
$auth_source = [$auth_source];
}
$official_code = $user['official_code'];
$email = $user['email'];
$phone = $user['phone'];
$username = $user['username'] ?? $userInfo['username'];
$status = (int) $user['status'];
$platform_admin = 0;
// Only platform admin can change user status to admin.
if (api_is_platform_admin()) {
Expand All @@ -463,17 +463,34 @@ function confirmation(name) {
}
$active = isset($user['active']) ? (int) $user['active'] : USER_SOFT_DELETED;

//If the user is set to admin the status will be overwrite by COURSEMANAGER = 1
if (1 == $platform_admin) {
$status = COURSEMANAGER;
}

if ('true' === api_get_setting('login_is_email')) {
$username = $email;
}

$template = $user['email_template_option'] ?? [];

$priorityRoles = [
'ROLE_SESSION_MANAGER' => SESSIONADMIN,
'ROLE_HR' => DRH,
'ROLE_TEACHER' => COURSEMANAGER,
'ROLE_STUDENT_BOSS' => STUDENT_BOSS,
'ROLE_INVITEE' => INVITEE,
'ROLE_STUDENT' => STUDENT,
];

$status = STUDENT;
foreach ($priorityRoles as $role => $roleStatus) {
if (in_array($role, $roles, true)) {
$status = $roleStatus;
break;
}
}

//If the user is set to admin the status will be overwrite by COURSEMANAGER = 1
if (1 == $platform_admin) {
$status = COURSEMANAGER;
}

UserManager::update_user(
$user_id,
$firstname,
Expand All @@ -499,7 +516,7 @@ function confirmation(name) {
$template
);

$studentBossListSent = isset($user['student_boss']) ? $user['student_boss'] : [];
$studentBossListSent = $user['student_boss'] ?? [];
UserManager::subscribeUserToBossList(
$user_id,
$studentBossListSent,
Expand All @@ -516,6 +533,13 @@ function confirmation(name) {
}
}

$repo = Container::getUserRepository();
$userEntity = $repo->find($user_id);
if ($userEntity) {
$userEntity->setRoles($roles);
$repo->updateUser($userEntity);
}

$extraFieldValue = new ExtraFieldValue('user');
$extraFieldValue->saveFieldValues($user);
$userInfo = api_get_user_info($user_id);
Expand Down
Loading
Loading