Skip to content

Commit

Permalink
Merge pull request #65 from fleetbase/dev-v1.4.5
Browse files Browse the repository at this point in the history
v1.4.5
  • Loading branch information
roncodes authored Feb 17, 2024
2 parents 27dcac2 + 2e2b966 commit dd8e5e6
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 66 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.4.4",
"version": "1.4.5",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
Expand Down
7 changes: 7 additions & 0 deletions src/Http/Filter/CompanyFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ class CompanyFilter extends Filter
{
public function queryForInternal()
{
// If admin query then do not filter
$isAdminQuery = $this->request->input('view') === 'admin' && $this->request->user()->isAdmin();
if ($isAdminQuery) {
return;
}

// Otherwise filter so that user only see's their own companies
$this->builder->where(
function ($query) {
$query
Expand Down
3 changes: 1 addition & 2 deletions src/Http/Requests/AdminRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class AdminRequest extends FleetbaseRequest
public function authorize()
{
$user = $this->user();

if (!$user === null) {
if (!$user) {
return false;
}

Expand Down
33 changes: 18 additions & 15 deletions src/Http/Resources/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,24 @@ class Organization extends FleetbaseResource
public function toArray($request)
{
return [
'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id),
'uuid' => $this->when(Http::isInternalRequest(), $this->uuid),
'public_id' => $this->when(Http::isInternalRequest(), $this->public_id),
'name' => $this->name,
'description' => $this->description,
'phone' => $this->phone,
'timezone' => $this->timezone,
'logo_url' => $this->logo_url,
'backdrop_url' => $this->backdrop_url,
'branding' => Setting::getBranding(),
'options' => $this->options,
'slug' => $this->slug,
'status' => $this->status,
'updated_at' => $this->updated_at,
'created_at' => $this->created_at,
'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id),
'uuid' => $this->when(Http::isInternalRequest(), $this->uuid),
'public_id' => $this->when(Http::isInternalRequest(), $this->public_id),
'name' => $this->name,
'description' => $this->description,
'phone' => $this->phone,
'type' => $this->when(Http::isInternalRequest(), $this->type),
'users_count' => $this->when(Http::isInternalRequest(), $this->companyUsers()->count()),
'timezone' => $this->timezone,
'logo_url' => $this->logo_url,
'backdrop_url' => $this->backdrop_url,
'branding' => Setting::getBranding(),
'options' => $this->options,
'owner' => new Author($this->owner),
'slug' => $this->slug,
'status' => $this->status,
'updated_at' => $this->updated_at,
'created_at' => $this->created_at,
];
}
}
18 changes: 15 additions & 3 deletions src/Listeners/LogFailedWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Fleetbase\Models\WebhookRequestLog;
use Fleetbase\Webhook\Events\WebhookCallFailedEvent;
use Illuminate\Support\Str;

class LogFailedWebhook
{
Expand All @@ -23,8 +24,11 @@ public function handle(WebhookCallFailedEvent $event)
/** @var string $connection The db connection the webhook was called on */
$connection = (bool) data_get($event, 'meta.is_sandbox') ? 'sandbox' : 'mysql';

// Log webhook callback event
WebhookRequestLog::on($connection)->create([
// Get API credential
$apiCredentialUuid = data_get($event, 'meta.api_credential_uuid');

// Prepare insert array
$data = [
'_key' => data_get($event, 'meta.api_key'),
'company_uuid' => data_get($event, 'meta.company_uuid'),
'api_credential_uuid' => data_get($event, 'meta.api_credential_uuid'),
Expand All @@ -41,6 +45,14 @@ public function handle(WebhookCallFailedEvent $event)
'headers' => $event->headers,
'meta' => $event->meta,
'sent_at' => data_get($event, 'meta.sent_at'),
]);
];

// Set api credential uuid
if ($apiCredentialUuid && Str::isUuid($apiCredentialUuid)) {
$data['api_credential_uuid'] = $apiCredentialUuid;
}

// Log webhook callback event
WebhookRequestLog::on($connection)->create($data);
}
}
18 changes: 14 additions & 4 deletions src/Listeners/LogFinalWebhookAttempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ public function handle(FinalWebhookCallFailedEvent $event)
/** @var int $statusCode The response status code */
$statusCode = $response ? $response->getStatusCode() : 500;

// log webhook event
WebhookRequestLog::on($connection)->create([
// Get API credential
$apiCredentialUuid = data_get($event, 'meta.api_credential_uuid');

// Prepare insert array
$data = [
'_key' => data_get($event, 'meta.api_key'),
'company_uuid' => data_get($event, 'meta.company_uuid'),
'api_credential_uuid' => data_get($event, 'meta.api_credential_uuid'),
'webhook_uuid' => data_get($event, 'meta.webhook_uuid'),
'api_event_uuid' => data_get($event, 'meta.api_event_uuid'),
'method' => $event->httpVerb,
Expand All @@ -44,6 +46,14 @@ public function handle(FinalWebhookCallFailedEvent $event)
'headers' => $event->headers,
'meta' => $event->meta,
'sent_at' => data_get($event, 'meta.sent_at'),
]);
];

// Set api credential uuid
if ($apiCredentialUuid && Str::isUuid($apiCredentialUuid)) {
$data['api_credential_uuid'] = $apiCredentialUuid;
}

// log webhook event
WebhookRequestLog::on($connection)->create($data);
}
}
19 changes: 15 additions & 4 deletions src/Listeners/LogSuccessfulWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Fleetbase\Models\WebhookRequestLog;
use Fleetbase\Webhook\Events\WebhookCallSucceededEvent;
use Illuminate\Support\Str;

class LogSuccessfulWebhook
{
Expand All @@ -23,11 +24,13 @@ public function handle(WebhookCallSucceededEvent $event)
/** @var string $connection The db connection the webhook was called on */
$connection = (bool) data_get($event, 'meta.is_sandbox') ? 'sandbox' : 'mysql';

// Log webhook callback event
WebhookRequestLog::on($connection)->create([
// Get API credential
$apiCredentialUuid = data_get($event, 'meta.api_credential_uuid');

// Prepare insert array
$data = [
'_key' => data_get($event, 'meta.api_key'),
'company_uuid' => data_get($event, 'meta.company_uuid'),
'api_credential_uuid' => data_get($event, 'meta.api_credential_uuid'),
'webhook_uuid' => data_get($event, 'meta.webhook_uuid'),
'api_event_uuid' => data_get($event, 'meta.api_event_uuid'),
'method' => $event->httpVerb,
Expand All @@ -41,6 +44,14 @@ public function handle(WebhookCallSucceededEvent $event)
'headers' => $event->headers,
'meta' => $event->meta,
'sent_at' => data_get($event, 'meta.sent_at'),
]);
];

// Set api credential uuid
if ($apiCredentialUuid && Str::isUuid($apiCredentialUuid)) {
$data['api_credential_uuid'] = $apiCredentialUuid;
}

// Log webhook callback event
WebhookRequestLog::on($connection)->create($data);
}
}
8 changes: 8 additions & 0 deletions src/Models/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ public function users()
return $this->belongsToMany(User::class, 'company_users');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function companyUsers()
{
return $this->hasManyThrough(User::class, CompanyUser::class, 'company_uuid', 'uuid', 'uuid', 'user_uuid');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
Expand Down
18 changes: 12 additions & 6 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Fleetbase\Models;

use Fleetbase\Casts\Json;
use Fleetbase\Notifications\UserCreated;
use Fleetbase\Notifications\UserInvited;
use Fleetbase\Support\NotificationRegistry;
use Fleetbase\Support\Utils;
use Fleetbase\Traits\Expandable;
use Fleetbase\Traits\Filterable;
Expand Down Expand Up @@ -214,6 +216,15 @@ public function assignCompany(Company $company)
CompanyUser::create(['company_uuid' => $company->uuid, 'user_uuid' => $this->uuid, 'status' => $this->status]);
}

// Determine if user should receive invite to join company
if ($user->isNotAdmin()) {
// Invite user to join company
$this->sendInviteFromCompany($company);

// Notify the company owner a user has been created
NotificationRegistry::notify(UserCreated::class, $this, $company);
}

$this->save();
}

Expand Down Expand Up @@ -582,12 +593,7 @@ public function sendInviteFromCompany(Company $company = null): bool
}

// make sure user isn't already invited
$isAlreadyInvited = Invite::where([
'company_uuid' => $company->uuid,
'subject_uuid' => $company->uuid,
'protocol' => 'email',
'reason' => 'join_company',
])->whereJsonContains('recipients', $this->email)->exists();
$isAlreadyInvited = Invite::isAlreadySentToJoinCompany($this, $company);
if ($isAlreadyInvited) {
return false;
}
Expand Down
31 changes: 0 additions & 31 deletions src/Observers/UserObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,9 @@

use Fleetbase\Models\CompanyUser;
use Fleetbase\Models\User;
use Fleetbase\Notifications\UserCreated;
use Fleetbase\Support\NotificationRegistry;

class UserObserver
{
/**
* Handle the User "created" event.
*
* @return void
*/
public function created(User $user)
{
// Make sure we have company
$company = $user->getCompany();

// If no company delete user and throw exception
if (!$company) {
$user->deleteQuietly();
throw new \Exception('Unable to assign user to company.');
}

if (CompanyUser::where(['company_uuid' => $company->uuid, 'user_uuid' => $user->uuid])->doesntExist()) {
CompanyUser::create(['company_uuid' => $company->uuid, 'user_uuid' => $user->uuid, 'status' => $user->status]);
}

// invite user to join company
$user->sendInviteFromCompany($company);

// Notify the company owner a user has been created
if ($company) {
NotificationRegistry::notify(UserCreated::class, $user, $company);
}
}

/**
* Handle the User "deleted" event.
*
Expand Down

0 comments on commit dd8e5e6

Please sign in to comment.