AbstractPrincipalCollection.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAVACL;
  4. use Sabre\DAV;
  5. use Sabre\Uri;
  6. /**
  7. * Principals Collection.
  8. *
  9. * This is a helper class that easily allows you to create a collection that
  10. * has a childnode for every principal.
  11. *
  12. * To use this class, simply implement the getChildForPrincipal method.
  13. *
  14. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  15. * @author Evert Pot (http://evertpot.com/)
  16. * @license http://sabre.io/license/ Modified BSD License
  17. */
  18. abstract class AbstractPrincipalCollection extends DAV\Collection implements IPrincipalCollection
  19. {
  20. /**
  21. * Principal backend.
  22. *
  23. * @var PrincipalBackend\BackendInterface
  24. */
  25. protected $principalBackend;
  26. /**
  27. * The path to the principals we're listing from.
  28. *
  29. * @var string
  30. */
  31. protected $principalPrefix;
  32. /**
  33. * If this value is set to true, it effectively disables listing of users
  34. * it still allows user to find other users if they have an exact url.
  35. *
  36. * @var bool
  37. */
  38. public $disableListing = false;
  39. /**
  40. * Creates the object.
  41. *
  42. * This object must be passed the principal backend. This object will
  43. * filter all principals from a specified prefix ($principalPrefix). The
  44. * default is 'principals', if your principals are stored in a different
  45. * collection, override $principalPrefix
  46. *
  47. * @param string $principalPrefix
  48. */
  49. public function __construct(PrincipalBackend\BackendInterface $principalBackend, $principalPrefix = 'principals')
  50. {
  51. $this->principalPrefix = $principalPrefix;
  52. $this->principalBackend = $principalBackend;
  53. }
  54. /**
  55. * This method returns a node for a principal.
  56. *
  57. * The passed array contains principal information, and is guaranteed to
  58. * at least contain a uri item. Other properties may or may not be
  59. * supplied by the authentication backend.
  60. *
  61. * @return DAV\INode
  62. */
  63. abstract public function getChildForPrincipal(array $principalInfo);
  64. /**
  65. * Returns the name of this collection.
  66. *
  67. * @return string
  68. */
  69. public function getName()
  70. {
  71. list(, $name) = Uri\split($this->principalPrefix);
  72. return $name;
  73. }
  74. /**
  75. * Return the list of users.
  76. *
  77. * @return array
  78. */
  79. public function getChildren()
  80. {
  81. if ($this->disableListing) {
  82. throw new DAV\Exception\MethodNotAllowed('Listing members of this collection is disabled');
  83. }
  84. $children = [];
  85. foreach ($this->principalBackend->getPrincipalsByPrefix($this->principalPrefix) as $principalInfo) {
  86. $children[] = $this->getChildForPrincipal($principalInfo);
  87. }
  88. return $children;
  89. }
  90. /**
  91. * Returns a child object, by its name.
  92. *
  93. * @param string $name
  94. *
  95. * @throws DAV\Exception\NotFound
  96. *
  97. * @return DAV\INode
  98. */
  99. public function getChild($name)
  100. {
  101. $principalInfo = $this->principalBackend->getPrincipalByPath($this->principalPrefix.'/'.$name);
  102. if (!$principalInfo) {
  103. throw new DAV\Exception\NotFound('Principal with name '.$name.' not found');
  104. }
  105. return $this->getChildForPrincipal($principalInfo);
  106. }
  107. /**
  108. * This method is used to search for principals matching a set of
  109. * properties.
  110. *
  111. * This search is specifically used by RFC3744's principal-property-search
  112. * REPORT. You should at least allow searching on
  113. * http://sabredav.org/ns}email-address.
  114. *
  115. * The actual search should be a unicode-non-case-sensitive search. The
  116. * keys in searchProperties are the WebDAV property names, while the values
  117. * are the property values to search on.
  118. *
  119. * By default, if multiple properties are submitted to this method, the
  120. * various properties should be combined with 'AND'. If $test is set to
  121. * 'anyof', it should be combined using 'OR'.
  122. *
  123. * This method should simply return a list of 'child names', which may be
  124. * used to call $this->getChild in the future.
  125. *
  126. * @param string $test
  127. *
  128. * @return array
  129. */
  130. public function searchPrincipals(array $searchProperties, $test = 'allof')
  131. {
  132. $result = $this->principalBackend->searchPrincipals($this->principalPrefix, $searchProperties, $test);
  133. $r = [];
  134. foreach ($result as $row) {
  135. list(, $r[]) = Uri\split($row);
  136. }
  137. return $r;
  138. }
  139. /**
  140. * Finds a principal by its URI.
  141. *
  142. * This method may receive any type of uri, but mailto: addresses will be
  143. * the most common.
  144. *
  145. * Implementation of this API is optional. It is currently used by the
  146. * CalDAV system to find principals based on their email addresses. If this
  147. * API is not implemented, some features may not work correctly.
  148. *
  149. * This method must return a relative principal path, or null, if the
  150. * principal was not found or you refuse to find it.
  151. *
  152. * @param string $uri
  153. *
  154. * @return string
  155. */
  156. public function findByUri($uri)
  157. {
  158. return $this->principalBackend->findByUri($uri, $this->principalPrefix);
  159. }
  160. }