Skip to content

Commit f890e1f

Browse files
authored
Merge pull request #13171 from snipe/features/importer/allow_update_by_user_id
Accept user ID as authoratative field for updates
2 parents 7a425f8 + 6c684bc commit f890e1f

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

app/Http/Livewire/Importer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ public function mount()
288288
];
289289

290290
$this->users_fields = [
291+
'id' => trans('general.id'),
291292
'company' => trans('general.company'),
292293
'location' => trans('general.location'),
293294
'department' => trans('general.department'),

app/Importer/UserImporter.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ protected function handle($row)
4242
public function createUserIfNotExists(array $row)
4343
{
4444
// Pull the records from the CSV to determine their values
45+
$this->item['id'] = $this->findCsvMatch($row, 'id');
4546
$this->item['username'] = $this->findCsvMatch($row, 'username');
4647
$this->item['first_name'] = $this->findCsvMatch($row, 'first_name');
4748
$this->item['last_name'] = $this->findCsvMatch($row, 'last_name');
@@ -76,13 +77,18 @@ public function createUserIfNotExists(array $row)
7677
$user_formatted_array = User::generateFormattedNameFromFullName($user_full_name, Setting::getSettings()->username_format);
7778
$this->item['username'] = $user_formatted_array['username'];
7879
}
79-
80-
$user = User::where('username', $this->item['username'])->first();
80+
81+
// Check if a numeric ID was passed. If it does, use that above all else.
82+
if ((array_key_exists('id', $this->item) && ($this->item['id'] != "") && (is_numeric($this->item['id'])))) {
83+
$user = User::find($this->item['id']);
84+
} else {
85+
$user = User::where('username', $this->item['username'])->first();
86+
}
87+
8188
if ($user) {
89+
8290
if (! $this->updating) {
83-
$this->log('A matching User '.$this->item['name'].' already exists. ');
8491
\Log::debug('A matching User '.$this->item['name'].' already exists. ');
85-
8692
return;
8793
}
8894
$this->log('Updating User');
@@ -109,7 +115,6 @@ public function createUserIfNotExists(array $row)
109115
$user->fill($this->sanitizeItemForStoring($user));
110116

111117
if ($user->save()) {
112-
// $user->logCreate('Imported using CSV Importer');
113118
$this->log('User '.$this->item['name'].' was created');
114119

115120
if (($user->email) && ($user->activated == '1')) {

0 commit comments

Comments
 (0)