Fix: Add graceful handling for unconfigured state, fix 500 error on unconfigured plugin

This commit is contained in:
WLTBAgent
2026-02-16 16:15:49 +00:00
parent 30d14cdb7d
commit a8bf6bcb6c
5 changed files with 386 additions and 287 deletions

View File

@@ -5,8 +5,10 @@ declare(strict_types=1);
namespace OCA\AnalyticsHub\Controller\Admin;
use OCP\IRequest;
use OCP\IConfig;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCA\AnalyticsHub\AppInfo\Application;
/**
* Admin Settings Controller
@@ -18,20 +20,89 @@ class AdminController {
private $appName;
private $request;
private $config;
public function __construct(string $appName, IRequest $request) {
public function __construct(string $appName, IRequest $request, IConfig $config) {
$this->appName = $appName;
$this->request = $request;
$this->config = $config;
}
/**
* Load configuration
*/
public function load(): JSONResponse {
$clientId = $this->config->getAppValue('google_client_id', Application::APP_NAME, '');
$apiKey = $this->config->getAppValue('anthropic_api_key', Application::APP_NAME, '');
$refreshToken = $this->config->getAppValue('google_refresh_token', Application::APP_NAME, '');
// Check if configured
$isConfigured = !empty($clientId) && !empty($apiKey) && !empty($refreshToken);
// Mask sensitive values for display
$maskedApiKey = '';
if (!empty($apiKey)) {
$maskedApiKey = substr($apiKey, 0, 8) . '...' . substr($apiKey, -4);
}
$maskedRefreshToken = '';
if (!empty($refreshToken)) {
$maskedRefreshToken = substr($refreshToken, 0, 10) . '...';
}
return new JSONResponse([
'success' => true,
'data' => [
'message' => 'Use PageController instead of AdminController',
'google_client_id' => $clientId,
'google_refresh_token' => $maskedRefreshToken,
'anthropic_api_key' => $maskedApiKey,
'is_configured' => $isConfigured,
],
]);
}
/**
* Save configuration
*/
public function save(): JSONResponse {
$data = json_decode($this->request->getRawBody(), true);
if (!$data) {
return new JSONResponse([
'success' => false,
'error' => 'Invalid request body',
], 400);
}
$clientId = $data['google_client_id'] ?? '';
$clientSecret = $data['google_client_secret'] ?? '';
$refreshToken = $data['google_refresh_token'] ?? '';
$apiKey = $data['anthropic_api_key'] ?? '';
if (empty($clientId) || empty($clientSecret) || empty($apiKey)) {
return new JSONResponse([
'success' => false,
'error' => 'Missing required fields',
], 400);
}
// Save configuration
$this->config->setAppValue('google_client_id', Application::APP_NAME, $clientId);
$this->config->setAppValue('google_client_secret', Application::APP_NAME, $clientSecret);
if (!empty($refreshToken)) {
$this->config->setAppValue('google_refresh_token', Application::APP_NAME, $refreshToken);
}
$this->config->setAppValue('anthropic_api_key', Application::APP_NAME, $apiKey);
// Check if fully configured
$isConfigured = !empty($clientId) && !empty($clientSecret) && !empty($apiKey) && !empty($refreshToken);
return new JSONResponse([
'success' => true,
'data' => [
'is_configured' => $isConfigured,
],
]);
}

View File

@@ -5,8 +5,12 @@ declare(strict_types=1);
namespace OCA\AnalyticsHub\Controller\Admin;
use OCP\IRequest;
use OCP\IConfig;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
use OCA\AnalyticsHub\AppInfo\Application;
use OCA\AnalyticsHub\Service\GoogleAnalyticsService;
use OCA\AnalyticsHub\Service\LLMService;
/**
* Admin Settings Controller
@@ -17,10 +21,22 @@ use OCA\AnalyticsHub\AppInfo\Application;
class PageController extends Controller {
private $appName;
private $config;
private $gaService;
private $llmService;
public function __construct(string $appName, IRequest $request) {
public function __construct(
string $appName,
IRequest $request,
IConfig $config,
GoogleAnalyticsService $gaService,
LLMService $llmService
) {
parent::__construct($appName, $request);
$this->appName = $appName;
$this->config = $config;
$this->gaService = $gaService;
$this->llmService = $llmService;
}
/**
@@ -30,10 +46,40 @@ class PageController extends Controller {
* @NoCSRFRequired
*/
public function index(): TemplateResponse {
// Check configuration status gracefully
$isConfigured = false;
$isGAConfigured = false;
$isLLMConfigured = false;
try {
$isGAConfigured = $this->gaService->isConfigured();
$isLLMConfigured = $this->llmService->isConfigured();
$isConfigured = $isGAConfigured && $isLLMConfigured;
} catch (\Exception $e) {
// If service initialization fails, app is not configured
$isConfigured = false;
}
// Get configuration values (masked for secrets)
$clientId = $this->config->getAppValue('google_client_id', Application::APP_NAME, '');
$apiKey = $this->config->getAppValue('anthropic_api_key', Application::APP_NAME, '');
// Mask API key for display
$maskedApiKey = '';
if (!empty($apiKey)) {
$maskedApiKey = substr($apiKey, 0, 8) . '...' . substr($apiKey, -4);
}
return new TemplateResponse($this->appName, 'admin', [
'app_name' => $this->appName,
'version' => Application::APP_VERSION,
'status' => 'Ready for development',
'is_configured' => $isConfigured,
'is_ga_configured' => $isGAConfigured,
'is_llm_configured' => $isLLMConfigured,
'google_client_id' => $clientId,
'anthropic_api_key_masked' => $maskedApiKey,
'request' => $this->request,
]);
}
}