NeedPrivileges.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAVACL\Exception;
  4. use Sabre\DAV;
  5. /**
  6. * NeedPrivileges.
  7. *
  8. * The 403-need privileges is thrown when a user didn't have the appropriate
  9. * permissions to perform an operation
  10. *
  11. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  12. * @author Evert Pot (http://evertpot.com/)
  13. * @license http://sabre.io/license/ Modified BSD License
  14. */
  15. class NeedPrivileges extends DAV\Exception\Forbidden
  16. {
  17. /**
  18. * The relevant uri.
  19. *
  20. * @var string
  21. */
  22. protected $uri;
  23. /**
  24. * The privileges the user didn't have.
  25. *
  26. * @var array
  27. */
  28. protected $privileges;
  29. /**
  30. * Constructor.
  31. *
  32. * @param string $uri
  33. */
  34. public function __construct($uri, array $privileges)
  35. {
  36. $this->uri = $uri;
  37. $this->privileges = $privileges;
  38. parent::__construct('User did not have the required privileges ('.implode(',', $privileges).') for path "'.$uri.'"');
  39. }
  40. /**
  41. * Adds in extra information in the xml response.
  42. *
  43. * This method adds the {DAV:}need-privileges element as defined in rfc3744
  44. */
  45. public function serialize(DAV\Server $server, \DOMElement $errorNode)
  46. {
  47. $doc = $errorNode->ownerDocument;
  48. $np = $doc->createElementNS('DAV:', 'd:need-privileges');
  49. $errorNode->appendChild($np);
  50. foreach ($this->privileges as $privilege) {
  51. $resource = $doc->createElementNS('DAV:', 'd:resource');
  52. $np->appendChild($resource);
  53. $resource->appendChild($doc->createElementNS('DAV:', 'd:href', $server->getBaseUri().$this->uri));
  54. $priv = $doc->createElementNS('DAV:', 'd:privilege');
  55. $resource->appendChild($priv);
  56. preg_match('/^{([^}]*)}(.*)$/', $privilege, $privilegeParts);
  57. $priv->appendChild($doc->createElementNS($privilegeParts[1], 'd:'.$privilegeParts[2]));
  58. }
  59. }
  60. }