fix: replace myservers.cfg reads in UpdateFlashBackup.php (#1517)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **New Features**
* Added a new method for verifying user sign-in status using a dedicated
configuration handler.
* Introduced a class to manage connection configuration and status
checks.

* **Refactor**
* Updated logic for checking connection and registration status to use
new configuration handling methods for improved clarity and reliability.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Pujit Mehrotra
2025-07-15 10:30:04 -04:00
committed by GitHub
parent 29dcb7d0f0
commit 441e1805c1
3 changed files with 41 additions and 15 deletions

View File

@@ -166,22 +166,23 @@ _enabled() {
return 1
}
_connected() {
CFG=$API_CONFIG_HOME/connect.json
[[ ! -f "${CFG}" ]] && return 1
local connect_config username status_cfg connection_status
connect_config=$API_CONFIG_HOME/connect.json
[[ ! -f "${connect_config}" ]] && return 1
username=$(jq -r '.username // empty' "${CFG}" 2>/dev/null)
# is the user signed in?
username=$(jq -r '.username // empty' "${connect_config}" 2>/dev/null)
if [ -z "${username}" ]; then
return 1
fi
# the minigraph status is no longer synced to the connect config file
# to avoid a false negative, we'll omit this check for now.
#
# shellcheck disable=SC1090
# source <(sed -nr '/\[connectionStatus\]/,/\[/{/minigraph/p}' "${CFG}" 2>/dev/null)
# # ensure connected
# if [[ -z "${minigraph}" || "${minigraph}" != "CONNECTED" ]]; then
# return 1
# fi
# are we connected to mothership?
status_cfg="/var/local/emhttp/connectStatus.json"
[[ ! -f "${status_cfg}" ]] && return 1
connection_status=$(jq -r '.connectionStatus // empty' "${status_cfg}" 2>/dev/null)
if [[ "${connection_status}" != "CONNECTED" ]]; then
return 1
fi
return 0
}
_haserror() {

View File

@@ -18,10 +18,9 @@ $cli = php_sapi_name()=='cli';
$docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
require_once "$docroot/webGui/include/Wrappers.php";
require_once "$docroot/plugins/dynamix.my.servers/include/connect-config.php";
$myservers_flash_cfg_path='/boot/config/plugins/dynamix.my.servers/myservers.cfg';
$myservers = file_exists($myservers_flash_cfg_path) ? @parse_ini_file($myservers_flash_cfg_path,true) : [];
$isRegistered = !empty($myservers['remote']['username']);
$isRegistered = ConnectConfig::isUserSignedIn();
// Read connection status from the new API status file
$statusFilePath = '/var/local/emhttp/connectStatus.json';

View File

@@ -0,0 +1,26 @@
<?php
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
require_once "$docroot/plugins/dynamix.my.servers/include/api-config.php";
/**
* Wrapper around the API's connect.json configuration file.
*/
class ConnectConfig
{
public const CONFIG_PATH = ApiConfig::CONFIG_DIR . '/connect.json';
public static function getConfig()
{
try {
return json_decode(file_get_contents(self::CONFIG_PATH), true) ?? [];
} catch (Throwable $e) {
return [];
}
}
public static function isUserSignedIn()
{
$config = self::getConfig();
return ApiConfig::isConnectPluginEnabled() && !empty($config['username'] ?? '');
}
}