ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Sabre\DAVACL\PrincipalBackend\PDO Class Reference

PDO principal backend. More...

+ Inheritance diagram for Sabre\DAVACL\PrincipalBackend\PDO:
+ Collaboration diagram for Sabre\DAVACL\PrincipalBackend\PDO:

Public Member Functions

 __construct (\PDO $pdo)
 This is the users' primary email-address. More...
 
 getPrincipalsByPrefix ($prefixPath)
 Returns a list of principals based on a prefix. More...
 
 getPrincipalByPath ($path)
 Returns a specific principal, specified by it's path. More...
 
 updatePrincipal ($path, DAV\PropPatch $propPatch)
 Updates one ore more webdav properties on a principal. More...
 
 searchPrincipals ($prefixPath, array $searchProperties, $test='allof')
 This method is used to search for principals matching a set of properties. More...
 
 findByUri ($uri, $principalPrefix)
 Finds a principal by its URI. More...
 
 getGroupMemberSet ($principal)
 Returns the list of members for a group-principal. More...
 
 getGroupMembership ($principal)
 Returns the list of groups a principal is a member of. More...
 
 setGroupMemberSet ($principal, array $members)
 Updates the list of group members for a group principal. More...
 
 createPrincipal ($path, MkCol $mkCol)
 Creates a new principal. More...
 
- Public Member Functions inherited from Sabre\DAVACL\PrincipalBackend\AbstractBackend
 findByUri ($uri, $principalPrefix)
 Finds a principal by its URI. More...
 
- Public Member Functions inherited from Sabre\DAVACL\PrincipalBackend\BackendInterface
 updatePrincipal ($path, \Sabre\DAV\PropPatch $propPatch)
 Updates one ore more webdav properties on a principal. More...
 

Data Fields

 $tableName = 'principals'
 
 $groupMembersTableName = 'groupmembers'
 

Protected Attributes

 $pdo
 
 $fieldMap
 

Detailed Description

PDO principal backend.

This backend assumes all principals are in a single collection. The default collection is 'principals/', but this can be overridden.

Author
Evert Pot (http://evertpot.com/) http://sabre.io/license/ Modified BSD License

Definition at line 20 of file PDO.php.

Constructor & Destructor Documentation

◆ __construct()

Sabre\DAVACL\PrincipalBackend\PDO::__construct ( \PDO  $pdo)

This is the users' primary email-address.

Sets up the backend.

Parameters
\PDO$pdo

Definition at line 70 of file PDO.php.

References Sabre\DAVACL\PrincipalBackend\PDO\$pdo.

70  {
71 
72  $this->pdo = $pdo;
73 
74  }

Member Function Documentation

◆ createPrincipal()

Sabre\DAVACL\PrincipalBackend\PDO::createPrincipal (   $path,
MkCol  $mkCol 
)

Creates a new principal.

This method receives a full path for the new principal. The mkCol object contains any additional webdav properties specified during the creation of the principal.

Parameters
string$path
MkCol$mkCol
Returns
void

Implements Sabre\DAVACL\PrincipalBackend\CreatePrincipalSupport.

Definition at line 423 of file PDO.php.

References $path, $stmt, and Sabre\DAVACL\PrincipalBackend\PDO\updatePrincipal().

423  {
424 
425  $stmt = $this->pdo->prepare('INSERT INTO ' . $this->tableName . ' (uri) VALUES (?)');
426  $stmt->execute([$path]);
427  $this->updatePrincipal($path, $mkCol);
428 
429  }
$path
Definition: aliased.php:25
$stmt
updatePrincipal($path, DAV\PropPatch $propPatch)
Updates one ore more webdav properties on a principal.
Definition: PDO.php:179
+ Here is the call graph for this function:

◆ findByUri()

Sabre\DAVACL\PrincipalBackend\PDO::findByUri (   $uri,
  $principalPrefix 
)

Finds a principal by its URI.

This method may receive any type of uri, but mailto: addresses will be the most common.

Implementation of this API is optional. It is currently used by the CalDAV system to find principals based on their email addresses. If this API is not implemented, some features may not work correctly.

This method must return a relative principal path, or null, if the principal was not found or you refuse to find it.

Parameters
string$uri
string$principalPrefix
Returns
string

Implements Sabre\DAVACL\PrincipalBackend\BackendInterface.

Definition at line 299 of file PDO.php.

References $query, $row, $stmt, and Sabre\HTTP\URLUtil\splitPath().

299  {
300  $value = null;
301  $scheme = null;
302  list($scheme, $value) = explode(":", $uri, 2);
303  if (empty($value)) return null;
304 
305  $uri = null;
306  switch ($scheme){
307  case "mailto":
308  $query = 'SELECT uri FROM ' . $this->tableName . ' WHERE lower(email)=lower(?)';
309  $stmt = $this->pdo->prepare($query);
310  $stmt->execute([$value]);
311 
312  while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
313  // Checking if the principal is in the prefix
314  list($rowPrefix) = URLUtil::splitPath($row['uri']);
315  if ($rowPrefix !== $principalPrefix) continue;
316 
317  $uri = $row['uri'];
318  break; //Stop on first match
319  }
320  break;
321  default:
322  //unsupported uri scheme
323  return null;
324  }
325  return $uri;
326  }
$stmt
$query
$row
static splitPath($path)
Returns the 'dirname' and 'basename' for a path.
Definition: URLUtil.php:83
+ Here is the call graph for this function:

