Skip to content

A free open source IT asset/license management system

* Alphabetize the createOrFetch methods * Move the html escaping to array_smart_fetch to avoid duplication * Ensure string comparisons have === everywhere

Daniel Meltzere42cfc7601fc
diff --git a/app/Console/Commands/ObjectImportCommand.php b/app/Console/Commands/ObjectImportCommand.php
index 2e38014875..96c3c62e5d 100644
--- a/app/Console/Commands/ObjectImportCommand.php
+++ b/app/Console/Commands/ObjectImportCommand.php
@@ -159,148 +159,61 @@ class ObjectImportCommand extends Command {
* @return string
*/
public function array_smart_fetch(Array $array, $key, $default = ''){
- return array_key_exists($key,$array) ? trim($array[ $key ]) : $default;
+ return array_key_exists($key,$array) ? e(trim($array[ $key ])) : $default;
}
- /**
- * @var
+ private $asset_models;
+ /**
+ * @param array
+ * @param $category Category
+ * @param $manufacturer Manufacturer
+ * @return Model
+ * @internal param $asset_modelno string
*/
- private $locations;
- /**
- * Checks the DB to see if a location with the same name exists, otherwise create it
- * @param $asset_location string
- * @return Location
- */
- public function createOrFetchLocation($asset_location)
- {
- foreach($this->locations as $templocation) {
- if( (strcmp($templocation->name,e($asset_location)) == 0) ) {
- $this->comment('Location ' . $asset_location . ' already exists');
- return $templocation;
- }
- }
- // No matching locations in the collection, create a new one.
- $location = new Location();
-
- if (!empty($asset_location)) {
- $location->name = e($asset_location);
- $location->address = '';
- $location->city = '';
- $location->state = '';
- $location->country = '';
- $location->user_id = 1;
- $this->locations->add($location);
-
- if (!$this->option('testrun')) {
- if ($location->save()) {
- $this->comment('Location ' . $asset_location . ' was created');
- return $location;
- } else {
- $this->comment('Something went wrong! Location ' . $asset_location . ' was NOT created');
- dd($location);
- return $location;
- }
- } else {
- return $location;
- }
- } else {
- $this->comment('No location given, so none created.');
- return $location;
- }
-
- }
-
-
- /**
- * Finds the user matching given data, or creates a new one if there is no match
- * @param $row array
- * @return User Model w/ matching name
- * @internal param string $user_username Username extracted from CSV
- * @internal param string $user_email Email extracted from CSV
- * @internal param string $first_name
- * @internal param string $last_name
- */
- public function createOrFetchUser($row)
+ public function createOrFetchAssetModel(array $row, $category, $manufacturer)
{
- $user_name = $this->array_smart_fetch($row, "name");
- $user_email = $this->array_smart_fetch($row, "email");
- $user_username = $this->array_smart_fetch($row, "username");
-
- // A number was given instead of a name
- if (is_numeric($user_name)) {
- $this->comment('User ' . $user_name . ' is a number - Assuming UID - hopefully this user already exists');
- $user_username = '';
- // No name was given
- } elseif (empty($user_name)) {
- $this->comment('No user data provided - skipping user creation, just adding asset');
- $first_name = '';
- $last_name = '';
-
- } else {
- $user_email_array = User::generateFormattedNameFromFullName($this->option('email_format'), $user_name);
- $first_name = $user_email_array['first_name'];
- $last_name = $user_email_array['last_name'];
+ $asset_model_name = $this->array_smart_fetch($row, "model name");
+ $asset_modelno = $this->array_smart_fetch($row, "model number");
+ if(empty($asset_model_name))
+ $asset_model_name='Unknown';
+ if(empty($asset_modelno))
+ $asset_modelno=0;
+ $this->comment('Model Name: ' . $asset_model_name);
+ $this->comment('Model No: ' . $asset_modelno);
- if (empty($user_email)) {
- $user_email = $user_email_array['username'] . '@' . Config::get('app.domain');
- }
-
- if (empty($user_username)) {
- if ($this->option('username_format') == 'email') {
- $user_username = $user_email;
- } else {
- $user_name_array = User::generateFormattedNameFromFullName($this->option('username_format'), $user_name);
- $user_username = $user_name_array['username'];
- }
+ foreach ($this->asset_models as $tempmodel) {
+ if ($tempmodel->name === $asset_model_name
+ && $tempmodel->modelno == $asset_modelno
+ && $tempmodel->category_id == $category->id
+ && $tempmodel->manufacturer_id == $manufacturer->id )
+ {
+ $this->comment('A matching model ' . $asset_model_name . ' with model number ' . $asset_modelno . ' already exists');
+ return $tempmodel;
}
-
}
- $this->comment("--- User Data ---");
- $this->comment('Full Name: ' . $user_name);
- $this->comment('First Name: ' . $first_name);
- $this->comment('Last Name: ' . $last_name);
- $this->comment('Username: ' . $user_username);
- $this->comment('Email: ' . $user_email);
- $this->comment('--- End User Data ---');
+ $asset_model = new AssetModel();
+ $asset_model->name = $asset_model_name;
+ $asset_model->manufacturer_id = $manufacturer->id;
+ $asset_model->modelno = e($asset_modelno);
+ $asset_model->category_id = $category->id;
+ $asset_model->user_id = 1;
+ $this->asset_models->add($asset_model);
- if($this->option('testrun'))
- return new User;
- if (!empty($user_username)) {
- if ($user = User::MatchEmailOrUsername($user_username, $user_email)
- ->whereNotNull('username')->first()
- ) {
- $this->comment('User ' . $user_username . ' already exists');
- return $user;
+ if(!$this->option('testrun')) {
+ if ($asset_model->save()) {
+ $this->comment('Asset Model ' . $asset_model_name . ' with model number ' . $asset_modelno . ' was created');
+ return $asset_model;
} else {
- // Create the user
- $user = Sentry::createUser(array(
- 'first_name' => $first_name,
- 'last_name' => $last_name,
- 'email' => $user_email,
- 'username' => $user_username,
- 'password' => substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 12),
- 'activated' => true,
- 'permissions' => array(
- 'admin' => 0,
- 'user' => 1,
- ),
- 'notes' => 'User imported through asset importer'
- ));
-
- // Find the group using the group id
- $userGroup = Sentry::findGroupById(3);
-
- // Assign the group to the user
- $user->addGroup($userGroup);
- $this->comment('User ' . $first_name . ' created');
- return $user;
+ $this->comment('Something went wrong! Asset Model ' . $asset_model_name . ' was NOT created');
+ dd($asset_model);
+ return $asset_model;
}
} else {
- $user = new User;
- return $user;
+ return $asset_model;
}
+
}
private $categories;
@@ -313,14 +226,11 @@ class ObjectImportCommand extends Command {
*/
public function createOrFetchCategory($asset_category, $item_type)
{
- if (empty($asset_category)) {
- $category_name = 'Unnamed Category';
- } else {
- $category_name = e($asset_category);
- }
+ if (empty($asset_category))
+ $asset_category = 'Unnamed Category';
foreach($this->categories as $tempcategory) {
- if( (strcmp($tempcategory->name, $category_name) == 0) && $tempcategory->category_type == $item_type) {
+ if( $tempcategory->name === $asset_category && $tempcategory->category_type === $item_type) {
$this->comment('Category ' . $asset_category . ' already exists');
return $tempcategory;
}
@@ -328,7 +238,7 @@ class ObjectImportCommand extends Command {
$category = new Category();
- $category->name = $category_name;
+ $category->name = $asset_category;
$category->category_type = $item_type;
$category->user_id = 1;
$this->categories->add($category);
@@ -348,6 +258,37 @@ class ObjectImportCommand extends Command {
}
+ private $companies;
+
+ /**
+ * @param $asset_company_name string
+ * @return Company
+ */
+ public function createOrFetchCompany($asset_company_name)
+ {
+ foreach ($this->companies as $tempcompany) {
+ if ($tempcompany->name === $asset_company_name) {
+ $this->comment('A matching Company ' . $asset_company_name . ' already exists');
+ return $tempcompany;
+ }
+ }
+
+ $company = new Company();
+ $company->name = $asset_company_name;
+
+ if(!$this->option('testrun')) {
+ if ($company->save()) {
+ $this->comment('Company ' . $asset_company_name . ' was created');
+ return $company;
+ } else {
+ $this->comment('Something went wrong! Company ' . $asset_company_name . ' was NOT created');
+ return $company;
+ }
+ } else {
+ return $company;
+ }
+ }
+
private $manufacturers;
@@ -357,6 +298,7 @@ class ObjectImportCommand extends Command {
* @return Manufacturer
* @internal param $asset_mfgr string
*/
+
public function createOrFetchManufacturer(array $row)
{
$asset_mfgr = $this->array_smart_fetch($row, "manufacturer");
@@ -367,7 +309,7 @@ class ObjectImportCommand extends Command {
$this->comment('Manufacturer ID: ' . $asset_mfgr);
foreach ($this->manufacturers as $tempmanufacturer) {
- if ($tempmanufacturer->name == $asset_mfgr) {
+ if ($tempmanufacturer->name === $asset_mfgr) {
$this->comment('Manufacturer ' . $asset_mfgr . ' already exists');
return $tempmanufacturer;
}
@@ -376,7 +318,7 @@ class ObjectImportCommand extends Command {
//Otherwise create a manufacturer.
$manufacturer = new Manufacturer();
- $manufacturer->name = e($asset_mfgr);
+ $manufacturer->name = $asset_mfgr;
$manufacturer->user_id = 1;
$this->manufacturers->add($manufacturer);
@@ -395,87 +337,52 @@ class ObjectImportCommand extends Command {
}
}
- private $asset_models;
- /**
- * @param array
- * @param $category Category
- * @param $manufacturer Manufacturer
- * @return Model
- * @internal param $asset_modelno string
+ /**
+ * @var
*/
- public function createOrFetchAssetModel(array $row, $category, $manufacturer)
- {
-
- $asset_model_name = $this->array_smart_fetch($row, "model name");
- $asset_modelno = $this->array_smart_fetch($row, "model number");
- if(empty($asset_model_name))
- $asset_model_name='Unknown';
- if(empty($asset_modelno))
- $asset_modelno=0;
- $this->comment('Model Name: ' . $asset_model_name);
- $this->comment('Model No: ' . $asset_modelno);
-
-
- foreach ($this->asset_models as $tempmodel) {
- if ($tempmodel->name == $asset_model_name && $tempmodel->modelno == $asset_modelno
- && $tempmodel->category_id == $category->id && $tempmodel->manufacturer_id == $manufacturer->id
- ) {
- $this->comment('A matching model ' . $asset_model_name . ' with model number ' . $asset_modelno . ' already exists');
- return $tempmodel;
- }
- }
- $asset_model = new AssetModel();
- $asset_model->name = e($asset_model_name);
- $asset_model->manufacturer_id = $manufacturer->id;
- $asset_model->modelno = e($asset_modelno);
- $asset_model->category_id = $category->id;
- $asset_model->user_id = 1;
- $this->asset_models->add($asset_model);
-
- if(!$this->option('testrun')) {
- if ($asset_model->save()) {
- $this->comment('Asset Model ' . $asset_model_name . ' with model number ' . $asset_modelno . ' was created');
- return $asset_model;
- } else {
- $this->comment('Something went wrong! Asset Model ' . $asset_model_name . ' was NOT created');
- dd($asset_model);
- return $asset_model;
- }
- } else {
- return $asset_model;
- }
-
- }
-
- private $companies;
-
+ private $locations;
/**
- * @param $asset_company_name string
- * @return Company
+ * Checks the DB to see if a location with the same name exists, otherwise create it
+ * @param $asset_location string
+ * @return Location
*/
- public function createOrFetchCompany($asset_company_name)
+ public function createOrFetchLocation($asset_location)
{
- foreach ($this->companies as $tempcompany) {
- if ($tempcompany->name == $asset_company_name ) {
- $this->comment('A matching Company ' . $asset_company_name . ' already exists');
- return $tempcompany;
+ foreach($this->locations as $templocation) {
+ if( $templocation->name === $asset_location ) {
+ $this->comment('Location ' . $asset_location . ' already exists');
+ return $templocation;
}
}
+ // No matching locations in the collection, create a new one.
+ $location = new Location();
- $company = new Company();
- $company->name = e($asset_company_name);
+ if (!empty($asset_location)) {
+ $location->name = $asset_location;
+ $location->address = '';
+ $location->city = '';
+ $location->state = '';
+ $location->country = '';
+ $location->user_id = 1;
+ $this->locations->add($location);
- if(!$this->option('testrun')) {
- if ($company->save()) {
- $this->comment('Company ' . $asset_company_name . ' was created');
- return $company;
+ if (!$this->option('testrun')) {
+ if ($location->save()) {
+ $this->comment('Location ' . $asset_location . ' was created');
+ return $location;
+ } else {
+ $this->comment('Something went wrong! Location ' . $asset_location . ' was NOT created');
+ dd($location);
+ return $location;
+ }
} else {
- $this->comment('Something went wrong! Company ' . $asset_company_name . ' was NOT created');
- return $company;
+ return $location;
}
} else {
- return $company;
+ $this->comment('No location given, so none created.');
+ return $location;
}
+
}
private $suppliers;
@@ -490,14 +397,14 @@ class ObjectImportCommand extends Command {
if(empty($supplier_name))
$supplier_name='Unknown';
foreach ($this->suppliers as $tempsupplier) {
- if ($tempsupplier->name == $supplier_name ) {
+ if ($tempsupplier->name === $supplier_name) {
$this->comment('A matching Company ' . $supplier_name . ' already exists');
return $tempsupplier;
}
}
$supplier = new Supplier();
- $supplier->name = e($supplier_name);
+ $supplier->name = $supplier_name;
$supplier->user_id = 1;
if(!$this->option('testrun')) {
@@ -513,6 +420,99 @@ class ObjectImportCommand extends Command {
return $supplier;
}
}
+
+ /**
+ * Finds the user matching given data, or creates a new one if there is no match
+ * @param $row array
+ * @return User Model w/ matching name
+ * @internal param string $user_username Username extracted from CSV
+ * @internal param string $user_email Email extracted from CSV
+ * @internal param string $first_name
+ * @internal param string $last_name
+ */
+ public function createOrFetchUser($row)
+ {
+ $user_name = $this->array_smart_fetch($row, "name");
+ $user_email = $this->array_smart_fetch($row, "email");
+ $user_username = $this->array_smart_fetch($row, "username");
+
+ // A number was given instead of a name
+ if (is_numeric($user_name)) {
+ $this->comment('User ' . $user_name . ' is a number - Assuming UID - hopefully this user already exists');
+ $user_username = '';
+
+ // No name was given
+ } elseif (empty($user_name)) {
+ $this->comment('No user data provided - skipping user creation, just adding asset');
+ $first_name = '';
+ $last_name = '';
+
+ } else {
+ $user_email_array = User::generateFormattedNameFromFullName($this->option('email_format'), $user_name);
+ $first_name = $user_email_array['first_name'];
+ $last_name = $user_email_array['last_name'];
+
+ if (empty($user_email)) {
+ $user_email = $user_email_array['username'] . '@' . Config::get('app.domain');
+ }
+
+ if (empty($user_username)) {
+ if ($this->option('username_format') == 'email') {
+ $user_username = $user_email;
+ } else {
+ $user_name_array = User::generateFormattedNameFromFullName($this->option('username_format'), $user_name);
+ $user_username = $user_name_array['username'];
+ }
+
+ }
+
+ }
+ $this->comment("--- User Data ---");
+ $this->comment('Full Name: ' . $user_name);
+ $this->comment('First Name: ' . $first_name);
+ $this->comment('Last Name: ' . $last_name);
+ $this->comment('Username: ' . $user_username);
+ $this->comment('Email: ' . $user_email);
+ $this->comment('--- End User Data ---');
+
+ if($this->option('testrun'))
+ return new User;
+ if (!empty($user_username)) {
+ if ($user = User::MatchEmailOrUsername($user_username, $user_email)
+ ->whereNotNull('username')->first()
+ ) {
+ $this->comment('User ' . $user_username . ' already exists');
+ return $user;
+ } else {
+ // Create the user
+ $user = Sentry::createUser(array(
+ 'first_name' => $first_name,
+ 'last_name' => $last_name,
+ 'email' => $user_email,
+ 'username' => $user_username,
+ 'password' => substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 12),
+ 'activated' => true,
+ 'permissions' => array(
+ 'admin' => 0,
+ 'user' => 1,
+ ),
+ 'notes' => 'User imported through asset importer'
+ ));
+
+ // Find the group using the group id
+ $userGroup = Sentry::findGroupById(3);
+
+ // Assign the group to the user
+ $user->addGroup($userGroup);
+ $this->comment('User ' . $first_name . ' created');
+ return $user;
+ }
+ } else {
+ $user = new User;
+ return $user;
+ }
+ }
+
private $assets;
/**
@@ -533,7 +533,7 @@ class ObjectImportCommand extends Command {
$this->comment('Notes: '.$item["notes"]);
foreach ($this->assets as $tempasset) {
- if ($tempasset->asset_tag == $asset_tag ) {
+ if ($tempasset->asset_tag === $asset_tag ) {
$this->comment('A matching Asset ' . $asset_tag . ' already exists');
return;
}