1<?php
2/**
3 * @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
4 *
5 * @author Julius Härtl <jus@bitgrid.net>
6 *
7 * @license GNU AGPL version 3 or any later version
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24declare(strict_types=1);
25
26
27namespace OCA\Deck\Service;
28
29use OCA\Deck\AppInfo\Application;
30use OCA\Deck\BadRequestException;
31use OCA\Deck\NoPermissionException;
32use OCP\IConfig;
33use OCP\IGroup;
34use OCP\IGroupManager;
35use OCP\IUserSession;
36
37class ConfigService {
38	public const SETTING_BOARD_NOTIFICATION_DUE_OFF = 'off';
39	public const SETTING_BOARD_NOTIFICATION_DUE_ASSIGNED = 'assigned';
40	public const SETTING_BOARD_NOTIFICATION_DUE_ALL = 'all';
41	public const SETTING_BOARD_NOTIFICATION_DUE_DEFAULT = self::SETTING_BOARD_NOTIFICATION_DUE_ASSIGNED;
42
43	private $config;
44	private $userId;
45	private $groupManager;
46
47	public function __construct(
48		IConfig $config,
49		IGroupManager $groupManager
50	) {
51		$this->groupManager = $groupManager;
52		$this->config = $config;
53	}
54
55	public function getUserId() {
56		if (!$this->userId) {
57			$user = \OC::$server->get(IUserSession::class)->getUser();
58			$this->userId = $user ? $user->getUID() : null;
59		}
60		return $this->userId;
61	}
62
63	public function getAll(): array {
64		if ($this->getUserId() === null) {
65			return [];
66		}
67
68		$data = [
69			'calendar' => $this->isCalendarEnabled()
70		];
71		if ($this->groupManager->isAdmin($this->getUserId())) {
72			$data['groupLimit'] = $this->get('groupLimit');
73		}
74		return $data;
75	}
76
77	public function get($key) {
78		$result = null;
79		[$scope] = explode(':', $key, 2);
80		switch ($scope) {
81			case 'groupLimit':
82				if ($this->getUserId() === null || !$this->groupManager->isAdmin($this->getUserId())) {
83					throw new NoPermissionException('You must be admin to get the group limit');
84				}
85				return $this->getGroupLimit();
86			case 'calendar':
87				if ($this->getUserId() === null) {
88					return false;
89				}
90				return (bool)$this->config->getUserValue($this->getUserId(), Application::APP_ID, 'calendar', true);
91		}
92	}
93
94	public function isCalendarEnabled(int $boardId = null): bool {
95		if ($this->getUserId() === null) {
96			return false;
97		}
98
99		$defaultState = (bool)$this->config->getUserValue($this->getUserId(), Application::APP_ID, 'calendar', true);
100		if ($boardId === null) {
101			return $defaultState;
102		}
103
104		return (bool)$this->config->getUserValue($this->getUserId(), Application::APP_ID, 'board:' . $boardId . ':calendar', $defaultState);
105	}
106
107	public function set($key, $value) {
108		if ($this->getUserId() === null) {
109			throw new NoPermissionException('Must be logged in to set user config');
110		}
111
112		$result = null;
113		[$scope] = explode(':', $key, 2);
114		switch ($scope) {
115			case 'groupLimit':
116				if (!$this->groupManager->isAdmin($this->getUserId())) {
117					throw new NoPermissionException('You must be admin to set the group limit');
118				}
119				$result = $this->setGroupLimit($value);
120				break;
121			case 'calendar':
122				$this->config->setUserValue($this->getUserId(), Application::APP_ID, 'calendar', (string)$value);
123				$result = $value;
124				break;
125			case 'board':
126				[$boardId, $boardConfigKey] = explode(':', $key);
127				if ($boardConfigKey === 'notify-due' && !in_array($value, [self::SETTING_BOARD_NOTIFICATION_DUE_ALL, self::SETTING_BOARD_NOTIFICATION_DUE_ASSIGNED, self::SETTING_BOARD_NOTIFICATION_DUE_OFF], true)) {
128					throw new BadRequestException('Board notification option must be one of: off, assigned, all');
129				}
130				$this->config->setUserValue($this->getUserId(), Application::APP_ID, $key, (string)$value);
131				$result = $value;
132		}
133		return $result;
134	}
135
136	private function setGroupLimit($value) {
137		$groups = [];
138		foreach ($value as $group) {
139			$groups[] = $group['id'];
140		}
141		$data = implode(',', $groups);
142		$this->config->setAppValue(Application::APP_ID, 'groupLimit', $data);
143		return $groups;
144	}
145
146	private function getGroupLimitList() {
147		$value = $this->config->getAppValue(Application::APP_ID, 'groupLimit', '');
148		$groups = explode(',', $value);
149		if ($value === '') {
150			return [];
151		}
152		return $groups;
153	}
154
155	private function getGroupLimit() {
156		$groups = $this->getGroupLimitList();
157		$groups = array_map(function ($groupId) {
158			/** @var IGroup $groups */
159			$group = $this->groupManager->get($groupId);
160			if ($group === null) {
161				return null;
162			}
163			return [
164				'id' => $group->getGID(),
165				'displayname' => $group->getDisplayName(),
166			];
167		}, $groups);
168		return array_filter($groups);
169	}
170
171	public function getAttachmentFolder(): string {
172		if ($this->getUserId() === null) {
173			throw new NoPermissionException('Must be logged in get the attachment folder');
174		}
175
176		return $this->config->getUserValue($this->getUserId(), 'deck', 'attachment_folder', '/Deck');
177	}
178}
179