GroupPrincipalBackend.php 7.6 KB

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