Skip to content

Commit

Permalink
Merge pull request #69 from fleetbase/dev-v1.4.8
Browse files Browse the repository at this point in the history
v1.4.8
  • Loading branch information
roncodes authored Feb 22, 2024
2 parents 9c21c00 + 0942c27 commit d5cf8b7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 57 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.7",
"version": "1.4.8",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
Expand Down
65 changes: 54 additions & 11 deletions src/Http/Controllers/Internal/v1/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Illuminate\Http\Request;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class SettingController extends Controller
{
Expand Down Expand Up @@ -86,7 +88,7 @@ public function testFilesystemConfig(AdminRequest $request)
config(['filesystem.default' => $disk]);

try {
\Illuminate\Support\Facades\Storage::disk($disk)->put('testfile.txt', 'Hello World');
Storage::disk($disk)->put('testfile.txt', 'Hello World');
} catch (\Aws\S3\Exception\S3Exception $e) {
$message = $e->getMessage();
$status = 'error';
Expand Down Expand Up @@ -373,24 +375,59 @@ public function saveNotificationChannelsConfig(AdminRequest $request)
$apn = $request->array('apn', config('broadcasting.connections.apn'));
$firebase = $request->array('firebase', config('firebase.projects.app'));

if (is_array($apn) && isset($apn['private_key_content'])) {
$apn['private_key_content'] = str_replace('\\n', "\n", trim($apn['private_key_content']));
}
// Get the APN key file and it's contents and store to config
$apn = static::_setupApnConfigUsingFileId($apn);

if (is_array($apn) && isset($apn['_private_key_path']) && is_string($apn['_private_key_path'])) {
$apn['private_key_path'] = storage_path('app/' . $apn['_private_key_path']);
}

if (is_array($firebase) && isset($firebase['credentials_file_path']) && is_string($firebase['credentials_file_path'])) {
$firebase['credentials'] = storage_path('app/' . $firebase['credentials_file_path']);
}
// Get credentials config array from file contents
$firebase = static::_setupFcmConfigUsingFileId($firebase);

Setting::configure('system.broadcasting.apn', array_merge(config('broadcasting.connections.apn', []), $apn));
Setting::configure('system.firebase.app', array_merge(config('firebase.projects.app', []), $firebase));

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

private static function _setupApnConfigUsingFileId(array $apn = []): array
{
// Get the APN key file and it's contents and store to config
if (is_array($apn) && isset($apn['private_key_file_id']) && Str::isUuid($apn['private_key_file_id'])) {
$apnKeyFile = File::where('uuid', $apn['private_key_file_id'])->first();
if ($apnKeyFile) {
$apnKeyFileContents = Storage::disk('local')->get($apnKeyFile->path);
if ($apnKeyFileContents) {
$apn['private_key_content'] = str_replace('\\n', "\n", trim($apnKeyFileContents));
}
}
}

// Always set apn `private_key_path` and `private_key_file`
unset($apn['private_key_path'], $apn['private_key_file']);

return $apn;
}

private static function _setupFcmConfigUsingFileId(array $firebase = []): array
{
if (is_array($firebase) && isset($firebase['credentials_file_id']) && Str::isUuid($firebase['credentials_file_id'])) {
$firebaseCredentialsFile = File::where('uuid', $firebase['credentials_file_id'])->first();
if ($firebaseCredentialsFile) {
$firebaseCredentialsContent = Storage::disk('local')->get($firebaseCredentialsFile->path);
if ($firebaseCredentialsContent) {
$firebaseCredentialsContentArray = json_decode($firebaseCredentialsContent, true);
if (is_array($firebaseCredentialsContentArray)) {
$firebaseCredentialsContentArray['private_key'] = str_replace('\\n', "\n", trim($firebaseCredentialsContentArray['private_key']));
}
$firebase['credentials'] = $firebaseCredentialsContentArray;
}
}
}

// Always set apn `credentials_file`
unset($firebase['credentials_file']);

return $firebase;
}

/**
* Test notification channels configuration.
*
Expand All @@ -405,6 +442,12 @@ public function testNotificationChannelsConfig(AdminRequest $request)
$apn = $request->array('apn', config('broadcasting.connections.apn'));
$firebase = $request->array('firebase', config('firebase.projects.app'));

// Get the APN key file and it's contents and store to config
$apn = static::_setupApnConfigUsingFileId($apn);

// Get credentials config array from file contents
$firebase = static::_setupFcmConfigUsingFileId($firebase);

// temporarily set apn config here
config(['broadcasting.connections.apn' => $apn]);

Expand Down
6 changes: 3 additions & 3 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function assignCompany(Company $company): User
}

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

Expand Down Expand Up @@ -249,8 +249,8 @@ public function assignCompanyFromId(?string $id): User

// Get company record
$company = Company::where('uuid', $id)->orWhere('public_id', $id)->first();
if ($comapny) {
return $this->assignCompany($comapny);
if ($company) {
return $this->assignCompany($company);
}

return $this;
Expand Down
43 changes: 1 addition & 42 deletions src/Providers/CoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ public function boot()
$this->loadViewsFrom(__DIR__ . '/../../views', 'fleetbase');
$this->registerCustomBladeComponents();
$this->mergeConfigFromSettings();
$this->addServerIpAsAllowedOrigin();
}

/**
Expand Down Expand Up @@ -169,7 +168,6 @@ public function mergeConfigFromSettings()
'services.google_maps' => ['api_key' => 'GOOGLE_MAPS_API_KEY', 'locale' => 'GOOGLE_MAPS_LOCALE'],
'services.twilio' => ['sid' => 'TWILIO_SID', 'token' => 'TWILIO_TOKEN', 'from' => 'TWILIO_FROM'],
'services.sentry' => ['dsn' => 'SENTRY_DSN'],
'firebase.app' => ['credentials' => 'FIREBASE_CREDENTIALS'],
];

$settings = [
Expand Down Expand Up @@ -252,7 +250,7 @@ public function mergeConfigFromSettings()

$envValue = data_get($value, $configEnvKey);
$doesntHaveEnvSet = empty(env($envKey));
$hasValue = !empty($envValue);
$hasValue = !empty($envValue) && !is_array($envValue);

// only set if env variable is not set already
if ($doesntHaveEnvSet && $hasValue) {
Expand All @@ -278,45 +276,6 @@ public function mergeConfigFromSettings()
}
}

/**
* Add the server's IP address to the CORS allowed origins.
*
* This function retrieves the server's IP address and adds it to the
* list of CORS allowed origins in the Laravel configuration. If the
* server's IP address is already in the list, the function doesn't
* add it again.
*
* @return void
*/
public function addServerIpAsAllowedOrigin()
{
$cacheKey = 'server_public_ip';
$cacheExpirationMinutes = 60 * 60 * 24 * 30;

// Check the cache first
$serverIp = Cache::get($cacheKey);

// If not cached, fetch the IP and store it in the cache
if (!$serverIp) {
$serverIp = trim(shell_exec('dig +short myip.opendns.com @resolver1.opendns.com'));

if (!$serverIp) {
return;
}

Cache::put($cacheKey, $serverIp, $cacheExpirationMinutes);
}

$allowedOrigins = config('cors.allowed_origins', []);
$serverIpOrigin = "http://{$serverIp}:4200";

if (!in_array($serverIpOrigin, $allowedOrigins, true)) {
$allowedOrigins[] = $serverIpOrigin;
}

config(['cors.allowed_origins' => $allowedOrigins]);
}

/**
* Registers all class extension macros from the specified path and namespace.
*
Expand Down

0 comments on commit d5cf8b7

Please sign in to comment.