Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion inc/clientinjection.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public static function showResultsForm(PluginDatainjectionModel $model)
$data = [
'ok' => $ok,
'from_url' => $from_url,
'popup_url' => plugin_datainjection_geturl() . "front/popup.php?popup=log&models_id=" . $model->fields['id'],
'popup_url' => plugin_datainjection_geturl() . "front/popup.php?popup=log&models_id=" . $model->fields['id'],
'model_id' => $model->fields['id'],
'has_pdf' => $plugin->isActivated('pdf'),
'has_errors' => !empty($error_lines),
Expand Down
10 changes: 9 additions & 1 deletion inc/commoninjectionlib.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ private function setValueForItemtype($itemtype, $field, $value, $fromdb = false)
'is_deleted',
'is_active',
];
if (empty($value)) {
if (empty($value) && $value !== 0 && $value !== '0') {
if (isForeignKeyField($field) || (str_contains($field, 'is_')) || (method_exists($injectionClass, 'isNullable') && !$injectionClass->isNullable($field))) {
// If the field is an id, we set it to 0
$this->values[$itemtype][$field] = self::DROPDOWN_EMPTY_VALUE;
Expand Down Expand Up @@ -1650,6 +1650,14 @@ private function effectiveAddOrUpdate($injectionClass, $item, $values, $add = tr
//If field is a dropdown and value is '', then replace it by 0
continue;
} else {
// Skip empty values for fields that cannot accept empty strings during updates
if ($value === self::EMPTY_VALUE && !$add) {
// Check if the field is nullable using the injection class method
if (method_exists($injectionClass, 'isNullable') && !$injectionClass->isNullable($key)) {
// Skip this field during update if value is empty and field is not nullable
continue;
}
}
$toinject[$key] = $value;
}

Expand Down
77 changes: 58 additions & 19 deletions inc/userinjection.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,20 @@

public function isNullable($field)
{
return in_array($field, [
'begin_date',
'date_sync',
'end_date',
'last_login',
'substitution_end_date',
'substitution_start_date',
]);
// Fields that cannot accept empty string values and should use their default values instead
$non_nullable_fields = [
'use_mode',
'highcontrast_css',
'default_central_tab',
'entities_id',
'profiles_id',
'is_active',
'is_deleted',
'authtype',
'auths_id'
];

return !in_array($field, $non_nullable_fields);
}


Expand All @@ -86,6 +92,8 @@
$tab[4]['displaytype'] = 'password';

$tab[5]['displaytype'] = 'text';
// Add email option to make it importable with the correct linkfield
$tab[5]['linkfield'] = 'useremails_id'; // Map email field to useremails_id for injection

//To manage groups : relies on a CommonDBRelation object !
$tab[100]['name'] = __s('Group');
Expand Down Expand Up @@ -153,6 +161,20 @@
}


/**
* @param array $values
*/
public function reformat(&$values)
{
// Remove token fields to avoid double encryption and length issues
$tokens = ['password_forget_token', 'personal_token', 'api_token', 'cookie_token'];
foreach ($tokens as $token) {
if (isset($values['User'][$token])) {
unset($values['User'][$token]);
}
}
}

/**
* @param array $values
* @param boolean $add (true by default)
Expand All @@ -164,17 +186,34 @@
global $DB;

//Manage user emails
if (isset($values['User']['useremails_id']) && $rights['add_dropdown'] && Session::haveRight('user', UPDATE) && !countElementsInTable(
"glpi_useremails",
[
'users_id' => $values['User']['id'],
'email' => $values['User']['useremails_id'],
],
)) {
$useremail = new UserEmail();
$tmp['users_id'] = $values['User']['id'];
$tmp['email'] = $values['User']['useremails_id'];
$useremail->add($tmp);
if (isset($values['User']['useremails_id']) && $rights['add_dropdown'] && Session::haveRight('user', UPDATE)) {
$emails = preg_split('/[\s,;]+/', $values['User']['useremails_id'], -1, PREG_SPLIT_NO_EMPTY);

Check failure on line 190 in inc/userinjection.class.php

View workflow job for this annotation

GitHub Actions / GLPI 11.0.x - php:8.5 - mariadb:11.8 / Continuous integration

Function preg_split is unsafe to use. It can return FALSE instead of throwing an exception. Please add 'use function Safe\preg_split;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library.

Check failure on line 190 in inc/userinjection.class.php

View workflow job for this annotation

GitHub Actions / GLPI 11.0.x - php:8.2 - mariadb:10.6 / Continuous integration

Function preg_split is unsafe to use. It can return FALSE instead of throwing an exception. Please add 'use function Safe\preg_split;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library.
foreach ($emails as $email) {
$email = trim($email);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (!countElementsInTable(
"glpi_useremails",
[
'users_id' => $values['User']['id'],
'email' => $email,
],
)) {
$useremail = new UserEmail();
$tmp = [
'users_id' => $values['User']['id'],
'email' => $email,
'is_default' => 0
];

// If user has no emails, set this one as default
if (!countElementsInTable("glpi_useremails", ['users_id' => $values['User']['id']])) {
$tmp['is_default'] = 1;
}

$useremail->add($tmp);
}
}
}
}

if (isset($values['User']['password']) && ($values['User']['password'] != '')) {
Expand Down
Loading