111 lines
3.3 KiB
PHP
111 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace OCA\AnalyticsHub\Controller;
|
|
|
|
use OCP\IRequest;
|
|
use OCP\IConfig;
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
use OCA\AnalyticsHub\AppInfo\Application;
|
|
|
|
/**
|
|
* Admin Settings Controller
|
|
*
|
|
* @NoAdminRequired
|
|
* @NoCSRFRequired
|
|
*/
|
|
class AdminController {
|
|
|
|
protected $appName;
|
|
protected $request;
|
|
protected $config;
|
|
|
|
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' => [
|
|
'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,
|
|
],
|
|
]);
|
|
}
|
|
}
|
|
|