◆ getGroupMemberSet()

Sabre\DAVACL\PrincipalBackend\PDO::getGroupMemberSet (   $principal)

Returns the list of members for a group-principal.

Parameters
string$principal
Returns
array

Implements Sabre\DAVACL\PrincipalBackend\BackendInterface.

Definition at line 334 of file PDO.php.

References $result, $row, $stmt, and Sabre\DAVACL\PrincipalBackend\PDO\getPrincipalByPath().

334  {
335 
336  $principal = $this->getPrincipalByPath($principal);
337  if (!$principal) throw new DAV\Exception('Principal not found');
338 
339  $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 = ?');
340  $stmt->execute([$principal['id']]);
341 
342  $result = [];
343  while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
344  $result[] = $row['uri'];
345  }
346  return $result;
347 
348  }
$result
$stmt
$row
getPrincipalByPath($path)
Returns a specific principal, specified by it's path.
Definition: PDO.php:135
+ Here is the call graph for this function:

◆ getGroupMembership()

Sabre\DAVACL\PrincipalBackend\PDO::getGroupMembership (   $principal)

Returns the list of groups a principal is a member of.

Parameters
string$principal
Returns
array

Implements Sabre\DAVACL\PrincipalBackend\BackendInterface.

Definition at line 356 of file PDO.php.

References $result, $row, $stmt, and Sabre\DAVACL\PrincipalBackend\PDO\getPrincipalByPath().

356  {
357 
358  $principal = $this->getPrincipalByPath($principal);
359  if (!$principal) throw new DAV\Exception('Principal not found');
360 
361  $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 = ?');
362  $stmt->execute([$principal['id']]);
363 
364  $result = [];
365  while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
366  $result[] = $row['uri'];
367  }
368  return $result;
369 
370  }
$result
$stmt
$row
getPrincipalByPath($path)
Returns a specific principal, specified by it's path.
Definition: PDO.php:135
+ Here is the call graph for this function:

◆ getPrincipalByPath()

Sabre\DAVACL\PrincipalBackend\PDO::getPrincipalByPath (   $path)

Returns a specific principal, specified by it's path.

The returned structure should be the exact same as from getPrincipalsByPrefix.

Parameters
string$path
Returns
array

Implements Sabre\DAVACL\PrincipalBackend\BackendInterface.

Definition at line 135 of file PDO.php.

References $key, $path, $row, and $stmt.

Referenced by Sabre\DAVACL\PrincipalBackend\PDO\getGroupMemberSet(), and Sabre\DAVACL\PrincipalBackend\PDO\getGroupMembership().

135  {
136 
137  $fields = [
138  'id',
139  'uri',
140  ];
141 
142  foreach ($this->fieldMap as $key => $value) {
143  $fields[] = $value['dbField'];
144  }
145  $stmt = $this->pdo->prepare('SELECT ' . implode(',', $fields) . ' FROM ' . $this->tableName . ' WHERE uri = ?');
146  $stmt->execute([$path]);
147 
148  $row = $stmt->fetch(\PDO::FETCH_ASSOC);
149  if (!$row) return;
150 
151  $principal = [
152  'id' => $row['id'],
153  'uri' => $row['uri'],
154  ];
155  foreach ($this->fieldMap as $key => $value) {
156  if ($row[$value['dbField']]) {
157  $principal[$key] = $row[$value['dbField']];
158  }
159  }
160  return $principal;
161 
162  }
$path
Definition: aliased.php:25
$stmt
$row
$key
Definition: croninfo.php:18
+ Here is the caller graph for this function:

