1<?php
2
3namespace Sabre\CalDAV\Principal;
4
5use Sabre\DAV;
6use Sabre\DAVACL;
7
8/**
9 * CalDAV principal
10 *
11 * This is a standard user-principal for CalDAV. This principal is also a
12 * collection and returns the caldav-proxy-read and caldav-proxy-write child
13 * principals.
14 *
15 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
16 * @author Evert Pot (http://evertpot.com/)
17 * @license http://sabre.io/license/ Modified BSD License
18 */
19class User extends DAVACL\Principal implements DAV\ICollection {
20
21    /**
22     * Creates a new file in the directory
23     *
24     * @param string $name Name of the file
25     * @param resource $data Initial payload, passed as a readable stream resource.
26     * @throws DAV\Exception\Forbidden
27     * @return void
28     */
29    function createFile($name, $data = null) {
30
31        throw new DAV\Exception\Forbidden('Permission denied to create file (filename ' . $name . ')');
32
33    }
34
35    /**
36     * Creates a new subdirectory
37     *
38     * @param string $name
39     * @throws DAV\Exception\Forbidden
40     * @return void
41     */
42    function createDirectory($name) {
43
44        throw new DAV\Exception\Forbidden('Permission denied to create directory');
45
46    }
47
48    /**
49     * Returns a specific child node, referenced by its name
50     *
51     * @param string $name
52     * @return DAV\INode
53     */
54    function getChild($name) {
55
56        $principal = $this->principalBackend->getPrincipalByPath($this->getPrincipalURL() . '/' . $name);
57        if (!$principal) {
58            throw new DAV\Exception\NotFound('Node with name ' . $name . ' was not found');
59        }
60        if ($name === 'calendar-proxy-read')
61            return new ProxyRead($this->principalBackend, $this->principalProperties);
62
63        if ($name === 'calendar-proxy-write')
64            return new ProxyWrite($this->principalBackend, $this->principalProperties);
65
66        throw new DAV\Exception\NotFound('Node with name ' . $name . ' was not found');
67
68    }
69
70    /**
71     * Returns an array with all the child nodes
72     *
73     * @return DAV\INode[]
74     */
75    function getChildren() {
76
77        $r = [];
78        if ($this->principalBackend->getPrincipalByPath($this->getPrincipalURL() . '/calendar-proxy-read')) {
79            $r[] = new ProxyRead($this->principalBackend, $this->principalProperties);
80        }
81        if ($this->principalBackend->getPrincipalByPath($this->getPrincipalURL() . '/calendar-proxy-write')) {
82            $r[] = new ProxyWrite($this->principalBackend, $this->principalProperties);
83        }
84
85        return $r;
86
87    }
88
89    /**
90     * Returns whether or not the child node exists
91     *
92     * @param string $name
93     * @return bool
94     */
95    function childExists($name) {
96
97        try {
98            $this->getChild($name);
99            return true;
100        } catch (DAV\Exception\NotFound $e) {
101            return false;
102        }
103
104    }
105
106    /**
107     * Returns a list of ACE's for this node.
108     *
109     * Each ACE has the following properties:
110     *   * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
111     *     currently the only supported privileges
112     *   * 'principal', a url to the principal who owns the node
113     *   * 'protected' (optional), indicating that this ACE is not allowed to
114     *      be updated.
115     *
116     * @return array
117     */
118    function getACL() {
119
120        $acl = parent::getACL();
121        $acl[] = [
122            'privilege' => '{DAV:}read',
123            'principal' => $this->principalProperties['uri'] . '/calendar-proxy-read',
124            'protected' => true,
125        ];
126        $acl[] = [
127            'privilege' => '{DAV:}read',
128            'principal' => $this->principalProperties['uri'] . '/calendar-proxy-write',
129            'protected' => true,
130        ];
131        return $acl;
132
133    }
134
135}
136