You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GroupPrincipalBackend.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2018, Georg Ehrke
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\DAV;
  29. use OCP\Constants;
  30. use OCP\IConfig;
  31. use OCP\IGroup;
  32. use OCP\IGroupManager;
  33. use OCP\IUser;
  34. use OCP\IUserSession;
  35. use OCP\Share\IManager as IShareManager;
  36. use Sabre\DAV\Exception;
  37. use Sabre\DAV\PropPatch;
  38. use Sabre\DAVACL\PrincipalBackend\BackendInterface;
  39. class GroupPrincipalBackend implements BackendInterface {
  40. public const PRINCIPAL_PREFIX = 'principals/groups';
  41. /** @var IGroupManager */
  42. private $groupManager;
  43. /** @var IUserSession */
  44. private $userSession;
  45. /** @var IShareManager */
  46. private $shareManager;
  47. /** @var IConfig */
  48. private $config;
  49. /**
  50. * @param IGroupManager $IGroupManager
  51. * @param IUserSession $userSession
  52. * @param IShareManager $shareManager
  53. */
  54. public function __construct(
  55. IGroupManager $IGroupManager,
  56. IUserSession $userSession,
  57. IShareManager $shareManager,
  58. IConfig $config
  59. ) {
  60. $this->groupManager = $IGroupManager;
  61. $this->userSession = $userSession;
  62. $this->shareManager = $shareManager;
  63. $this->config = $config;
  64. }
  65. /**
  66. * Returns a list of principals based on a prefix.
  67. *
  68. * This prefix will often contain something like 'principals'. You are only
  69. * expected to return principals that are in this base path.
  70. *
  71. * You are expected to return at least a 'uri' for every user, you can
  72. * return any additional properties if you wish so. Common properties are:
  73. * {DAV:}displayname
  74. *
  75. * @param string $prefixPath
  76. * @return string[]
  77. */
  78. public function getPrincipalsByPrefix($prefixPath) {
  79. $principals = [];
  80. if ($prefixPath === self::PRINCIPAL_PREFIX) {
  81. foreach ($this->groupManager->search('') as $user) {
  82. $principals[] = $this->groupToPrincipal($user);
  83. }
  84. }
  85. return $principals;
  86. }
  87. /**
  88. * Returns a specific principal, specified by it's path.
  89. * The returned structure should be the exact same as from
  90. * getPrincipalsByPrefix.
  91. *
  92. * @param string $path
  93. * @return array
  94. */
  95. public function getPrincipalByPath($path) {
  96. $elements = explode('/', $path, 3);
  97. if ($elements[0] !== 'principals') {
  98. return null;
  99. }
  100. if ($elements[1] !== 'groups') {
  101. return null;
  102. }
  103. $name = urldecode($elements[2]);
  104. $group = $this->groupManager->get($name);
  105. if (!is_null($group)) {
  106. return $this->groupToPrincipal($group);
  107. }
  108. return null;
  109. }
  110. /**
  111. * Returns the list of members for a group-principal
  112. *
  113. * @param string $principal
  114. * @return string[]
  115. * @throws Exception
  116. */
  117. public function getGroupMemberSet($principal) {
  118. $elements = explode('/', $principal);
  119. if ($elements[0] !== 'principals') {
  120. return [];
  121. }
  122. if ($elements[1] !== 'groups') {
  123. return [];
  124. }
  125. $name = $elements[2];
  126. $group = $this->groupManager->get($name);
  127. if (is_null($group)) {
  128. return [];
  129. }
  130. return array_map(function ($user) {
  131. return $this->userToPrincipal($user);
  132. }, $group->getUsers());
  133. }
  134. /**
  135. * Returns the list of groups a principal is a member of
  136. *
  137. * @param string $principal
  138. * @return array
  139. * @throws Exception
  140. */
  141. public function getGroupMembership($principal) {
  142. return [];
  143. }
  144. /**
  145. * Updates the list of group members for a group principal.
  146. *
  147. * The principals should be passed as a list of uri's.
  148. *
  149. * @param string $principal
  150. * @param string[] $members
  151. * @throws Exception
  152. */
  153. public function setGroupMemberSet($principal, array $members) {
  154. throw new Exception('Setting members of the group is not supported yet');
  155. }
  156. /**
  157. * @param string $path
  158. * @param PropPatch $propPatch
  159. * @return int
  160. */
  161. public function updatePrincipal($path, PropPatch $propPatch) {
  162. return 0;
  163. }
  164. /**
  165. * @param string $prefixPath
  166. * @param array $searchProperties
  167. * @param string $test
  168. * @return array
  169. */
  170. public function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') {
  171. $results = [];
  172. if (\count($searchProperties) === 0) {
  173. return [];
  174. }
  175. if ($prefixPath !== self::PRINCIPAL_PREFIX) {
  176. return [];
  177. }
  178. // If sharing is disabled, return the empty array
  179. $shareAPIEnabled = $this->shareManager->shareApiEnabled();
  180. if (!$shareAPIEnabled) {
  181. return [];
  182. }
  183. // If sharing is restricted to group members only,
  184. // return only members that have groups in common
  185. $restrictGroups = false;
  186. if ($this->shareManager->shareWithGroupMembersOnly()) {
  187. $user = $this->userSession->getUser();
  188. if (!$user) {
  189. return [];
  190. }
  191. $restrictGroups = $this->groupManager->getUserGroupIds($user);
  192. }
  193. $searchLimit = $this->config->getSystemValueInt('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT);
  194. if ($searchLimit <= 0) {
  195. $searchLimit = null;
  196. }
  197. foreach ($searchProperties as $prop => $value) {
  198. switch ($prop) {
  199. case '{DAV:}displayname':
  200. $groups = $this->groupManager->search($value, $searchLimit);
  201. $results[] = array_reduce($groups, function (array $carry, IGroup $group) use ($restrictGroups) {
  202. $gid = $group->getGID();
  203. // is sharing restricted to groups only?
  204. if ($restrictGroups !== false) {
  205. if (!\in_array($gid, $restrictGroups, true)) {
  206. return $carry;
  207. }
  208. }
  209. $carry[] = self::PRINCIPAL_PREFIX . '/' . $gid;
  210. return $carry;
  211. }, []);
  212. break;
  213. case '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set':
  214. // If you add support for more search properties that qualify as a user-address,
  215. // please also add them to the array below
  216. $results[] = $this->searchPrincipals(self::PRINCIPAL_PREFIX, [
  217. ], 'anyof');
  218. break;
  219. default:
  220. $results[] = [];
  221. break;
  222. }
  223. }
  224. // results is an array of arrays, so this is not the first search result
  225. // but the results of the first searchProperty
  226. if (count($results) === 1) {
  227. return $results[0];
  228. }
  229. switch ($test) {
  230. case 'anyof':
  231. return array_values(array_unique(array_merge(...$results)));
  232. case 'allof':
  233. default:
  234. return array_values(array_intersect(...$results));
  235. }
  236. }
  237. /**
  238. * @param string $uri
  239. * @param string $principalPrefix
  240. * @return string
  241. */
  242. public function findByUri($uri, $principalPrefix) {
  243. // If sharing is disabled, return the empty array
  244. $shareAPIEnabled = $this->shareManager->shareApiEnabled();
  245. if (!$shareAPIEnabled) {
  246. return null;
  247. }
  248. // If sharing is restricted to group members only,
  249. // return only members that have groups in common
  250. $restrictGroups = false;
  251. if ($this->shareManager->shareWithGroupMembersOnly()) {
  252. $user = $this->userSession->getUser();
  253. if (!$user) {
  254. return null;
  255. }
  256. $restrictGroups = $this->groupManager->getUserGroupIds($user);
  257. }
  258. if (strpos($uri, 'principal:principals/groups/') === 0) {
  259. $name = urlencode(substr($uri, 28));
  260. if ($restrictGroups !== false && !\in_array($name, $restrictGroups, true)) {
  261. return null;
  262. }
  263. return substr($uri, 10);
  264. }
  265. return null;
  266. }
  267. /**
  268. * @param IGroup $group
  269. * @return array
  270. */
  271. protected function groupToPrincipal($group) {
  272. $groupId = $group->getGID();
  273. // getDisplayName returns UID if none
  274. $displayName = $group->getDisplayName();
  275. return [
  276. 'uri' => 'principals/groups/' . urlencode($groupId),
  277. '{DAV:}displayname' => $displayName,
  278. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
  279. ];
  280. }
  281. /**
  282. * @param IUser $user
  283. * @return array
  284. */
  285. protected function userToPrincipal($user) {
  286. $userId = $user->getUID();
  287. // getDisplayName returns UID if none
  288. $displayName = $user->getDisplayName();
  289. $principal = [
  290. 'uri' => 'principals/users/' . $userId,
  291. '{DAV:}displayname' => $displayName,
  292. '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
  293. ];
  294. $email = $user->getEMailAddress();
  295. if (!empty($email)) {
  296. $principal['{http://sabredav.org/ns}email-address'] = $email;
  297. }
  298. return $principal;
  299. }
  300. }