GroupPrincipalBackend.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Thomas Müller <thomas.mueller@tmit.eu>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\DAV\DAV;
  23. use OCP\IGroup;
  24. use OCP\IGroupManager;
  25. use OCP\IUser;
  26. use Sabre\DAV\Exception;
  27. use \Sabre\DAV\PropPatch;
  28. use Sabre\DAVACL\PrincipalBackend\BackendInterface;
  29. class GroupPrincipalBackend implements BackendInterface {
  30. const PRINCIPAL_PREFIX = 'principals/groups';
  31. /** @var IGroupManager */
  32. private $groupManager;
  33. /**
  34. * @param IGroupManager $IGroupManager
  35. */
  36. public function __construct(IGroupManager $IGroupManager) {
  37. $this->groupManager = $IGroupManager;
  38. }
  39. /**
  40. * Returns a list of principals based on a prefix.
  41. *
  42. * This prefix will often contain something like 'principals'. You are only
  43. * expected to return principals that are in this base path.
  44. *
  45. * You are expected to return at least a 'uri' for every user, you can
  46. * return any additional properties if you wish so. Common properties are:
  47. * {DAV:}displayname
  48. *
  49. * @param string $prefixPath
  50. * @return string[]
  51. */
  52. public function getPrincipalsByPrefix($prefixPath) {
  53. $principals = [];
  54. if ($prefixPath === self::PRINCIPAL_PREFIX) {
  55. foreach($this->groupManager->search('') as $user) {
  56. $principals[] = $this->groupToPrincipal($user);
  57. }
  58. }
  59. return $principals;
  60. }
  61. /**
  62. * Returns a specific principal, specified by it's path.
  63. * The returned structure should be the exact same as from
  64. * getPrincipalsByPrefix.
  65. *
  66. * @param string $path
  67. * @return array
  68. */
  69. public function getPrincipalByPath($path) {
  70. $elements = explode('/', $path, 3);
  71. if ($elements[0] !== 'principals') {
  72. return null;
  73. }
  74. if ($elements[1] !== 'groups') {
  75. return null;
  76. }
  77. $name = urldecode($elements[2]);
  78. $group = $this->groupManager->get($name);
  79. if (!is_null($group)) {
  80. return $this->groupToPrincipal($group);
  81. }
  82. return null;
  83. }
  84. /**
  85. * Returns the list of members for a group-principal
  86. *
  87. * @param string $principal
  88. * @return string[]
  89. * @throws Exception
  90. */
  91. public function getGroupMemberSet($principal) {
  92. $elements = explode('/', $principal);
  93. if ($elements[0] !== 'principals') {
  94. return [];
  95. }
  96. if ($elements[1] !== 'groups') {
  97. return [];
  98. }
  99. $name = $elements[2];
  100. $group = $this->groupManager->get($name);
  101. if (is_null($group)) {
  102. return [];
  103. }
  104. return array_map(function($user) {
  105. return $this->userToPrincipal($user);
  106. }, $group->getUsers());
  107. }
  108. /**
  109. * Returns the list of groups a principal is a member of
  110. *
  111. * @param string $principal
  112. * @return array
  113. * @throws Exception
  114. */
  115. public function getGroupMembership($principal) {
  116. return [];
  117. }
  118. /**
  119. * Updates the list of group members for a group principal.
  120. *
  121. * The principals should be passed as a list of uri's.
  122. *
  123. * @param string $principal
  124. * @param string[] $members
  125. * @throws Exception
  126. */
  127. public function setGroupMemberSet($principal, array $members) {
  128. throw new Exception('Setting members of the group is not supported yet');
  129. }
  130. /**
  131. * @param string $path
  132. * @param PropPatch $propPatch
  133. * @return int
  134. */
  135. function updatePrincipal($path, PropPatch $propPatch) {
  136. return 0;
  137. }
  138. /**
  139. * @param string $prefixPath
  140. * @param array $searchProperties
  141. * @param string $test
  142. * @return array
  143. */
  144. function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') {
  145. return [];
  146. }
  147. /**
  148. * @param string $uri
  149. * @param string $principalPrefix
  150. * @return string
  151. */
  152. function findByUri($uri, $principalPrefix) {
  153. return '';
  154. }
  155. /**
  156. * @param IGroup $group
  157. * @return array
  158. */
  159. protected function groupToPrincipal($group) {
  160. $groupId = $group->getGID();
  161. $principal = [
  162. 'uri' => 'principals/groups/' . urlencode($groupId),
  163. '{DAV:}displayname' => $groupId,
  164. ];
  165. return $principal;
  166. }
  167. /**
  168. * @param IUser $user
  169. * @return array
  170. */
  171. protected function userToPrincipal($user) {
  172. $principal = [
  173. 'uri' => 'principals/users/' . $user->getUID(),
  174. '{DAV:}displayname' => $user->getDisplayName(),
  175. ];
  176. return $principal;
  177. }
  178. }