Skip to content

Commit

Permalink
Merge pull request #125 from fleetbase/dev-v1.5.20
Browse files Browse the repository at this point in the history
command to fix user companies, users crearted before version 1.4
  • Loading branch information
roncodes authored Nov 9, 2024
2 parents 71c27a5 + 219fd78 commit 8a99eda
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fleetbase/core-api",
"version": "1.5.19",
"version": "1.5.20",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
Expand Down
2 changes: 1 addition & 1 deletion config/responsecache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/*
* Determine if the response cache middleware should be enabled.
*/
'enabled' => env('RESPONSE_CACHE_ENABLED', true),
'enabled' => env('RESPONSE_CACHE_ENABLED', false),

/*
* The given class will determinate if a request should be cached. The
Expand Down
52 changes: 52 additions & 0 deletions src/Console/Commands/FixUserCompanies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Fleetbase\Console\Commands;

use Fleetbase\Models\Company;
use Fleetbase\Models\CompanyUser;
use Fleetbase\Models\User;
use Illuminate\Console\Command;

class FixUserCompanies extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'fleetbase:fix-user-companies';

/**
* The console command description.
*
* @var string
*/
protected $description = 'This is a command which checks users\'s to make sure they are assigned to company, if not it assigns them to their correct company.';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$users = User::whereNotNull('company_uuid')->get();

// Fix these users
/** @var User $user */
foreach ($users as $user) {
// Check if user has a customer user record with the company
$doesntHaveCompanyUser = CompanyUser::where(['user_uuid' => $user->uuid, 'company_uuid' => $user->company_uuid])->doesntExist();
if ($doesntHaveCompanyUser) {
$this->line('Found user ' . $user->name . ' (' . $user->email . ') which doesnt have correct company assignment.');
$company = Company::where('uuid', $user->company_uuid)->first();
if ($company) {
$user->assignCompany($company);
$this->line('User ' . $user->email . ' was assigned to company: ' . $company->name);
}
}
}

return Command::SUCCESS;
}
}
23 changes: 23 additions & 0 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Fleetbase\Traits\Searchable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Concerns\HasTimestamps;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
Expand Down Expand Up @@ -1106,6 +1107,11 @@ public function sendInviteFromCompany(?Company $company = null): bool
return false;
}

// if no email cant send invite
if (!$this->email) {
return false;
}

// make sure user isn't already invited
$isAlreadyInvited = Invite::isAlreadySentToJoinCompany($this, $company);
if ($isAlreadyInvited) {
Expand Down Expand Up @@ -1350,4 +1356,21 @@ public function getRoleName(): ?string

return null;
}

public function syncProperty(string $property, Model $model): bool
{
$synced = false;

if ($this->isFillable($property) && !$this->{$property} && $model->{$property}) {
$this->updateQuietly([$property => $model->{$property}]);
$synced = true;
}

if ($model->isFillable($property) && !$model->{$property} && $this->{$property}) {
$model->updateQuietly([$property => $this->{$property}]);
$synced = true;
}

return $synced;
}
}
1 change: 1 addition & 0 deletions src/Providers/CoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class CoreServiceProvider extends ServiceProvider
\Fleetbase\Console\Commands\InitializeSandboxKeyColumn::class,
\Fleetbase\Console\Commands\SyncSandbox::class,
\Fleetbase\Console\Commands\CreatePermissions::class,
\Fleetbase\Console\Commands\FixUserCompanies::class,
\Fleetbase\Console\Commands\BackupDatabase\MysqlS3Backup::class,
];

Expand Down
2 changes: 1 addition & 1 deletion src/Support/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static function setSession($user = null, $login = false): bool
return true;
}

session(['company' => $user->company_uuid, 'user' => $user->uuid, 'is_admin' => $user->isAdmin()]);
session(['company' => $user->company_uuid, 'user' => $user->uuid, 'is_admin' => $user->isAdmin(), 'is_customer' => $user->isType('customer'), 'is_driver' => $user->isType('driver')]);
if ($login) {
Authentication::login($user);
}
Expand Down

0 comments on commit 8a99eda

Please sign in to comment.