source: pmb4.2/trunk/fuentes/pmb/admin/connecteurs/out/webdav/lib/Sabre/DAVACL/PrincipalBackend/PDO.php @ 815

Last change on this file since 815 was 815, checked in by jrpelegrina, 4 years ago

Initial release of pmb 4.2

  • Property svn:executable set to *
File size: 12.7 KB
Line 
1<?php
2
3namespace Sabre\DAVACL\PrincipalBackend;
4
5use Sabre\DAV;
6use Sabre\DAVACL;
7
8/**
9 * PDO principal backend
10 *
11 *
12 * This backend assumes all principals are in a single collection. The default collection
13 * is 'principals/', but this can be overriden.
14 *
15 * @copyright Copyright (C) 2007-2013 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 */
19class PDO extends AbstractBackend {
20
21    /**
22     * pdo
23     *
24     * @var PDO
25     */
26    protected $pdo;
27
28    /**
29     * PDO table name for 'principals'
30     *
31     * @var string
32     */
33    protected $tableName;
34
35    /**
36     * PDO table name for 'group members'
37     *
38     * @var string
39     */
40    protected $groupMembersTableName;
41
42    /**
43     * A list of additional fields to support
44     *
45     * @var array
46     */
47    protected $fieldMap = array(
48
49        /**
50         * This property can be used to display the users' real name.
51         */
52        '{DAV:}displayname' => array(
53            'dbField' => 'displayname',
54        ),
55
56        /**
57         * This property is actually used by the CardDAV plugin, where it gets
58         * mapped to {http://calendarserver.orgi/ns/}me-card.
59         *
60         * The reason we don't straight-up use that property, is because
61         * me-card is defined as a property on the users' addressbook
62         * collection.
63         */
64        '{http://sabredav.org/ns}vcard-url' => array(
65            'dbField' => 'vcardurl',
66        ),
67        /**
68         * This is the users' primary email-address.
69         */
70        '{http://sabredav.org/ns}email-address' => array(
71            'dbField' => 'email',
72        ),
73    );
74
75    /**
76     * Sets up the backend.
77     *
78     * @param PDO $pdo
79     * @param string $tableName
80     * @param string $groupMembersTableName
81     */
82    public function __construct(\PDO $pdo, $tableName = 'principals', $groupMembersTableName = 'groupmembers') {
83
84        $this->pdo = $pdo;
85        $this->tableName = $tableName;
86        $this->groupMembersTableName = $groupMembersTableName;
87
88    }
89
90
91    /**
92     * Returns a list of principals based on a prefix.
93     *
94     * This prefix will often contain something like 'principals'. You are only
95     * expected to return principals that are in this base path.
96     *
97     * You are expected to return at least a 'uri' for every user, you can
98     * return any additional properties if you wish so. Common properties are:
99     *   {DAV:}displayname
100     *   {http://sabredav.org/ns}email-address - This is a custom SabreDAV
101     *     field that's actualy injected in a number of other properties. If
102     *     you have an email address, use this property.
103     *
104     * @param string $prefixPath
105     * @return array
106     */
107    public function getPrincipalsByPrefix($prefixPath) {
108
109        $fields = array(
110            'uri',
111        );
112
113        foreach($this->fieldMap as $key=>$value) {
114            $fields[] = $value['dbField'];
115        }
116        $result = $this->pdo->query('SELECT '.implode(',', $fields).'  FROM '. $this->tableName);
117
118        $principals = array();
119
120        while($row = $result->fetch(\PDO::FETCH_ASSOC)) {
121
122            // Checking if the principal is in the prefix
123            list($rowPrefix) = DAV\URLUtil::splitPath($row['uri']);
124            if ($rowPrefix !== $prefixPath) continue;
125
126            $principal = array(
127                'uri' => $row['uri'],
128            );
129            foreach($this->fieldMap as $key=>$value) {
130                if ($row[$value['dbField']]) {
131                    $principal[$key] = $row[$value['dbField']];
132                }
133            }
134            $principals[] = $principal;
135
136        }
137
138        return $principals;
139
140    }
141
142    /**
143     * Returns a specific principal, specified by it's path.
144     * The returned structure should be the exact same as from
145     * getPrincipalsByPrefix.
146     *
147     * @param string $path
148     * @return array
149     */
150    public function getPrincipalByPath($path) {
151
152        $fields = array(
153            'id',
154            'uri',
155        );
156
157        foreach($this->fieldMap as $key=>$value) {
158            $fields[] = $value['dbField'];
159        }
160        $stmt = $this->pdo->prepare('SELECT '.implode(',', $fields).'  FROM '. $this->tableName . ' WHERE uri = ?');
161        $stmt->execute(array($path));
162
163        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
164        if (!$row) return;
165
166        $principal = array(
167            'id'  => $row['id'],
168            'uri' => $row['uri'],
169        );
170        foreach($this->fieldMap as $key=>$value) {
171            if ($row[$value['dbField']]) {
172                $principal[$key] = $row[$value['dbField']];
173            }
174        }
175        return $principal;
176
177    }
178
179    /**
180     * Updates one ore more webdav properties on a principal.
181     *
182     * The list of mutations is supplied as an array. Each key in the array is
183     * a propertyname, such as {DAV:}displayname.
184     *
185     * Each value is the actual value to be updated. If a value is null, it
186     * must be deleted.
187     *
188     * This method should be atomic. It must either completely succeed, or
189     * completely fail. Success and failure can simply be returned as 'true' or
190     * 'false'.
191     *
192     * It is also possible to return detailed failure information. In that case
193     * an array such as this should be returned:
194     *
195     * array(
196     *   200 => array(
197     *      '{DAV:}prop1' => null,
198     *   ),
199     *   201 => array(
200     *      '{DAV:}prop2' => null,
201     *   ),
202     *   403 => array(
203     *      '{DAV:}prop3' => null,
204     *   ),
205     *   424 => array(
206     *      '{DAV:}prop4' => null,
207     *   ),
208     * );
209     *
210     * In this previous example prop1 was successfully updated or deleted, and
211     * prop2 was succesfully created.
212     *
213     * prop3 failed to update due to '403 Forbidden' and because of this prop4
214     * also could not be updated with '424 Failed dependency'.
215     *
216     * This last example was actually incorrect. While 200 and 201 could appear
217     * in 1 response, if there's any error (403) the other properties should
218     * always fail with 423 (failed dependency).
219     *
220     * But anyway, if you don't want to scratch your head over this, just
221     * return true or false.
222     *
223     * @param string $path
224     * @param array $mutations
225     * @return array|bool
226     */
227    public function updatePrincipal($path, $mutations) {
228
229        $updateAble = array();
230        foreach($mutations as $key=>$value) {
231
232            // We are not aware of this field, we must fail.
233            if (!isset($this->fieldMap[$key])) {
234
235                $response = array(
236                    403 => array(
237                        $key => null,
238                    ),
239                    424 => array(),
240                );
241
242                // Adding the rest to the response as a 424
243                foreach($mutations as $subKey=>$subValue) {
244                    if ($subKey !== $key) {
245                        $response[424][$subKey] = null;
246                    }
247                }
248                return $response;
249            }
250
251            $updateAble[$this->fieldMap[$key]['dbField']] = $value;
252
253        }
254
255        // No fields to update
256        $query = "UPDATE " . $this->tableName . " SET ";
257
258        $first = true;
259        foreach($updateAble as $key => $value) {
260            if (!$first) {
261                $query.= ', ';
262            }
263            $first = false;
264            $query.= "$key = :$key ";
265        }
266        $query.='WHERE uri = :uri';
267        $stmt = $this->pdo->prepare($query);
268        $updateAble['uri'] =  $path;
269        $stmt->execute($updateAble);
270
271        return true;
272
273    }
274
275    /**
276     * This method is used to search for principals matching a set of
277     * properties.
278     *
279     * This search is specifically used by RFC3744's principal-property-search
280     * REPORT. You should at least allow searching on
281     * http://sabredav.org/ns}email-address.
282     *
283     * The actual search should be a unicode-non-case-sensitive search. The
284     * keys in searchProperties are the WebDAV property names, while the values
285     * are the property values to search on.
286     *
287     * If multiple properties are being searched on, the search should be
288     * AND'ed.
289     *
290     * This method should simply return an array with full principal uri's.
291     *
292     * If somebody attempted to search on a property the backend does not
293     * support, you should simply return 0 results.
294     *
295     * You can also just return 0 results if you choose to not support
296     * searching at all, but keep in mind that this may stop certain features
297     * from working.
298     *
299     * @param string $prefixPath
300     * @param array $searchProperties
301     * @return array
302     */
303    public function searchPrincipals($prefixPath, array $searchProperties) {
304
305        $query = 'SELECT uri FROM ' . $this->tableName . ' WHERE 1=1 ';
306        $values = array();
307        foreach($searchProperties as $property => $value) {
308
309            switch($property) {
310
311                case '{DAV:}displayname' :
312                    $query.=' AND displayname LIKE ?';
313                    $values[] = '%' . $value . '%';
314                    break;
315                case '{http://sabredav.org/ns}email-address' :
316                    $query.=' AND email LIKE ?';
317                    $values[] = '%' . $value . '%';
318                    break;
319                default :
320                    // Unsupported property
321                    return array();
322
323            }
324
325        }
326        $stmt = $this->pdo->prepare($query);
327        $stmt->execute($values);
328
329        $principals = array();
330        while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
331
332            // Checking if the principal is in the prefix
333            list($rowPrefix) = DAV\URLUtil::splitPath($row['uri']);
334            if ($rowPrefix !== $prefixPath) continue;
335
336            $principals[] = $row['uri'];
337
338        }
339
340        return $principals;
341
342    }
343
344    /**
345     * Returns the list of members for a group-principal
346     *
347     * @param string $principal
348     * @return array
349     */
350    public function getGroupMemberSet($principal) {
351
352        $principal = $this->getPrincipalByPath($principal);
353        if (!$principal) throw new DAV\Exception('Principal not found');
354
355        $stmt = $this->pdo->prepare('SELECT principals.uri as uri FROM '.$this->groupMembersTableName.' AS groupmembers LEFT JOIN '.$this->tableName.' AS principals ON groupmembers.member_id = principals.id WHERE groupmembers.principal_id = ?');
356        $stmt->execute(array($principal['id']));
357
358        $result = array();
359        while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
360            $result[] = $row['uri'];
361        }
362        return $result;
363
364    }
365
366    /**
367     * Returns the list of groups a principal is a member of
368     *
369     * @param string $principal
370     * @return array
371     */
372    public function getGroupMembership($principal) {
373
374        $principal = $this->getPrincipalByPath($principal);
375        if (!$principal) throw new DAV\Exception('Principal not found');
376
377        $stmt = $this->pdo->prepare('SELECT principals.uri as uri FROM '.$this->groupMembersTableName.' AS groupmembers LEFT JOIN '.$this->tableName.' AS principals ON groupmembers.principal_id = principals.id WHERE groupmembers.member_id = ?');
378        $stmt->execute(array($principal['id']));
379
380        $result = array();
381        while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
382            $result[] = $row['uri'];
383        }
384        return $result;
385
386    }
387
388    /**
389     * Updates the list of group members for a group principal.
390     *
391     * The principals should be passed as a list of uri's.
392     *
393     * @param string $principal
394     * @param array $members
395     * @return void
396     */
397    public function setGroupMemberSet($principal, array $members) {
398
399        // Grabbing the list of principal id's.
400        $stmt = $this->pdo->prepare('SELECT id, uri FROM '.$this->tableName.' WHERE uri IN (? ' . str_repeat(', ? ', count($members)) . ');');
401        $stmt->execute(array_merge(array($principal), $members));
402
403        $memberIds = array();
404        $principalId = null;
405
406        while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
407            if ($row['uri'] == $principal) {
408                $principalId = $row['id'];
409            } else {
410                $memberIds[] = $row['id'];
411            }
412        }
413        if (!$principalId) throw new DAV\Exception('Principal not found');
414
415        // Wiping out old members
416        $stmt = $this->pdo->prepare('DELETE FROM '.$this->groupMembersTableName.' WHERE principal_id = ?;');
417        $stmt->execute(array($principalId));
418
419        foreach($memberIds as $memberId) {
420
421            $stmt = $this->pdo->prepare('INSERT INTO '.$this->groupMembersTableName.' (principal_id, member_id) VALUES (?, ?);');
422            $stmt->execute(array($principalId, $memberId));
423
424        }
425
426    }
427
428}
Note: See TracBrowser for help on using the repository browser.