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\Circles\CirclesManager;
30use OCA\Circles\Model\Member;
31use OCP\App\IAppManager;
32
33/**
34 * Wrapper around circles app API since it is not in a public namespace so we need to make sure that
35 * having the app disabled is properly handled
36 */
37class CirclesService {
38	private $circlesEnabled;
39
40	public function __construct(IAppManager $appManager) {
41		$this->circlesEnabled = $appManager->isEnabledForUser('circles');
42	}
43
44	public function isCirclesEnabled(): bool {
45		return $this->circlesEnabled;
46	}
47
48	public function getCircle($circleId) {
49		if (!$this->circlesEnabled) {
50			return null;
51		}
52
53		return \OCA\Circles\Api\v1\Circles::detailsCircle($circleId, true);
54	}
55
56	public function isUserInCircle($circleId, $userId): bool {
57		if (!$this->circlesEnabled) {
58			return false;
59		}
60
61		try {
62			/** @var CirclesManager $circlesManager */
63			$circlesManager = \OC::$server->get(CirclesManager::class);
64			$federatedUser = $circlesManager->getFederatedUser($userId, Member::TYPE_USER);
65			$circlesManager->startSession($federatedUser);
66			$circle = $circlesManager->getCircle($circleId);
67			$member = $circle->getInitiator();
68			return $member !== null && $member->getLevel() >= Member::LEVEL_MEMBER;
69		} catch (\Exception $e) {
70		}
71		return false;
72	}
73}
74