GroupPrincipalBackend.php 8.2 KB

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