◆ getPrincipalsByPrefix()

Sabre\DAVACL\PrincipalBackend\PDO::getPrincipalsByPrefix (   $prefixPath)

Returns a list of principals based on a prefix.

This prefix will often contain something like 'principals'. You are only expected to return principals that are in this base path.

You are expected to return at least a 'uri' for every user, you can return any additional properties if you wish so. Common properties are: {DAV:}displayname {http://sabredav.org/ns}email-address - This is a custom SabreDAV field that's actualy injected in a number of other properties. If you have an email address, use this property.

Parameters
string$prefixPath
Returns
array

Implements Sabre\DAVACL\PrincipalBackend\BackendInterface.

Definition at line 92 of file PDO.php.

References $key, $result, $row, and Sabre\HTTP\URLUtil\splitPath().

92  {
93 
94  $fields = [
95  'uri',
96  ];
97 
98  foreach ($this->fieldMap as $key => $value) {
99  $fields[] = $value['dbField'];
100  }
101  $result = $this->pdo->query('SELECT ' . implode(',', $fields) . ' FROM ' . $this->tableName);
102 
103  $principals = [];
104 
105  while ($row = $result->fetch(\PDO::FETCH_ASSOC)) {
106 
107  // Checking if the principal is in the prefix
108  list($rowPrefix) = URLUtil::splitPath($row['uri']);
109  if ($rowPrefix !== $prefixPath) continue;
110 
111  $principal = [
112  'uri' => $row['uri'],
113  ];
114  foreach ($this->fieldMap as $key => $value) {
115  if ($row[$value['dbField']]) {
116  $principal[$key] = $row[$value['dbField']];
117  }
118  }
119  $principals[] = $principal;
120 
121  }
122 
123  return $principals;
124 
125  }
$result
$row
$key
Definition: croninfo.php:18
static splitPath($path)
Returns the 'dirname' and 'basename' for a path.
Definition: URLUtil.php:83
+ Here is the call graph for this function:

◆ searchPrincipals()

Sabre\DAVACL\PrincipalBackend\PDO::searchPrincipals (   $prefixPath,
array  $searchProperties,
  $test = 'allof' 
)

This method is used to search for principals matching a set of properties.

This search is specifically used by RFC3744's principal-property-search REPORT.

The actual search should be a unicode-non-case-sensitive search. The keys in searchProperties are the WebDAV property names, while the values are the property values to search on.

By default, if multiple properties are submitted to this method, the various properties should be combined with 'AND'. If $test is set to 'anyof', it should be combined using 'OR'.

This method should simply return an array with full principal uri's.

If somebody attempted to search on a property the backend does not support, you should simply return 0 results.

You can also just return 0 results if you choose to not support searching at all, but keep in mind that this may stop certain features from working.

Parameters
string$prefixPath
array$searchProperties
string$test
Returns
array

Implements Sabre\DAVACL\PrincipalBackend\BackendInterface.

Definition at line 242 of file PDO.php.

References $query, $row, $stmt, $test, $values, and Sabre\HTTP\URLUtil\splitPath().

242  {
243  if (count($searchProperties) == 0) return []; //No criteria
244 
245  $query = 'SELECT uri FROM ' . $this->tableName . ' WHERE ';
246  $values = [];
247  foreach ($searchProperties as $property => $value) {
248  switch ($property) {
249  case '{DAV:}displayname' :
250  $column = "displayname";
251  break;
252  case '{http://sabredav.org/ns}email-address' :
253  $column = "email";
254  break;
255  default :
256  // Unsupported property
257  return [];
258  }
259  if (count($values) > 0) $query .= (strcmp($test, "anyof") == 0 ? " OR " : " AND ");
260  $query .= 'lower(' . $column . ') LIKE lower(?)';
261  $values[] = '%' . $value . '%';
262 
263  }
264  $stmt = $this->pdo->prepare($query);
265  $stmt->execute($values);
266 
267  $principals = [];
268  while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
269 
270  // Checking if the principal is in the prefix
271  list($rowPrefix) = URLUtil::splitPath($row['uri']);
272  if ($rowPrefix !== $prefixPath) continue;
273 
274  $principals[] = $row['uri'];
275 
276  }
277 
278  return $principals;
279 
280  }
$stmt
$values
$query
$row
static splitPath($path)
Returns the 'dirname' and 'basename' for a path.
Definition: URLUtil.php:83
$test
Definition: Utf8Test.php:84
+ Here is the call graph for this function:

