Accept user ID as authoratative field for updates

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2023-06-15 10:02:05 +01:00
parent 157a4341ea
commit 6c684bccd0
2 changed files with 13 additions and 7 deletions

View file

@ -288,6 +288,7 @@ class Importer extends Component
];
$this->users_fields = [
'id' => trans('general.id'),
'company' => trans('general.company'),
'location' => trans('general.location'),
'department' => trans('general.department'),

View file

@ -42,6 +42,7 @@ class UserImporter extends ItemImporter
public function createUserIfNotExists(array $row)
{
// Pull the records from the CSV to determine their values
$this->item['id'] = $this->findCsvMatch($row, 'id');
$this->item['username'] = $this->findCsvMatch($row, 'username');
$this->item['first_name'] = $this->findCsvMatch($row, 'first_name');
$this->item['last_name'] = $this->findCsvMatch($row, 'last_name');
@ -77,12 +78,17 @@ class UserImporter extends ItemImporter
$this->item['username'] = $user_formatted_array['username'];
}
// Check if a numeric ID was passed. If it does, use that above all else.
if ((array_key_exists('id', $this->item) && ($this->item['id'] != "") && (is_numeric($this->item['id'])))) {
$user = User::find($this->item['id']);
} else {
$user = User::where('username', $this->item['username'])->first();
if ($user) {
if (! $this->updating) {
$this->log('A matching User '.$this->item['name'].' already exists. ');
\Log::debug('A matching User '.$this->item['name'].' already exists. ');
}
if ($user) {
if (! $this->updating) {
\Log::debug('A matching User '.$this->item['name'].' already exists. ');
return;
}
$this->log('Updating User');
@ -109,7 +115,6 @@ class UserImporter extends ItemImporter
$user->fill($this->sanitizeItemForStoring($user));
if ($user->save()) {
// $user->logCreate('Imported using CSV Importer');
$this->log('User '.$this->item['name'].' was created');
if (($user->email) && ($user->activated == '1')) {