Principal.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * Principal class
  4. *
  5. * This class is a representation of a simple principal
  6. *
  7. * Many WebDAV specs require a user to show up in the directory
  8. * structure.
  9. *
  10. * This principal also has basic ACL settings, only allowing the principal
  11. * access it's own principal.
  12. *
  13. * @package Sabre
  14. * @subpackage DAVACL
  15. * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
  16. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  17. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  18. */
  19. class Sabre_DAVACL_Principal extends Sabre_DAV_Node implements Sabre_DAVACL_IPrincipal, Sabre_DAV_IProperties, Sabre_DAVACL_IACL {
  20. /**
  21. * Struct with principal information.
  22. *
  23. * @var array
  24. */
  25. protected $principalProperties;
  26. /**
  27. * Principal backend
  28. *
  29. * @var Sabre_DAVACL_IPrincipalBackend
  30. */
  31. protected $principalBackend;
  32. /**
  33. * Creates the principal object
  34. *
  35. * @param Sabre_DAVACL_IPrincipalBackend $principalBackend
  36. * @param array $principalProperties
  37. */
  38. public function __construct(Sabre_DAVACL_IPrincipalBackend $principalBackend, array $principalProperties = array()) {
  39. if (!isset($principalProperties['uri'])) {
  40. throw new Sabre_DAV_Exception('The principal properties must at least contain the \'uri\' key');
  41. }
  42. $this->principalBackend = $principalBackend;
  43. $this->principalProperties = $principalProperties;
  44. }
  45. /**
  46. * Returns the full principal url
  47. *
  48. * @return string
  49. */
  50. public function getPrincipalUrl() {
  51. return $this->principalProperties['uri'];
  52. }
  53. /**
  54. * Returns a list of alternative urls for a principal
  55. *
  56. * This can for example be an email address, or ldap url.
  57. *
  58. * @return array
  59. */
  60. public function getAlternateUriSet() {
  61. $uris = array();
  62. if (isset($this->principalProperties['{DAV:}alternate-URI-set'])) {
  63. $uris = $this->principalProperties['{DAV:}alternate-URI-set'];
  64. }
  65. if (isset($this->principalProperties['{http://sabredav.org/ns}email-address'])) {
  66. $uris[] = 'mailto:' . $this->principalProperties['{http://sabredav.org/ns}email-address'];
  67. }
  68. return array_unique($uris);
  69. }
  70. /**
  71. * Returns the list of group members
  72. *
  73. * If this principal is a group, this function should return
  74. * all member principal uri's for the group.
  75. *
  76. * @return array
  77. */
  78. public function getGroupMemberSet() {
  79. return $this->principalBackend->getGroupMemberSet($this->principalProperties['uri']);
  80. }
  81. /**
  82. * Returns the list of groups this principal is member of
  83. *
  84. * If this principal is a member of a (list of) groups, this function
  85. * should return a list of principal uri's for it's members.
  86. *
  87. * @return array
  88. */
  89. public function getGroupMembership() {
  90. return $this->principalBackend->getGroupMemberShip($this->principalProperties['uri']);
  91. }
  92. /**
  93. * Sets a list of group members
  94. *
  95. * If this principal is a group, this method sets all the group members.
  96. * The list of members is always overwritten, never appended to.
  97. *
  98. * This method should throw an exception if the members could not be set.
  99. *
  100. * @param array $groupMembers
  101. * @return void
  102. */
  103. public function setGroupMemberSet(array $groupMembers) {
  104. $this->principalBackend->setGroupMemberSet($this->principalProperties['uri'], $groupMembers);
  105. }
  106. /**
  107. * Returns this principals name.
  108. *
  109. * @return string
  110. */
  111. public function getName() {
  112. $uri = $this->principalProperties['uri'];
  113. list(, $name) = Sabre_DAV_URLUtil::splitPath($uri);
  114. return $name;
  115. }
  116. /**
  117. * Returns the name of the user
  118. *
  119. * @return string
  120. */
  121. public function getDisplayName() {
  122. if (isset($this->principalProperties['{DAV:}displayname'])) {
  123. return $this->principalProperties['{DAV:}displayname'];
  124. } else {
  125. return $this->getName();
  126. }
  127. }
  128. /**
  129. * Returns a list of properties
  130. *
  131. * @param array $requestedProperties
  132. * @return array
  133. */
  134. public function getProperties($requestedProperties) {
  135. $newProperties = array();
  136. foreach($requestedProperties as $propName) {
  137. if (isset($this->principalProperties[$propName])) {
  138. $newProperties[$propName] = $this->principalProperties[$propName];
  139. }
  140. }
  141. return $newProperties;
  142. }
  143. /**
  144. * Updates this principals properties.
  145. *
  146. * @param array $mutations
  147. * @see Sabre_DAV_IProperties::updateProperties
  148. * @return bool|array
  149. */
  150. public function updateProperties($mutations) {
  151. return $this->principalBackend->updatePrincipal($this->principalProperties['uri'], $mutations);
  152. }
  153. /**
  154. * Returns the owner principal
  155. *
  156. * This must be a url to a principal, or null if there's no owner
  157. *
  158. * @return string|null
  159. */
  160. public function getOwner() {
  161. return $this->principalProperties['uri'];
  162. }
  163. /**
  164. * Returns a group principal
  165. *
  166. * This must be a url to a principal, or null if there's no owner
  167. *
  168. * @return string|null
  169. */
  170. public function getGroup() {
  171. return null;
  172. }
  173. /**
  174. * Returns a list of ACE's for this node.
  175. *
  176. * Each ACE has the following properties:
  177. * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
  178. * currently the only supported privileges
  179. * * 'principal', a url to the principal who owns the node
  180. * * 'protected' (optional), indicating that this ACE is not allowed to
  181. * be updated.
  182. *
  183. * @return array
  184. */
  185. public function getACL() {
  186. return array(
  187. array(
  188. 'privilege' => '{DAV:}read',
  189. 'principal' => $this->getPrincipalUrl(),
  190. 'protected' => true,
  191. ),
  192. );
  193. }
  194. /**
  195. * Updates the ACL
  196. *
  197. * This method will receive a list of new ACE's.
  198. *
  199. * @param array $acl
  200. * @return void
  201. */
  202. public function setACL(array $acl) {
  203. throw new Sabre_DAV_Exception_MethodNotAllowed('Updating ACLs is not allowed here');
  204. }
  205. /**
  206. * Returns the list of supported privileges for this node.
  207. *
  208. * The returned data structure is a list of nested privileges.
  209. * See Sabre_DAVACL_Plugin::getDefaultSupportedPrivilegeSet for a simple
  210. * standard structure.
  211. *
  212. * If null is returned from this method, the default privilege set is used,
  213. * which is fine for most common usecases.
  214. *
  215. * @return array|null
  216. */
  217. public function getSupportedPrivilegeSet() {
  218. return null;
  219. }
  220. }