◆ setGroupMemberSet()

Sabre\DAVACL\PrincipalBackend\PDO::setGroupMemberSet (   $principal,
array  $members 
)

Updates the list of group members for a group principal.

The principals should be passed as a list of uri's.

Parameters
string$principal
array$members
Returns
void

Implements Sabre\DAVACL\PrincipalBackend\BackendInterface.

Definition at line 381 of file PDO.php.

References $row, and $stmt.

381  {
382 
383  // Grabbing the list of principal id's.
384  $stmt = $this->pdo->prepare('SELECT id, uri FROM ' . $this->tableName . ' WHERE uri IN (? ' . str_repeat(', ? ', count($members)) . ');');
385  $stmt->execute(array_merge([$principal], $members));
386 
387  $memberIds = [];
388  $principalId = null;
389 
390  while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
391  if ($row['uri'] == $principal) {
392  $principalId = $row['id'];
393  } else {
394  $memberIds[] = $row['id'];
395  }
396  }
397  if (!$principalId) throw new DAV\Exception('Principal not found');
398 
399  // Wiping out old members
400  $stmt = $this->pdo->prepare('DELETE FROM ' . $this->groupMembersTableName . ' WHERE principal_id = ?;');
401  $stmt->execute([$principalId]);
402 
403  foreach ($memberIds as $memberId) {
404 
405  $stmt = $this->pdo->prepare('INSERT INTO ' . $this->groupMembersTableName . ' (principal_id, member_id) VALUES (?, ?);');
406  $stmt->execute([$principalId, $memberId]);
407 
408  }
409 
410  }
$stmt
$row

◆ updatePrincipal()

Sabre\DAVACL\PrincipalBackend\PDO::updatePrincipal (   $path,
DAV\PropPatch  $propPatch 
)

Updates one ore more webdav properties on a principal.

The list of mutations is stored in a Sabre object. To do the actual updates, you must tell this object which properties you're going to process with the handle() method.

Calling the handle method is like telling the PropPatch object "I promise I can handle updating this property".

Read the PropPatch documentation for more info and examples.

Parameters
string$path
DAV\PropPatch$propPatch

Definition at line 179 of file PDO.php.

References $key, $path, $query, $stmt, and $values.

Referenced by Sabre\DAVACL\PrincipalBackend\PDO\createPrincipal().

179  {
180 
181  $propPatch->handle(array_keys($this->fieldMap), function($properties) use ($path) {
182 
183  $query = "UPDATE " . $this->tableName . " SET ";
184  $first = true;
185 
186  $values = [];
187 
188  foreach ($properties as $key => $value) {
189 
190  $dbField = $this->fieldMap[$key]['dbField'];
191 
192  if (!$first) {
193  $query .= ', ';
194  }
195  $first = false;
196  $query .= $dbField . ' = :' . $dbField;
197  $values[$dbField] = $value;
198 
199  }
200 
201  $query .= " WHERE uri = :uri";
202  $values['uri'] = $path;
203 
204  $stmt = $this->pdo->prepare($query);
205  $stmt->execute($values);
206 
207  return true;
208 
209  });
210 
211  }
$path
Definition: aliased.php:25
$stmt
$values
$query
$key
Definition: croninfo.php:18
+ Here is the caller graph for this function:

Field Documentation

◆ $fieldMap

Sabre\DAVACL\PrincipalBackend\PDO::$fieldMap
protected
Initial value:
= [
'{DAV:}displayname' => [
'dbField' => 'displayname'

Definition at line 48 of file PDO.php.

◆ $groupMembersTableName

Sabre\DAVACL\PrincipalBackend\PDO::$groupMembersTableName = 'groupmembers'

Definition at line 34 of file PDO.php.

◆ $pdo

Sabre\DAVACL\PrincipalBackend\PDO::$pdo
protected

Definition at line 41 of file PDO.php.

Referenced by Sabre\DAVACL\PrincipalBackend\PDO\__construct().

◆ $tableName

Sabre\DAVACL\PrincipalBackend\PDO::$tableName = 'principals'

Definition at line 27 of file PDO.php.


The documentation for this class was generated from the following file: