Skip to content

Commit

Permalink
Merge pull request #124 from fleetbase/dev-v1.5.19
Browse files Browse the repository at this point in the history
v1.5.19
  • Loading branch information
roncodes authored Nov 7, 2024
2 parents 9a9c200 + 3b3597d commit 71c27a5
Show file tree
Hide file tree
Showing 15 changed files with 276 additions and 75 deletions.
9 changes: 1 addition & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fleetbase/core-api",
"version": "1.5.18",
"version": "1.5.19",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
Expand All @@ -22,7 +22,6 @@
"fleetbase/twilio": "^5.0.1",
"aws/aws-sdk-php-laravel": "^3.7",
"fleetbase/laravel-mysql-spatial": "^1.0.2",
"genealabs/laravel-model-caching": "dev-fix/handle-datetime-values",
"giggsey/libphonenumber-for-php": "^8.13",
"guzzlehttp/guzzle": "^7.4",
"hammerstone/fast-paginate": "^1.0",
Expand Down Expand Up @@ -60,12 +59,6 @@
"phpstan/phpstan": "^1.10.38",
"symfony/var-dumper": "^5.4.29"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/fleetbase/laravel-model-caching"
}
],
"autoload": {
"psr-4": {
"Fleetbase\\": "src/",
Expand Down
11 changes: 0 additions & 11 deletions config/laravel-model-caching.php

This file was deleted.

19 changes: 6 additions & 13 deletions src/Expansions/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function fleetbaseRestRoutes()
* @return PendingResourceRegistration
*/
return function (string $name, $controller = null, $options = [], ?\Closure $callback = null) {
/** @var \Illuminate\Routing\Router $this */
if (is_callable($controller) && $callback === null) {
$callback = $controller;
$controller = null;
Expand All @@ -51,9 +52,6 @@ public function fleetbaseRestRoutes()
$controller = Str::studly(Str::singular($name)) . 'Controller';
}

/**
* @var \Illuminate\Routing\Router $this
*/
if ($this->container && $this->container->bound(RESTRegistrar::class)) {
$registrar = $this->container->make(RESTRegistrar::class);
} else {
Expand All @@ -67,6 +65,7 @@ public function fleetbaseRestRoutes()
public function fleetbaseRoutes()
{
return function (string $name, callable|array|null $registerFn = null, $options = [], $controller = null) {
/** @var \Illuminate\Routing\Router $this */
if (is_array($registerFn) && !empty($registerFn) && empty($options)) {
$options = $registerFn;
}
Expand All @@ -89,14 +88,13 @@ public function fleetbaseRoutes()
$options['controller'] = $controller;
}

// if (!isset($options['prefix'])) {
// $options['prefix'] = $name;
// }

$make = function (string $routeName) use ($controller) {
return $controller . '@' . $routeName;
};

// Add groupstack to options
$options['groupStack'] = $this->getGroupStack();

$register = function ($router) use ($name, $registerFn, $make, $controller, $options) {
if (is_callable($registerFn)) {
$router->group(
Expand All @@ -110,19 +108,14 @@ function ($router) use ($registerFn, $make, $controller) {
$router->fleetbaseRestRoutes($name, $controller, $options);
};

/*
* @var \Illuminate\Routing\Router $this
*/
return $this->group($options, $register);
};
}

public function fleetbaseAuthRoutes()
{
return function (?callable $registerFn = null, ?callable $registerProtectedFn = null) {
/*
* @var \Illuminate\Routing\Router $this
*/
/** @var \Illuminate\Routing\Router $this */
return $this->group(
['prefix' => 'auth'],
function ($router) use ($registerFn, $registerProtectedFn) {
Expand Down
73 changes: 72 additions & 1 deletion src/Http/Controllers/Internal/v1/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Fleetbase\Exceptions\InvalidVerificationCodeException;
use Fleetbase\Http\Controllers\Controller;
use Fleetbase\Http\Requests\AdminRequest;
use Fleetbase\Http\Requests\Internal\ResetPasswordRequest;
use Fleetbase\Http\Requests\Internal\UserForgotPasswordRequest;
use Fleetbase\Http\Requests\JoinOrganizationRequest;
Expand Down Expand Up @@ -104,7 +105,12 @@ public function session(Request $request)
return response()->error('Session has expired.', 401, ['restore' => false]);
}

return response()->json(['token' => $request->bearerToken(), 'user' => $user->uuid, 'verified' => $user->isVerified(), 'type' => $user->getType()]);
$session = ['token' => $request->bearerToken(), 'user' => $user->uuid, 'verified' => $user->isVerified(), 'type' => $user->getType()];
if (session()->has('impersonator')) {
$session['impersonator'] = session()->get('impersonator');
}

return response()->json($session);
}

/**
Expand Down Expand Up @@ -676,4 +682,69 @@ public function changeUserPassword(Request $request)

return response()->json(['status' => 'ok']);
}

/**
* Allows system admin to impersonate a user.
*
* @return \Illuminate\Http\Response
*/
public function impersonate(AdminRequest $request)
{
$currentUser = Auth::getUserFromSession($request);
if ($currentUser->isNotAdmin()) {
return response()->error('Not authorized to impersonate users.');
}

$targetUserId = $request->input('user');
if (!$targetUserId) {
return response()->error('Not target user selected to impersonate.');
}

$targetUser = User::where('uuid', $targetUserId)->first();
if (!$targetUser) {
return response()->error('The selected user to impersonate was not found.');
}

try {
Auth::setSession($targetUser);
session()->put('impersonator', $currentUser->uuid);
$token = $targetUser->createToken($targetUser->uuid);
} catch (\Exception $e) {
return response()->error($e->getMessage());
}

return response()->json(['status' => 'ok', 'token' => $token->plainTextToken]);
}

/**
* Ends the impersonation session.
*
* @return \Illuminate\Http\Response
*/
public function endImpersonation()
{
$impersonatorId = session()->get('impersonator');
if (!$impersonatorId) {
return response()->error('Not impersonator session found.');
}

$impersonator = User::where('uuid', $impersonatorId)->first();
if (!$impersonator) {
return response()->error('The impersonator user was not found.');
}

if ($impersonator->isNotAdmin()) {
return response()->error('The impersonator does not have permissions. Logout.');
}

try {
Auth::setSession($impersonator);
session()->remove('impersonator');
$token = $impersonator->createToken($impersonator->uuid);
} catch (\Exception $e) {
return response()->error($e->getMessage());
}

return response()->json(['status' => 'ok', 'token' => $token->plainTextToken]);
}
}
15 changes: 15 additions & 0 deletions src/Http/Controllers/Internal/v1/CustomFieldValueController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Fleetbase\Http\Controllers\Internal\v1;

use Fleetbase\Http\Controllers\FleetbaseController;

class CustomFieldValueController extends FleetbaseController
{
/**
* The resource to query.
*
* @var string
*/
public $resource = 'custom_field_value';
}
74 changes: 62 additions & 12 deletions src/Http/Controllers/Internal/v1/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Fleetbase\Support\Utils;
use Illuminate\Http\Request;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Storage;
Expand Down Expand Up @@ -206,10 +207,14 @@ public function testFilesystemConfig(AdminRequest $request)
*/
public function getMailConfig(AdminRequest $request)
{
$mailer = config('mail.default');
$from = config('mail.from');
$mailers = array_keys(config('mail.mailers', []));
$smtpConfig = config('mail.mailers.smtp');
$mailer = config('mail.default');
$from = config('mail.from');
$mailers = Arr::except(array_keys(config('mail.mailers', [])), ['log', 'array', 'failover']);
$smtpConfig = config('mail.mailers.smtp');
$mailgunConfig = config('services.mailgun');
$postmarkConfig = config('services.postmark');
$sendgridConfig = config('services.sendgrid');
$resendConfig = config('services.resend');

$config = [
'mailer' => $mailer,
Expand All @@ -226,6 +231,22 @@ public function getMailConfig(AdminRequest $request)
$config['smtp' . ucfirst($key)] = $value;
}

foreach ($mailgunConfig as $key => $value) {
$config['mailgun' . ucfirst($key)] = $value;
}

foreach ($postmarkConfig as $key => $value) {
$config['postmark' . ucfirst($key)] = $value;
}

foreach ($sendgridConfig as $key => $value) {
$config['sendgrid' . ucfirst($key)] = $value;
}

foreach ($resendConfig as $key => $value) {
$config['resend' . ucfirst($key)] = $value;
}

return response()->json($config);
}

Expand All @@ -236,13 +257,21 @@ public function getMailConfig(AdminRequest $request)
*/
public function saveMailConfig(AdminRequest $request)
{
$mailer = $request->input('mailer', 'smtp');
$from = $request->input('from', []);
$smtp = $request->input('smtp', []);
$mailer = $request->input('mailer', 'smtp');
$from = $request->array('from', []);
$smtp = $request->array('smtp');
$mailgun = $request->array('mailgun');
$postmark = $request->array('postmark');
$sendgrid = $request->array('sendgrid');
$resend = $request->array('resend');

Setting::configureSystem('mail.mailer', $mailer);
Setting::configureSystem('mail.from', $from);
Setting::configureSystem('mail.smtp', array_merge(['transport' => 'smtp'], $smtp));
Setting::configureSystem('services.mailgun', $mailgun);
Setting::configureSystem('services.postmark', $postmark);
Setting::configureSystem('services.sendgrid', $sendgrid);
Setting::configureSystem('services.resend', $resend);

return response()->json(['status' => 'OK']);
}
Expand All @@ -268,16 +297,37 @@ public function testMailConfig(AdminRequest $request)
'name' => 'Fleetbase',
]
);
$smtp = $request->input('smtp', []);
$user = $request->user();
$message = 'Mail configuration is successful, check your inbox for the test email to confirm.';
$status = 'success';
$smtp = $request->input('smtp', []);
$mailgun = $request->array('mailgun');
$postmark = $request->array('postmark');
$sendgrid = $request->array('sendgrid');
$resend = $request->array('resend');
$user = $request->user();
$message = 'Mail configuration is successful, check your inbox for the test email to confirm.';
$status = 'success';

// set config values from input
config(['mail.default' => $mailer, 'mail.from' => $from, 'mail.mailers.smtp' => array_merge(['transport' => 'smtp'], $smtp)]);

// set mailer configs
if ($mailer === 'mailgun') {
config(['services.mailgun' => $mailgun]);
}

if ($mailer === 'postmark') {
config(['services.postmark' => $postmark]);
}

if ($mailer === 'sendgrid') {
config(['services.sendgrid' => $sendgrid]);
}

if ($mailer === 'resend') {
config(['services.resend' => $resend]);
}

try {
Mail::send(new \Fleetbase\Mail\TestMail($user));
Mail::send(new \Fleetbase\Mail\TestMail($user, $mailer));
} catch (\Aws\Ses\Exception\SesException|\Exception $e) {
$message = $e->getMessage();
$status = 'error';
Expand Down
43 changes: 43 additions & 0 deletions src/Http/Requests/Internal/BulkActionRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Fleetbase\Http\Requests\Internal;

use Fleetbase\Http\Requests\FleetbaseRequest;

class BulkActionRequest extends FleetbaseRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return $this->session()->has('user');
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'ids' => ['required', 'array'],
];
}

/**
* Get the validation rules error messages.
*
* @return array
*/
public function messages()
{
return [
'ids.required' => 'Please provide a resource ID.',
'ids.array' => 'Please provide multiple resource ID\'s.',
];
}
}
Loading

0 comments on commit 71c27a5

Please sign in to comment.