Skip to content

Commit 35a058c

Browse files
authored
Fix - User field updates and email import (#566)
* Fix - User field updates and email import * Fix Lints
1 parent 4563890 commit 35a058c

File tree

3 files changed

+63
-22
lines changed

3 files changed

+63
-22
lines changed

inc/clientinjection.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public static function showResultsForm(PluginDatainjectionModel $model)
312312
$data = [
313313
'ok' => $ok,
314314
'from_url' => $from_url,
315-
'popup_url' => plugin_datainjection_geturl() . "front/popup.php?popup=log&models_id=" . $model->fields['id'],
315+
'popup_url' => plugin_datainjection_geturl() . "front/popup.php?popup=log&models_id=" . $model->fields['id'],
316316
'model_id' => $model->fields['id'],
317317
'has_pdf' => $plugin->isActivated('pdf'),
318318
'has_errors' => !empty($error_lines),

inc/commoninjectionlib.class.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ private function setValueForItemtype($itemtype, $field, $value, $fromdb = false)
944944
'is_deleted',
945945
'is_active',
946946
];
947-
if (empty($value)) {
947+
if (empty($value) && $value !== 0 && $value !== '0') {
948948
if (isForeignKeyField($field) || (str_contains($field, 'is_')) || (method_exists($injectionClass, 'isNullable') && !$injectionClass->isNullable($field))) {
949949
// If the field is an id, we set it to 0
950950
$this->values[$itemtype][$field] = self::DROPDOWN_EMPTY_VALUE;
@@ -1650,6 +1650,12 @@ private function effectiveAddOrUpdate($injectionClass, $item, $values, $add = tr
16501650
//If field is a dropdown and value is '', then replace it by 0
16511651
continue;
16521652
} else {
1653+
// Skip empty values for fields that cannot accept empty strings during updates
1654+
// Check if the field is nullable using the injection class method
1655+
if ($value === self::EMPTY_VALUE && !$add && (method_exists($injectionClass, 'isNullable') && !$injectionClass->isNullable($key))) {
1656+
// Skip this field during update if value is empty and field is not nullable
1657+
continue;
1658+
}
16531659
$toinject[$key] = $value;
16541660
}
16551661

inc/userinjection.class.php

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* -------------------------------------------------------------------------
2929
*/
3030

31-
31+
use function Safe\preg_split;
3232

3333
class PluginDatainjectionUserInjection extends User implements PluginDatainjectionInjectionInterface
3434
{
@@ -55,14 +55,20 @@ public function connectedTo()
5555

5656
public function isNullable($field)
5757
{
58-
return in_array($field, [
59-
'begin_date',
60-
'date_sync',
61-
'end_date',
62-
'last_login',
63-
'substitution_end_date',
64-
'substitution_start_date',
65-
]);
58+
// Fields that cannot accept empty string values and should use their default values instead
59+
$non_nullable_fields = [
60+
'use_mode',
61+
'highcontrast_css',
62+
'default_central_tab',
63+
'entities_id',
64+
'profiles_id',
65+
'is_active',
66+
'is_deleted',
67+
'authtype',
68+
'auths_id',
69+
];
70+
71+
return !in_array($field, $non_nullable_fields);
6672
}
6773

6874

@@ -86,6 +92,8 @@ public function getOptions($primary_type = '')
8692
$tab[4]['displaytype'] = 'password';
8793

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

9098
//To manage groups : relies on a CommonDBRelation object !
9199
$tab[100]['name'] = __s('Group');
@@ -153,6 +161,20 @@ public function addSpecificNeededFields($primary_type, $values)
153161
}
154162

155163

164+
/**
165+
* @param array $values
166+
*/
167+
public function reformat(&$values)
168+
{
169+
// Remove token fields to avoid double encryption and length issues
170+
$tokens = ['password_forget_token', 'personal_token', 'api_token', 'cookie_token'];
171+
foreach ($tokens as $token) {
172+
if (isset($values['User'][$token])) {
173+
unset($values['User'][$token]);
174+
}
175+
}
176+
}
177+
156178
/**
157179
* @param array $values
158180
* @param boolean $add (true by default)
@@ -164,17 +186,30 @@ public function processAfterInsertOrUpdate($values, $add = true, $rights = [])
164186
global $DB;
165187

166188
//Manage user emails
167-
if (isset($values['User']['useremails_id']) && $rights['add_dropdown'] && Session::haveRight('user', UPDATE) && !countElementsInTable(
168-
"glpi_useremails",
169-
[
170-
'users_id' => $values['User']['id'],
171-
'email' => $values['User']['useremails_id'],
172-
],
173-
)) {
174-
$useremail = new UserEmail();
175-
$tmp['users_id'] = $values['User']['id'];
176-
$tmp['email'] = $values['User']['useremails_id'];
177-
$useremail->add($tmp);
189+
if (isset($values['User']['useremails_id']) && $rights['add_dropdown'] && Session::haveRight('user', UPDATE)) {
190+
$emails = preg_split('/[\s,;]+/', $values['User']['useremails_id'], -1, PREG_SPLIT_NO_EMPTY);
191+
foreach ($emails as $email) {
192+
$email = trim($email);
193+
if (filter_var($email, FILTER_VALIDATE_EMAIL) && !countElementsInTable(
194+
"glpi_useremails",
195+
[
196+
'users_id' => $values['User']['id'],
197+
'email' => $email,
198+
],
199+
)) {
200+
$useremail = new UserEmail();
201+
$tmp = [
202+
'users_id' => $values['User']['id'],
203+
'email' => $email,
204+
'is_default' => 0,
205+
];
206+
// If user has no emails, set this one as default
207+
if (!countElementsInTable("glpi_useremails", ['users_id' => $values['User']['id']])) {
208+
$tmp['is_default'] = 1;
209+
}
210+
$useremail->add($tmp);
211+
}
212+
}
178213
}
179214

180215
if (isset($values['User']['password']) && ($values['User']['password'] != '')) {

0 commit comments

Comments
 (0)