Principal.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAVACL;
  4. use Sabre\DAV;
  5. use Sabre\Uri;
  6. /**
  7. * Principal class.
  8. *
  9. * This class is a representation of a simple principal
  10. *
  11. * Many WebDAV specs require a user to show up in the directory
  12. * structure.
  13. *
  14. * This principal also has basic ACL settings, only allowing the principal
  15. * access it's own principal.
  16. *
  17. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  18. * @author Evert Pot (http://evertpot.com/)
  19. * @license http://sabre.io/license/ Modified BSD License
  20. */
  21. class Principal extends DAV\Node implements IPrincipal, DAV\IProperties, IACL
  22. {
  23. use ACLTrait;
  24. /**
  25. * Struct with principal information.
  26. *
  27. * @var array
  28. */
  29. protected $principalProperties;
  30. /**
  31. * Principal backend.
  32. *
  33. * @var PrincipalBackend\BackendInterface
  34. */
  35. protected $principalBackend;
  36. /**
  37. * Creates the principal object.
  38. */
  39. public function __construct(PrincipalBackend\BackendInterface $principalBackend, array $principalProperties = [])
  40. {
  41. if (!isset($principalProperties['uri'])) {
  42. throw new DAV\Exception('The principal properties must at least contain the \'uri\' key');
  43. }
  44. $this->principalBackend = $principalBackend;
  45. $this->principalProperties = $principalProperties;
  46. }
  47. /**
  48. * Returns the full principal url.
  49. *
  50. * @return string
  51. */
  52. public function getPrincipalUrl()
  53. {
  54. return $this->principalProperties['uri'];
  55. }
  56. /**
  57. * Returns a list of alternative urls for a principal.
  58. *
  59. * This can for example be an email address, or ldap url.
  60. *
  61. * @return array
  62. */
  63. public function getAlternateUriSet()
  64. {
  65. $uris = [];
  66. if (isset($this->principalProperties['{DAV:}alternate-URI-set'])) {
  67. $uris = $this->principalProperties['{DAV:}alternate-URI-set'];
  68. }
  69. if (isset($this->principalProperties['{http://sabredav.org/ns}email-address'])) {
  70. $uris[] = 'mailto:'.$this->principalProperties['{http://sabredav.org/ns}email-address'];
  71. }
  72. return array_unique($uris);
  73. }
  74. /**
  75. * Returns the list of group members.
  76. *
  77. * If this principal is a group, this function should return
  78. * all member principal uri's for the group.
  79. *
  80. * @return array
  81. */
  82. public function getGroupMemberSet()
  83. {
  84. return $this->principalBackend->getGroupMemberSet($this->principalProperties['uri']);
  85. }
  86. /**
  87. * Returns the list of groups this principal is member of.
  88. *
  89. * If this principal is a member of a (list of) groups, this function
  90. * should return a list of principal uri's for it's members.
  91. *
  92. * @return array
  93. */
  94. public function getGroupMembership()
  95. {
  96. return $this->principalBackend->getGroupMemberShip($this->principalProperties['uri']);
  97. }
  98. /**
  99. * Sets a list of group members.
  100. *
  101. * If this principal is a group, this method sets all the group members.
  102. * The list of members is always overwritten, never appended to.
  103. *
  104. * This method should throw an exception if the members could not be set.
  105. */
  106. public function setGroupMemberSet(array $groupMembers)
  107. {
  108. $this->principalBackend->setGroupMemberSet($this->principalProperties['uri'], $groupMembers);
  109. }
  110. /**
  111. * Returns this principals name.
  112. *
  113. * @return string
  114. */
  115. public function getName()
  116. {
  117. $uri = $this->principalProperties['uri'];
  118. list(, $name) = Uri\split($uri);
  119. return $name;
  120. }
  121. /**
  122. * Returns the name of the user.
  123. *
  124. * @return string
  125. */
  126. public function getDisplayName()
  127. {
  128. if (isset($this->principalProperties['{DAV:}displayname'])) {
  129. return $this->principalProperties['{DAV:}displayname'];
  130. } else {
  131. return $this->getName();
  132. }
  133. }
  134. /**
  135. * Returns a list of properties.
  136. *
  137. * @param array $requestedProperties
  138. *
  139. * @return array
  140. */
  141. public function getProperties($requestedProperties)
  142. {
  143. $newProperties = [];
  144. foreach ($requestedProperties as $propName) {
  145. if (isset($this->principalProperties[$propName])) {
  146. $newProperties[$propName] = $this->principalProperties[$propName];
  147. }
  148. }
  149. return $newProperties;
  150. }
  151. /**
  152. * Updates properties on this node.
  153. *
  154. * This method received a PropPatch object, which contains all the
  155. * information about the update.
  156. *
  157. * To update specific properties, call the 'handle' method on this object.
  158. * Read the PropPatch documentation for more information.
  159. */
  160. public function propPatch(DAV\PropPatch $propPatch)
  161. {
  162. return $this->principalBackend->updatePrincipal(
  163. $this->principalProperties['uri'],
  164. $propPatch
  165. );
  166. }
  167. /**
  168. * Returns the owner principal.
  169. *
  170. * This must be a url to a principal, or null if there's no owner
  171. *
  172. * @return string|null
  173. */
  174. public function getOwner()
  175. {
  176. return $this->principalProperties['uri'];
  177. }
  178. }