principal.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Felix Moeller <mail@felixmoeller.de>
  5. * @author Jakob Sack <mail@jakobsack.de>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Lukas Reschke <lukas@owncloud.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Sebastian Döll <sebastian.doell@libasys.de>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Thomas Tanghus <thomas@tanghus.net>
  12. * @author Vincent Petry <pvince81@owncloud.com>
  13. *
  14. * @copyright Copyright (c) 2015, ownCloud, Inc.
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\Connector\Sabre;
  31. use OCP\IUserManager;
  32. use OCP\IConfig;
  33. use \Sabre\DAV\PropPatch;
  34. class Principal implements \Sabre\DAVACL\PrincipalBackend\BackendInterface {
  35. /** @var IConfig */
  36. private $config;
  37. /** @var IUserManager */
  38. private $userManager;
  39. /**
  40. * @param IConfig $config
  41. * @param IUserManager $userManager
  42. */
  43. public function __construct(IConfig $config,
  44. IUserManager $userManager) {
  45. $this->config = $config;
  46. $this->userManager = $userManager;
  47. }
  48. /**
  49. * Returns a list of principals based on a prefix.
  50. *
  51. * This prefix will often contain something like 'principals'. You are only
  52. * expected to return principals that are in this base path.
  53. *
  54. * You are expected to return at least a 'uri' for every user, you can
  55. * return any additional properties if you wish so. Common properties are:
  56. * {DAV:}displayname
  57. *
  58. * @param string $prefixPath
  59. * @return string[]
  60. */
  61. public function getPrincipalsByPrefix($prefixPath) {
  62. $principals = [];
  63. if ($prefixPath === 'principals') {
  64. foreach($this->userManager->search('') as $user) {
  65. $principal = [
  66. 'uri' => 'principals/' . $user->getUID(),
  67. '{DAV:}displayname' => $user->getUID(),
  68. ];
  69. $email = $this->config->getUserValue($user->getUID(), 'settings', 'email');
  70. if(!empty($email)) {
  71. $principal['{http://sabredav.org/ns}email-address'] = $email;
  72. }
  73. $principals[] = $principal;
  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. list($prefix, $name) = explode('/', $path);
  88. $user = $this->userManager->get($name);
  89. if ($prefix === 'principals' && !is_null($user)) {
  90. $principal = [
  91. 'uri' => 'principals/' . $user->getUID(),
  92. '{DAV:}displayname' => $user->getUID(),
  93. ];
  94. $email = $this->config->getUserValue($user->getUID(), 'settings', 'email');
  95. if($email) {
  96. $principal['{http://sabredav.org/ns}email-address'] = $email;
  97. }
  98. return $principal;
  99. }
  100. return null;
  101. }
  102. /**
  103. * Returns the list of members for a group-principal
  104. *
  105. * @param string $principal
  106. * @return string[]
  107. * @throws \Sabre\DAV\Exception
  108. */
  109. public function getGroupMemberSet($principal) {
  110. // TODO: for now the group principal has only one member, the user itself
  111. $principal = $this->getPrincipalByPath($principal);
  112. if (!$principal) {
  113. throw new \Sabre\DAV\Exception('Principal not found');
  114. }
  115. return [$principal['uri']];
  116. }
  117. /**
  118. * Returns the list of groups a principal is a member of
  119. *
  120. * @param string $principal
  121. * @return array
  122. * @throws \Sabre\DAV\Exception
  123. */
  124. public function getGroupMembership($principal) {
  125. list($prefix, $name) = \Sabre\HTTP\URLUtil::splitPath($principal);
  126. $group_membership = array();
  127. if ($prefix === 'principals') {
  128. $principal = $this->getPrincipalByPath($principal);
  129. if (!$principal) {
  130. throw new \Sabre\DAV\Exception('Principal not found');
  131. }
  132. // TODO: for now the user principal has only its own groups
  133. return array(
  134. 'principals/'.$name.'/calendar-proxy-read',
  135. 'principals/'.$name.'/calendar-proxy-write',
  136. // The addressbook groups are not supported in Sabre,
  137. // see http://groups.google.com/group/sabredav-discuss/browse_thread/thread/ef2fa9759d55f8c#msg_5720afc11602e753
  138. //'principals/'.$name.'/addressbook-proxy-read',
  139. //'principals/'.$name.'/addressbook-proxy-write',
  140. );
  141. }
  142. return $group_membership;
  143. }
  144. /**
  145. * Updates the list of group members for a group principal.
  146. *
  147. * The principals should be passed as a list of uri's.
  148. *
  149. * @param string $principal
  150. * @param array $members
  151. * @throws \Sabre\DAV\Exception
  152. */
  153. public function setGroupMemberSet($principal, array $members) {
  154. throw new \Sabre\DAV\Exception('Setting members of the group is not supported yet');
  155. }
  156. /**
  157. * @param string $path
  158. * @param PropPatch $propPatch
  159. * @return int
  160. */
  161. function updatePrincipal($path, PropPatch $propPatch) {
  162. return 0;
  163. }
  164. /**
  165. * @param string $prefixPath
  166. * @param array $searchProperties
  167. * @param string $test
  168. * @return array
  169. */
  170. function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') {
  171. return [];
  172. }
  173. /**
  174. * @param string $uri
  175. * @param string $principalPrefix
  176. * @return string
  177. */
  178. function findByUri($uri, $principalPrefix) {
  179. return '';
  180. }
  181. }