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