Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command to fix user companies, users crearted before version 1.4 #125

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading