/
kev
/
Nextcloud_File-Changes-Tracker
Обзор
Документация
Войти
/
kev
/
Nextcloud_File-Changes-Tracker
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
lib/Controller/FolderConfigController.php
160 строк
6 KB
Evgeny Konovalov
first_commit
16 фев 2026, 17:45
16 фев 2026, 17:45
476f06c
Код
Авторство
О чём код?
<?php declare(strict_types=1); namespace OCA\FileChangesTracker\Controller; use OCA\FileChangesTracker\Service\FolderConfigService; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\JSONResponse; use OCP\IGroupManager; use OCP\IRequest; use OCP\IUserSession; /** * Контроллер для управления конфигурацией отслеживаемых папок */ class FolderConfigController extends Controller { private FolderConfigService $service; private IUserSession $userSession; private IGroupManager $groupManager; public function __construct( string $appName, IRequest $request, FolderConfigService $service, IUserSession $userSession, IGroupManager $groupManager ) { parent::__construct($appName, $request); $this->service = $service; $this->userSession = $userSession; $this->groupManager = $groupManager; } /** * Получить список всех конфигураций * * @NoAdminRequired */ public function list(): JSONResponse { try { $user = $this->userSession->getUser(); if (!$user) { return new JSONResponse(['error' => 'Пользователь не авторизован'], Http::STATUS_UNAUTHORIZED); } $userId = $user->getUID(); // Получаем все конфигурации (администратор видит все, обычные пользователи - только свои) if ($this->groupManager->isAdmin($userId)) { $configs = $this->service->findAll(); } else { $configs = $this->service->findByResponsible($userId, 'user'); } return new JSONResponse(['data' => $configs], Http::STATUS_OK); } catch (\Exception $e) { return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); } } /** * Получить конфигурацию по ID * * @NoAdminRequired */ public function get(int $id): JSONResponse { try { $config = $this->service->find($id); if ($config === null) { return new JSONResponse(['error' => 'Конфигурация не найдена'], Http::STATUS_NOT_FOUND); } return new JSONResponse(['data' => $config], Http::STATUS_OK); } catch (\Exception $e) { return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); } } /** * Создать новую конфигурацию * * @NoAdminRequired */ public function create(): JSONResponse { try { $user = $this->userSession->getUser(); if (!$user) { return new JSONResponse(['error' => 'Пользователь не авторизован'], Http::STATUS_UNAUTHORIZED); } $folderPath = $this->request->getParam('folderPath'); $responsibleType = $this->request->getParam('responsibleType'); $responsibleId = $this->request->getParam('responsibleId'); $notificationsEnabled = $this->request->getParam('notificationsEnabled', true); if (empty($folderPath) || empty($responsibleType) || empty($responsibleId)) { return new JSONResponse(['error' => 'Не все обязательные поля заполнены'], Http::STATUS_BAD_REQUEST); } $config = $this->service->create( $folderPath, $responsibleType, $responsibleId, (bool)$notificationsEnabled, $user->getUID() ); return new JSONResponse(['data' => $config, 'message' => 'Конфигурация создана'], Http::STATUS_CREATED); } catch (\Exception $e) { return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); } } /** * Обновить конфигурацию * * @NoAdminRequired */ public function update(int $id): JSONResponse { try { $folderPath = $this->request->getParam('folderPath'); $responsibleType = $this->request->getParam('responsibleType'); $responsibleId = $this->request->getParam('responsibleId'); $notificationsEnabled = $this->request->getParam('notificationsEnabled', true); if (empty($folderPath) || empty($responsibleType) || empty($responsibleId)) { return new JSONResponse(['error' => 'Не все обязательные поля заполнены'], Http::STATUS_BAD_REQUEST); } $config = $this->service->update( $id, $folderPath, $responsibleType, $responsibleId, (bool)$notificationsEnabled ); return new JSONResponse(['data' => $config, 'message' => 'Конфигурация обновлена'], Http::STATUS_OK); } catch (\Exception $e) { return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); } } /** * Удалить конфигурацию * * @NoAdminRequired */ public function delete(int $id): JSONResponse { try { $this->service->delete($id); return new JSONResponse(['message' => 'Конфигурация удалена'], Http::STATUS_OK); } catch (\Exception $e) { return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); } } }