Rearrange some of the user creation logic

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2023-04-18 16:34:10 -07:00
parent 003cd372dd
commit 345662c5c2

View file

@ -283,7 +283,7 @@ abstract class Importer
* @return User Model w/ matching name * @return User Model w/ matching name
* @internal param array $user_array User details parsed from csv * @internal param array $user_array User details parsed from csv
*/ */
protected function createOrFetchUser($row, $type = null) protected function createOrFetchUser($row, $type = 'user')
{ {
$user_array = [ $user_array = [
@ -298,22 +298,22 @@ abstract class Importer
if ($type == 'manager') { if ($type == 'manager') {
\Log::debug('Type = manager'); $user_array['full_name'] = $this->findCsvMatch($row, 'manager');
$user_array = [ $user_array['username'] = $this->findCsvMatch($row, 'manager_username');
'full_name' => $this->findCsvMatch($row, 'manager'),
'username' => $this->findCsvMatch($row, 'manager_username'),
];
} }
// Maybe we're lucky and the user already exists. // Maybe we're lucky and the username was passed and it already exists.
if (!empty($user_array['username'])) {
if ($user = User::where('username', $user_array['username'])->first()) { if ($user = User::where('username', $user_array['username'])->first()) {
$this->log('User '.$user_array['username'].' already exists'); $this->log('User '.$user_array['username'].' already exists');
return $user; return $user;
} }
}
// If the full name is empty, bail out--we need this to extract first name (at the very least)
if (empty($user_array['full_name'])) { // If the full name and username is empty, bail out--we need this to extract first name (at the very least)
$this->log('Insufficient user data provided (Full name is required) - skipping user creation.'); if ((empty($user_array['username'])) && (empty($user_array['full_name']))) {
$this->log('Insufficient user data provided (Full name or username is required) - skipping user creation.');
\Log::debug(print_r($user_array, true)); \Log::debug(print_r($user_array, true));
\Log::debug(print_r($row, true)); \Log::debug(print_r($row, true));
return false; return false;
@ -325,6 +325,7 @@ abstract class Importer
$user_array['email'] = User::generateEmailFromFullName($user_array['full_name']); $user_array['email'] = User::generateEmailFromFullName($user_array['full_name']);
} }
// Get some fields for first name and last name based off of full name
$user_formatted_array = User::generateFormattedNameFromFullName($user_array['full_name'], Setting::getSettings()->username_format); $user_formatted_array = User::generateFormattedNameFromFullName($user_array['full_name'], Setting::getSettings()->username_format);
$user_array['first_name'] = $user_formatted_array['first_name']; $user_array['first_name'] = $user_formatted_array['first_name'];
$user_array['last_name'] = $user_formatted_array['last_name']; $user_array['last_name'] = $user_formatted_array['last_name'];
@ -334,22 +335,20 @@ abstract class Importer
if ($this->usernameFormat == 'email') { if ($this->usernameFormat == 'email') {
$user_array['username'] = $user_array['email']; $user_array['username'] = $user_array['email'];
} }
}
// Does this ever actually fire?? // Check for a matching username one more time after trying to guess username.
// Check for a matching user after trying to guess username.
if ($user = User::where('username', $user_array['username'])->first()) { if ($user = User::where('username', $user_array['username'])->first()) {
$this->log('User '.$user_array['username'].' already exists'); $this->log('User '.$user_array['username'].' already exists');
return $user; return $user;
} }
}
// If at this point we have not found a username or first name, bail out in shame. // If at this point we have not found a username or first name, bail out in shame.
if (empty($user_array['username']) || empty($user_array['first_name'])) { if (empty($user_array['username']) || empty($user_array['first_name'])) {
return false; return false;
} }
// No Luck, let's create one. // No luck finding a user on username or first name, let's create one.
$user = new User; $user = new User;
$user->first_name = $user_array['first_name']; $user->first_name = $user_array['first_name'];
$user->last_name = $user_array['last_name']; $user->last_name = $user_array['last_name'];
@ -364,9 +363,9 @@ abstract class Importer
if ($user->save()) { if ($user->save()) {
$this->log('User '.$user_array['username'].' created'); $this->log('User '.$user_array['username'].' created');
return $user; return $user;
} }
$this->logError($user, 'User "'.$user_array['username'].'" was not able to be created.'); $this->logError($user, 'User "'.$user_array['username'].'" was not able to be created.');
return false; return false;