Skip to content
Merged
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
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
8 changes: 7 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,12 @@ 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
// Check if the field is nullable using the injection class method
if ($value === self::EMPTY_VALUE && !$add && (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
75 changes: 55 additions & 20 deletions inc/userinjection.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* -------------------------------------------------------------------------
*/


use function Safe\preg_split;

class PluginDatainjectionUserInjection extends User implements PluginDatainjectionInjectionInterface
{
Expand All @@ -55,14 +55,20 @@ public function connectedTo()

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 @@ public function getOptions($primary_type = '')
$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 @@ public function addSpecificNeededFields($primary_type, $values)
}


/**
* @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,30 @@ public function processAfterInsertOrUpdate($values, $add = true, $rights = [])
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);
foreach ($emails as $email) {
$email = trim($email);
if (filter_var($email, FILTER_VALIDATE_EMAIL) && !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