Skip to content

Commit

Permalink
Add calendar setting and move to more generic config ocs routes
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Sep 15, 2020
1 parent 2f44532 commit 1b16dba
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 99 deletions.
9 changes: 3 additions & 6 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
'routes' => [
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],

['name' => 'Config#get', 'url' => '/config', 'verb' => 'GET'],
['name' => 'Config#setValue', 'url' => '/config/{key}', 'verb' => 'POST'],

// boards
['name' => 'board#index', 'url' => '/boards', 'verb' => 'GET'],
['name' => 'board#create', 'url' => '/boards', 'verb' => 'POST'],
Expand Down Expand Up @@ -125,17 +122,17 @@
['name' => 'attachment_api#delete', 'url' => '/api/v1.0/boards/{boardId}/stacks/{stackId}/cards/{cardId}/attachments/{attachmentId}', 'verb' => 'DELETE'],
['name' => 'attachment_api#restore', 'url' => '/api/v1.0/boards/{boardId}/stacks/{stackId}/cards/{cardId}/attachments/{attachmentId}/restore', 'verb' => 'PUT'],



['name' => 'board_api#preflighted_cors', 'url' => '/api/v1.0/{path}','verb' => 'OPTIONS', 'requirements' => ['path' => '.+']],
],
'ocs' => [
['name' => 'Config#get', 'url' => '/api/v1.0/config', 'verb' => 'GET'],
['name' => 'Config#setValue', 'url' => '/api/v1.0/config/{key}', 'verb' => 'POST'],

['name' => 'comments_api#list', 'url' => '/api/v1.0/cards/{cardId}/comments', 'verb' => 'GET'],
['name' => 'comments_api#create', 'url' => '/api/v1.0/cards/{cardId}/comments', 'verb' => 'POST'],
['name' => 'comments_api#update', 'url' => '/api/v1.0/cards/{cardId}/comments/{commentId}', 'verb' => 'PUT'],
['name' => 'comments_api#delete', 'url' => '/api/v1.0/cards/{cardId}/comments/{commentId}', 'verb' => 'DELETE'],

// dashboard
['name' => 'overview_api#upcomingCards', 'url' => '/api/v1.0/overview/upcoming', 'verb' => 'GET'],
]
];
72 changes: 12 additions & 60 deletions lib/Controller/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
10 changes: 5 additions & 5 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -64,6 +63,7 @@ public function __construct(
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');

Expand Down
18 changes: 17 additions & 1 deletion lib/DAV/CalendarPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,51 @@
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 {
return 'deck';
}

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());
return in_array($calendarUri, $boards, true);
}

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));
Expand Down
129 changes: 129 additions & 0 deletions lib/Service/ConfigService.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit 1b16dba

Please sign in to comment.