Files
nextcloud-analytics/analyticshub/lib/Controller/AdminController.php

110 lines
3.2 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;
/**
* 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', 'analyticshub', '');
$apiKey = $this->config->getAppValue('anthropic_api_key', 'analyticshub', '');
$refreshToken = $this->config->getAppValue('google_refresh_token', 'analyticshub', '');
// 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', 'analyticshub', $clientId);
$this->config->setAppValue('google_client_secret', 'analyticshub', $clientSecret);
if (!empty($refreshToken)) {
$this->config->setAppValue('google_refresh_token', 'analyticshub', $refreshToken);
}
$this->config->setAppValue('anthropic_api_key', 'analyticshub', $apiKey);
// Check if fully configured
$isConfigured = !empty($clientId) && !empty($clientSecret) && !empty($apiKey) && !empty($refreshToken);
return new JSONResponse([
'success' => true,
'data' => [
'is_configured' => $isConfigured,
],
]);
}
}