Skip to content

Commit

Permalink
updated to add the current existing server ip address to allowed orig…
Browse files Browse the repository at this point in the history
…ins by default
  • Loading branch information
roncodes committed Jun 9, 2023
1 parent 99713f2 commit 674dec0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
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.0.4-alpha",
"version": "1.0.5-alpha",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
Expand Down
39 changes: 39 additions & 0 deletions src/Providers/CoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,24 @@ public function boot()
$this->mergeConfigFrom(__DIR__ . '/../../config/activitylog.php', 'activitylog');
$this->mergeConfigFrom(__DIR__ . '/../../config/excel.php', 'excel');
$this->mergeConfigFromSettings();
$this->addServerIpAsAllowedOrigin();
}

/**
* Merge configuration values from application settings.
*
* This function iterates through a predefined list of settings keys,
* retrieves their values from the system settings, and updates the
* Laravel configuration values accordingly. For some settings, it
* also updates corresponding environment variables.
*
* The settings keys and the corresponding config keys are defined
* in the $settings array. The $putsenv array defines the settings
* keys that also need to update environment variables and maps each
* settings key to the environment variables that need to be updated.
*
* @return void
*/
public function mergeConfigFromSettings()
{
$putsenv = [
Expand Down Expand Up @@ -151,6 +167,29 @@ 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()
{
$serverIp = gethostbyname(gethostname());
$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 674dec0

Please sign in to comment.