1<?php
2/**
3 * Circles - Bring cloud-users closer together.
4 *
5 * This file is licensed under the Affero General Public License version 3 or
6 * later. See the COPYING file.
7 *
8 * @author Maxence Lange <maxence@artificial-owl.com>
9 * @author Vinicius Cubas Brand <vinicius@eita.org.br>
10 * @author Daniel Tygel <dtygel@eita.org.br>
11 *
12 * @copyright 2017
13 * @license GNU AGPL version 3 or any later version
14 *
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Affero General Public License as
17 * published by the Free Software Foundation, either version 3 of the
18 * License, or (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 * GNU Affero General Public License for more details.
24 *
25 * You should have received a copy of the GNU Affero General Public License
26 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27 *
28 */
29
30namespace OCA\Circles\Api\v1;
31
32use OCA\Circles\Exceptions\CircleNotFoundException;
33use OCA\Circles\Exceptions\FederatedUserException;
34use OCA\Circles\Exceptions\FederatedUserNotFoundException;
35use OCA\Circles\Exceptions\InitiatorNotFoundException;
36use OCA\Circles\Exceptions\InvalidIdException;
37use OCA\Circles\Exceptions\RequestBuilderException;
38use OCA\Circles\Exceptions\SingleCircleNotFoundException;
39use OCA\Circles\Model\Circle;
40use OCA\Circles\Model\Member;
41use OCA\Circles\Model\Probes\CircleProbe;
42use OCA\Circles\Service\CircleService;
43use OCA\Circles\Service\FederatedUserService;
44
45class Circles {
46	public const API_VERSION = [0, 10, 0];
47
48	// Expose circle and member constants via API
49	public const CIRCLES_PERSONAL = 1;
50	public const CIRCLES_SECRET = 2;
51	public const CIRCLES_CLOSED = 4;
52	public const CIRCLES_PUBLIC = 8;
53	public const CIRCLES_ALL = 15;
54
55	public const TYPE_USER = Member::TYPE_USER;
56	public const TYPE_GROUP = Member::TYPE_GROUP;
57	public const TYPE_MAIL = Member::TYPE_MAIL;
58	public const TYPE_CONTACT = Member::TYPE_CONTACT;
59
60	public const LEVEL_NONE = Member::LEVEL_NONE;
61	public const LEVEL_MEMBER = Member::LEVEL_MEMBER;
62	public const LEVEL_MODERATOR = Member::LEVEL_MODERATOR;
63	public const LEVEL_ADMIN = Member::LEVEL_ADMIN;
64	public const LEVEL_OWNER = Member::LEVEL_OWNER;
65
66
67	/**
68	 * Circles::listCircles();
69	 *
70	 * This function list all circles fitting a search regarding its name and the level and the
71	 * rights from the current user. In case of Secret circle, name needs to be complete so the
72	 * circle is included in the list (or if the current user is the owner)
73	 *
74	 * example: Circles::listCircles(Circles::CIRCLES_ALL, '', 8, callback); will returns all
75	 * circles when the current user is at least an Admin.
76	 *
77	 * @param mixed $type
78	 * @param string $name
79	 * @param int $level
80	 * @param string $userId
81	 * @param bool $forceAll
82	 *
83	 * @return Circle[]
84	 */
85	public static function listCircles($type, $name = '', $level = 0, $userId = '', $forceAll = false) {
86		/** @var FederatedUserService $federatedUserService */
87		$federatedUserService = \OC::$server->get(FederatedUserService::class);
88
89		$personalCircle = false;
90		if ($forceAll) {
91			$personalCircle = true;
92		}
93
94		if ($userId === '') {
95			$federatedUserService->initCurrentUser();
96		} else {
97			$federatedUserService->setLocalCurrentUserId($userId);
98		}
99
100		/** @var CircleService $circleService */
101		$circleService = \OC::$server->get(CircleService::class);
102
103		$probe = new CircleProbe();
104		$probe->includePersonalCircles($personalCircle);
105		$probe->filterHiddenCircles();
106
107		return $circleService->getCircles($probe);
108	}
109
110
111	/**
112	 * @param string $userId
113	 * @param bool $forceAll
114	 *
115	 * @return Circle[]
116	 * @throws FederatedUserException
117	 * @throws FederatedUserNotFoundException
118	 * @throws InitiatorNotFoundException
119	 * @throws InvalidIdException
120	 * @throws RequestBuilderException
121	 * @throws SingleCircleNotFoundException
122	 *
123	 * @deprecated - used by apps/dav/lib/Connector/Sabre/Principal.php
124	 *
125	 * Circles::joinedCircles();
126	 *
127	 * Return all the circle the current user is a member.
128	 */
129	public static function joinedCircles($userId = '', $forceAll = false) {
130		/** @var FederatedUserService $federatedUserService */
131		$federatedUserService = \OC::$server->get(FederatedUserService::class);
132
133		$personalCircle = false;
134		if ($forceAll) {
135			$personalCircle = true;
136		}
137
138		if ($userId === '') {
139			$federatedUserService->initCurrentUser();
140		} else {
141			$federatedUserService->setLocalCurrentUserId($userId);
142		}
143
144		/** @var CircleService $circleService */
145		$circleService = \OC::$server->get(CircleService::class);
146
147		$probe = new CircleProbe();
148		$probe->mustBeMember();
149		$probe->includePersonalCircles($personalCircle);
150		$probe->filterHiddenCircles();
151
152		return $circleService->getCircles($probe);
153	}
154
155
156	/**
157	 * @param string $circleUniqueId
158	 * @param bool $forceAll
159	 *
160	 * @return Circle
161	 * @throws CircleNotFoundException
162	 * @throws FederatedUserException
163	 * @throws FederatedUserNotFoundException
164	 * @throws InitiatorNotFoundException
165	 * @throws InvalidIdException
166	 * @throws RequestBuilderException
167	 * @throws SingleCircleNotFoundException
168	 *
169	 * @deprecated - used by apps/dav/lib/Connector/Sabre/Principal.php
170	 *             - used by apps/files_sharing/lib/Controller/ShareAPIController.php
171	 *             - used by lib/private/Share20/Manager.php
172	 *
173	 * Circles::detailsCircle();
174	 *
175	 * WARNING - This function is called by the core - WARNING
176	 *                 Do not change it
177	 *
178	 * Returns details on the circle. If the current user is a member, the members list will be
179	 * return as well.
180	 *
181	 */
182	public static function detailsCircle(string $circleUniqueId, bool $forceAll = false): Circle {
183		/** @var FederatedUserService $federatedUserService */
184		$federatedUserService = \OC::$server->get(FederatedUserService::class);
185		if ($forceAll) {
186			$federatedUserService->bypassCurrentUserCondition($forceAll);
187		} else {
188			$federatedUserService->initCurrentUser();
189		}
190
191		/** @var CircleService $circleService */
192		$circleService = \OC::$server->get(CircleService::class);
193
194		return $circleService->getCircle($circleUniqueId);
195	}
196
197
198	/**
199	 * @param string $circleUniqueId
200	 * @param string $ident
201	 * @param int $type
202	 * @param bool $forceAll
203	 *
204	 * @return Member
205	 *
206	 * @deprecated - used by apps/files_sharing/lib/Controller/ShareAPIController.php
207	 *
208	 * Circles::getMember();
209	 *
210	 * This function will return information on a member of the circle. Current user need at least
211	 * to be Member.
212	 *
213	 */
214	public static function getMember($circleUniqueId, $ident, $type, $forceAll = false) {
215		throw new \BadMethodCallException('Method is deprecated and not longer works');
216	}
217
218
219	/**
220	 * @param array $circleUniqueIds
221	 *
222	 * @return string[] array of object ids or empty array if none found
223	 *
224	 * @deprecated - used by apps/dav/lib/Connector/Sabre/FilesReportPlugin.php
225	 *
226	 * Get a list of objects which are shred with $circleUniqueId.
227	 *
228	 * @since 0.14.0
229	 *
230	 */
231	public static function getFilesForCircles($circleUniqueIds) {
232		throw new \BadMethodCallException('Method is deprecated and not longer works');
233	}
234}
235