Add calendar setting and move to more generic config ocs routes
Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
@@ -23,90 +23,42 @@
|
||||
|
||||
namespace OCA\Deck\Controller;
|
||||
|
||||
use OCA\Deck\Service\ConfigService;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
use OCP\AppFramework\Http\NotFoundResponse;
|
||||
use OCP\IConfig;
|
||||
use OCP\IGroup;
|
||||
use OCP\IGroupManager;
|
||||
use OCP\AppFramework\OCSController;
|
||||
use OCP\IRequest;
|
||||
use OCP\AppFramework\Controller;
|
||||
|
||||
class ConfigController extends Controller {
|
||||
private $config;
|
||||
private $userId;
|
||||
private $groupManager;
|
||||
class ConfigController extends OCSController {
|
||||
private $configService;
|
||||
|
||||
public function __construct(
|
||||
$AppName,
|
||||
IRequest $request,
|
||||
IConfig $config,
|
||||
IGroupManager $groupManager,
|
||||
$userId
|
||||
ConfigService $configService
|
||||
) {
|
||||
parent::__construct($AppName, $request);
|
||||
|
||||
$this->userId = $userId;
|
||||
$this->groupManager = $groupManager;
|
||||
$this->config = $config;
|
||||
$this->configService = $configService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoCSRFRequired
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function get() {
|
||||
$data = [
|
||||
'groupLimit' => $this->getGroupLimit(),
|
||||
];
|
||||
return new DataResponse($data);
|
||||
public function get(): DataResponse {
|
||||
return new DataResponse($this->configService->getAll());
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoCSRFRequired
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function setValue($key, $value) {
|
||||
switch ($key) {
|
||||
case 'groupLimit':
|
||||
$result = $this->setGroupLimit($value);
|
||||
break;
|
||||
}
|
||||
public function setValue(string $key, $value) {
|
||||
$result = $this->configService->set($key, $value);
|
||||
if ($result === null) {
|
||||
return new NotFoundResponse();
|
||||
}
|
||||
return new DataResponse($result);
|
||||
}
|
||||
|
||||
private function setGroupLimit($value) {
|
||||
$groups = [];
|
||||
foreach ($value as $group) {
|
||||
$groups[] = $group['id'];
|
||||
}
|
||||
$data = implode(',', $groups);
|
||||
$this->config->setAppValue($this->appName, 'groupLimit', $data);
|
||||
return $groups;
|
||||
}
|
||||
|
||||
private function getGroupLimitList() {
|
||||
$value = $this->config->getAppValue($this->appName, 'groupLimit', '');
|
||||
$groups = explode(',', $value);
|
||||
if ($value === '') {
|
||||
return [];
|
||||
}
|
||||
return $groups;
|
||||
}
|
||||
|
||||
private function getGroupLimit() {
|
||||
$groups = $this->getGroupLimitList();
|
||||
$groups = array_map(function ($groupId) {
|
||||
/** @var IGroup $groups */
|
||||
$group = $this->groupManager->get($groupId);
|
||||
if ($group === null) {
|
||||
return null;
|
||||
}
|
||||
return [
|
||||
'id' => $group->getGID(),
|
||||
'displayname' => $group->getDisplayName(),
|
||||
];
|
||||
}, $groups);
|
||||
return array_filter($groups);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,34 +24,33 @@
|
||||
namespace OCA\Deck\Controller;
|
||||
|
||||
use OCA\Deck\AppInfo\Application;
|
||||
use OCA\Deck\Service\ConfigService;
|
||||
use OCA\Deck\Service\PermissionService;
|
||||
use OCP\AppFramework\Http\ContentSecurityPolicy;
|
||||
use OCP\IInitialStateService;
|
||||
use OCP\IRequest;
|
||||
use OCP\AppFramework\Http\TemplateResponse;
|
||||
use OCP\AppFramework\Controller;
|
||||
use OCP\IL10N;
|
||||
|
||||
class PageController extends Controller {
|
||||
private $permissionService;
|
||||
private $userId;
|
||||
private $l10n;
|
||||
private $initialState;
|
||||
private $configService;
|
||||
|
||||
public function __construct(
|
||||
$AppName,
|
||||
IRequest $request,
|
||||
PermissionService $permissionService,
|
||||
IInitialStateService $initialStateService,
|
||||
IL10N $l10n,
|
||||
$userId
|
||||
ConfigService $configService
|
||||
) {
|
||||
parent::__construct($AppName, $request);
|
||||
|
||||
$this->userId = $userId;
|
||||
$this->permissionService = $permissionService;
|
||||
$this->initialState = $initialStateService;
|
||||
$this->l10n = $l10n;
|
||||
$this->configService = $configService;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,6 +63,7 @@ class PageController extends Controller {
|
||||
public function index() {
|
||||
$this->initialState->provideInitialState(Application::APP_ID, 'maxUploadSize', (int)\OCP\Util::uploadLimit());
|
||||
$this->initialState->provideInitialState(Application::APP_ID, 'canCreate', $this->permissionService->canCreate());
|
||||
$this->initialState->provideInitialState(Application::APP_ID, 'config', $this->configService->getAll());
|
||||
|
||||
$response = new TemplateResponse('deck', 'main');
|
||||
|
||||
|
||||
@@ -26,15 +26,19 @@ namespace OCA\Deck\DAV;
|
||||
use OCA\DAV\CalDAV\Integration\ExternalCalendar;
|
||||
use OCA\DAV\CalDAV\Integration\ICalendarProvider;
|
||||
use OCA\Deck\Db\Board;
|
||||
use OCA\Deck\Service\ConfigService;
|
||||
use Sabre\DAV\Exception\NotFound;
|
||||
|
||||
class CalendarPlugin implements ICalendarProvider {
|
||||
|
||||
/** @var DeckCalendarBackend */
|
||||
private $backend;
|
||||
/** @var bool */
|
||||
private $calendarIntegrationEnabled;
|
||||
|
||||
public function __construct(DeckCalendarBackend $backend) {
|
||||
public function __construct(DeckCalendarBackend $backend, ConfigService $configService) {
|
||||
$this->backend = $backend;
|
||||
$this->calendarIntegrationEnabled = $configService->get('calendar');
|
||||
}
|
||||
|
||||
public function getAppId(): string {
|
||||
@@ -42,12 +46,20 @@ class CalendarPlugin implements ICalendarProvider {
|
||||
}
|
||||
|
||||
public function fetchAllForCalendarHome(string $principalUri): array {
|
||||
if (!$this->calendarIntegrationEnabled) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_map(function (Board $board) use ($principalUri) {
|
||||
return new Calendar($principalUri, 'board-' . $board->getId(), $board, $this->backend);
|
||||
}, $this->backend->getBoards());
|
||||
}
|
||||
|
||||
public function hasCalendarInCalendarHome(string $principalUri, string $calendarUri): bool {
|
||||
if (!$this->calendarIntegrationEnabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$boards = array_map(static function (Board $board) {
|
||||
return 'board-' . $board->getId();
|
||||
}, $this->backend->getBoards());
|
||||
@@ -55,6 +67,10 @@ class CalendarPlugin implements ICalendarProvider {
|
||||
}
|
||||
|
||||
public function getCalendarInCalendarHome(string $principalUri, string $calendarUri): ?ExternalCalendar {
|
||||
if (!$this->calendarIntegrationEnabled) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->hasCalendarInCalendarHome($principalUri, $calendarUri)) {
|
||||
try {
|
||||
$board = $this->backend->getBoard((int)str_replace('board-', '', $calendarUri));
|
||||
|
||||
129
lib/Service/ConfigService.php
Normal file
129
lib/Service/ConfigService.php
Normal file
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace OCA\Deck\Service;
|
||||
|
||||
use OCA\Deck\AppInfo\Application;
|
||||
use OCA\Deck\NoPermissionException;
|
||||
use OCP\IConfig;
|
||||
use OCP\IGroup;
|
||||
use OCP\IGroupManager;
|
||||
|
||||
class ConfigService {
|
||||
private $config;
|
||||
private $userId;
|
||||
private $groupManager;
|
||||
|
||||
public function __construct(
|
||||
IConfig $config,
|
||||
IGroupManager $groupManager,
|
||||
$userId
|
||||
) {
|
||||
$this->userId = $userId;
|
||||
$this->groupManager = $groupManager;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function getAll(): array {
|
||||
$data = [
|
||||
'calendar' => $this->get('calendar')
|
||||
];
|
||||
if ($this->groupManager->isAdmin($this->userId)) {
|
||||
$data = [
|
||||
'groupLimit' => $this->get('groupLimit'),
|
||||
];
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function get($key) {
|
||||
$result = null;
|
||||
switch ($key) {
|
||||
case 'groupLimit':
|
||||
if (!$this->groupManager->isAdmin($this->userId)) {
|
||||
throw new NoPermissionException('You must be admin to get the group limit');
|
||||
}
|
||||
$result = $this->getGroupLimit();
|
||||
break;
|
||||
case 'calendar':
|
||||
$result = (bool)$this->config->getUserValue($this->userId, Application::APP_ID, 'calendar', true);
|
||||
break;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function set($key, $value) {
|
||||
$result = null;
|
||||
switch ($key) {
|
||||
case 'groupLimit':
|
||||
if (!$this->groupManager->isAdmin($this->userId)) {
|
||||
throw new NoPermissionException('You must be admin to set the group limit');
|
||||
}
|
||||
$result = $this->setGroupLimit($value);
|
||||
break;
|
||||
case 'calendar':
|
||||
$this->config->setUserValue($this->userId, Application::APP_ID, 'calendar', (int)$value);
|
||||
$result = $value;
|
||||
break;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function setGroupLimit($value) {
|
||||
$groups = [];
|
||||
foreach ($value as $group) {
|
||||
$groups[] = $group['id'];
|
||||
}
|
||||
$data = implode(',', $groups);
|
||||
$this->config->setAppValue(Application::APP_ID, 'groupLimit', $data);
|
||||
return $groups;
|
||||
}
|
||||
|
||||
private function getGroupLimitList() {
|
||||
$value = $this->config->getAppValue(Application::APP_ID, 'groupLimit', '');
|
||||
$groups = explode(',', $value);
|
||||
if ($value === '') {
|
||||
return [];
|
||||
}
|
||||
return $groups;
|
||||
}
|
||||
|
||||
private function getGroupLimit() {
|
||||
$groups = $this->getGroupLimitList();
|
||||
$groups = array_map(function ($groupId) {
|
||||
/** @var IGroup $groups */
|
||||
$group = $this->groupManager->get($groupId);
|
||||
if ($group === null) {
|
||||
return null;
|
||||
}
|
||||
return [
|
||||
'id' => $group->getGID(),
|
||||
'displayname' => $group->getDisplayName(),
|
||||
];
|
||||
}, $groups);
|
||||
return array_filter($groups);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user