Fix: Add graceful handling for unconfigured state, fix 500 error on unconfigured plugin
This commit is contained in:
@@ -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,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user