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