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

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