1<?php 2 3namespace Sabre\DAVACL\PrincipalBackend; 4 5class Mock extends AbstractBackend { 6 7 public $groupMembers = []; 8 public $principals; 9 10 function __construct(array $principals = null) { 11 12 $this->principals = $principals; 13 14 if (is_null($principals)) { 15 16 $this->principals = [ 17 [ 18 'uri' => 'principals/user1', 19 '{DAV:}displayname' => 'User 1', 20 '{http://sabredav.org/ns}email-address' => 'user1.sabredav@sabredav.org', 21 '{http://sabredav.org/ns}vcard-url' => 'addressbooks/user1/book1/vcard1.vcf', 22 ], 23 [ 24 'uri' => 'principals/admin', 25 '{DAV:}displayname' => 'Admin', 26 ], 27 [ 28 'uri' => 'principals/user2', 29 '{DAV:}displayname' => 'User 2', 30 '{http://sabredav.org/ns}email-address' => 'user2.sabredav@sabredav.org', 31 ], 32 ]; 33 34 } 35 36 } 37 38 function getPrincipalsByPrefix($prefix) { 39 40 $prefix = trim($prefix, '/'); 41 if ($prefix) $prefix .= '/'; 42 $return = []; 43 44 foreach ($this->principals as $principal) { 45 46 if ($prefix && strpos($principal['uri'], $prefix) !== 0) continue; 47 48 $return[] = $principal; 49 50 } 51 52 return $return; 53 54 } 55 56 function addPrincipal(array $principal) { 57 58 $this->principals[] = $principal; 59 60 } 61 62 function getPrincipalByPath($path) { 63 64 foreach ($this->getPrincipalsByPrefix('principals') as $principal) { 65 if ($principal['uri'] === $path) return $principal; 66 } 67 68 } 69 70 function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { 71 72 $matches = []; 73 foreach ($this->getPrincipalsByPrefix($prefixPath) as $principal) { 74 75 foreach ($searchProperties as $key => $value) { 76 77 if (!isset($principal[$key])) { 78 continue 2; 79 } 80 if (mb_stripos($principal[$key], $value, 0, 'UTF-8') === false) { 81 continue 2; 82 } 83 84 // We have a match for this searchProperty! 85 if ($test === 'allof') { 86 continue; 87 } else { 88 break; 89 } 90 91 } 92 $matches[] = $principal['uri']; 93 94 } 95 return $matches; 96 97 } 98 99 function getGroupMemberSet($path) { 100 101 return isset($this->groupMembers[$path]) ? $this->groupMembers[$path] : []; 102 103 } 104 105 function getGroupMembership($path) { 106 107 $membership = []; 108 foreach ($this->groupMembers as $group => $members) { 109 if (in_array($path, $members)) $membership[] = $group; 110 } 111 return $membership; 112 113 } 114 115 function setGroupMemberSet($path, array $members) { 116 117 $this->groupMembers[$path] = $members; 118 119 } 120 121 /** 122 * Updates one ore more webdav properties on a principal. 123 * 124 * The list of mutations is stored in a Sabre\DAV\PropPatch object. 125 * To do the actual updates, you must tell this object which properties 126 * you're going to process with the handle() method. 127 * 128 * Calling the handle method is like telling the PropPatch object "I 129 * promise I can handle updating this property". 130 * 131 * Read the PropPatch documenation for more info and examples. 132 * 133 * @param string $path 134 * @param \Sabre\DAV\PropPatch $propPatch 135 */ 136 function updatePrincipal($path, \Sabre\DAV\PropPatch $propPatch) { 137 138 $value = null; 139 foreach ($this->principals as $principalIndex => $value) { 140 if ($value['uri'] === $path) { 141 $principal = $value; 142 break; 143 } 144 } 145 if (!$principal) return; 146 147 $propPatch->handleRemaining(function($mutations) use ($principal, $principalIndex) { 148 149 foreach ($mutations as $prop => $value) { 150 151 if (is_null($value) && isset($principal[$prop])) { 152 unset($principal[$prop]); 153 } else { 154 $principal[$prop] = $value; 155 } 156 157 } 158 159 $this->principals[$principalIndex] = $principal; 160 161 return true; 162 163 }); 164 165 } 166 167 168} 169