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.3KB

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