Skip to content

A free open source IT asset/license management system

WIP: Better handle activation column in importer (#6290)

snipef587d2248b76
diff --git a/app/Importer/Importer.php b/app/Importer/Importer.php
index a7c8e61154..7304c10faa 100644
--- a/app/Importer/Importer.php
+++ b/app/Importer/Importer.php
@@ -29,6 +29,7 @@ abstract class Importer
*/
private $defaultFieldMap = [
'asset_tag' => 'asset tag',
+ 'activated' => 'activated',
'category' => 'category',
'checkout_class' => 'checkout type', // Supports Location or User for assets. Using checkout_class instead of checkout_type because type exists on asset already.
'checkout_location' => 'checkout location',
@@ -181,7 +182,7 @@ abstract class Importer
$val = $default;
$key = $this->lookupCustomKey($key);
- $this->log("Custom Key: ${key}");
+ // $this->log("Custom Key: ${key}");
if (array_key_exists($key, $array)) {
$val = Encoding::toUTF8(trim($array[ $key ]));
}
@@ -249,7 +250,10 @@ abstract class Importer
}
/**
- * Finds the user matching given data, or creates a new one if there is no match
+ * Finds the user matching given data, or creates a new one if there is no match.
+ * This is NOT used by the User Import, only for Asset/Accessory/etc where
+ * there are users listed and we have to create them and associate them at
+ * the same time. [ALG]
*
* @author Daniel Melzter
* @since 3.0
@@ -262,8 +266,11 @@ abstract class Importer
$user_array = [
'full_name' => $this->findCsvMatch($row, "full_name"),
'email' => $this->findCsvMatch($row, "email"),
- 'username' => $this->findCsvMatch($row, "username")
+ 'username' => $this->findCsvMatch($row, "username"),
+ 'activated' => $this->fetchHumanBoolean($this->findCsvMatch($row, 'activated')),
];
+ \Log::debug('Importer.php Activated: '.$this->findCsvMatch($row, 'activated'));
+
// 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'])) {
$this->log('Insufficient user data provided (Full name is required)- skipping user creation, just adding asset');
@@ -291,8 +298,9 @@ abstract class Importer
}
}
+ // Does this ever actually fire??
// 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');
return $user;
}
@@ -310,10 +318,13 @@ abstract class Importer
$user->email = $user_array['email'];
$user->manager_id = $user_array['manager_id'];
$user->department_id = $user_array['department_id'];
- $user->activated = 1;
+ $user->activated = $user_array['activated'];
$user->password = $this->tempPassword;
+ \Log::debug('Creating a user with the following attributes: '.print_r($user_array, true));
+
if ($user->save()) {
+ \Log::debug('Importer.php Name: '.$user->first_name.' '.$user->last_name.' ('.$user->username.')');
$this->log('User '.$user_array['username'].' created');
return $user;
}
@@ -424,4 +435,13 @@ abstract class Importer
return $this;
}
+
+ public function fetchHumanBoolean($value)
+ {
+ if (($value =='1') || (strtolower($value) =='true') || (strtolower($value) =='yes'))
+ {
+ return '1';
+ }
+ return '0';
+ }
}
diff --git a/app/Importer/UserImporter.php b/app/Importer/UserImporter.php
index 0f9d29db23..4d006d1397 100644
--- a/app/Importer/UserImporter.php
+++ b/app/Importer/UserImporter.php
@@ -6,6 +6,15 @@ use App\Helpers\Helper;
use App\Models\User;
use App\Notifications\WelcomeNotification;
+/**
+ * This is ONLY used for the User Import. When we are importing users
+ * via an Asset/etc import, we use createOrFetchUser() in
+ * App\Importer.php. [ALG]
+ *
+ * Class UserImporter
+ * @package App\Importer
+ *
+ */
class UserImporter extends ItemImporter
{
protected $users;
@@ -30,13 +39,19 @@ class UserImporter extends ItemImporter
*/
public function createUserIfNotExists(array $row)
{
+ // Pull the records from the CSV to determine their values
$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');
+ \Log::debug('UserImporter.php Name: '.$this->item['first_name'].' '.$this->item['last_name'].' ('.$this->item['username'].')');
$this->item['email'] = $this->findCsvMatch($row, 'email');
$this->item['phone'] = $this->findCsvMatch($row, 'phone_number');
$this->item['jobtitle'] = $this->findCsvMatch($row, 'jobtitle');
- $this->item['activated'] = $this->findCsvMatch($row, 'activated');
+ $this->item['activated'] = $this->fetchHumanBoolean($this->findCsvMatch($row, 'activated'));
+
+ \Log::debug('UserImporter.php Activated: '.$this->findCsvMatch($row, 'activated'));
+ \Log::debug('UserImporter.php Activated fetchHumanBoolean: '. $this->fetchHumanBoolean($this->findCsvMatch($row, 'activated')));
+
$this->item['employee_num'] = $this->findCsvMatch($row, 'employee_num');
$this->item['department_id'] = $this->createOrFetchDepartment($this->findCsvMatch($row, 'department'));
$this->item['manager_id'] = $this->fetchManager($this->findCsvMatch($row, 'manager_first_name'), $this->findCsvMatch($row, 'manager_last_name'));
@@ -46,14 +61,18 @@ class UserImporter extends ItemImporter
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. ');
return;
}
$this->log('Updating User');
$user->update($this->sanitizeItemForUpdating($user));
$user->save();
+ // \Log::debug('UserImporter.php Updated User ' . print_r($user, true));
return;
}
+
+
// This needs to be applied after the update logic, otherwise we'll overwrite user passwords
// Issue #5408
$this->item['password'] = $this->tempPassword;
@@ -61,9 +80,14 @@ class UserImporter extends ItemImporter
$this->log("No matching user, creating one");
$user = new User();
$user->fill($this->sanitizeItemForStoring($user));
+
if ($user->save()) {
+
+ \Log::debug('UserImporter.php New User ' . print_r($user, true));
+
$this->log("User " . $this->item["name"] . ' was created');
- if($user->email) {
+
+ if(($user->email) && ($user->activated=='1')) {
$data = [
'email' => $user->email,
'username' => $user->username,
@@ -72,7 +96,6 @@ class UserImporter extends ItemImporter
'password' => $this->tempPassword,
];
- // UNCOMMENT this to re-enable sending email notifications on user import
if ($this->send_welcome) {
$user->notify(new WelcomeNotification($data));
}