diff --git a/composer.json b/composer.json index ff24f51..f8b1443 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Http/Controllers/Internal/v1/SettingController.php b/src/Http/Controllers/Internal/v1/SettingController.php index a645b71..0c2af94 100644 --- a/src/Http/Controllers/Internal/v1/SettingController.php +++ b/src/Http/Controllers/Internal/v1/SettingController.php @@ -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 { @@ -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'; @@ -373,17 +375,11 @@ 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)); @@ -391,6 +387,47 @@ public function saveNotificationChannelsConfig(AdminRequest $request) 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. * @@ -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]); diff --git a/src/Models/User.php b/src/Models/User.php index 047f15a..8cdf8ff 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -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); @@ -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; diff --git a/src/Providers/CoreServiceProvider.php b/src/Providers/CoreServiceProvider.php index 4fb8f27..e44cfc6 100644 --- a/src/Providers/CoreServiceProvider.php +++ b/src/Providers/CoreServiceProvider.php @@ -121,7 +121,6 @@ public function boot() $this->loadViewsFrom(__DIR__ . '/../../views', 'fleetbase'); $this->registerCustomBladeComponents(); $this->mergeConfigFromSettings(); - $this->addServerIpAsAllowedOrigin(); } /** @@ -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 = [ @@ -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) { @@ -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. *