PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Sabre/DAVACL/PrincipalBackend/Mock.php

https://github.com/KOLANICH/SabreDAV
PHP | 184 lines | 89 code | 47 blank | 48 comment | 10 complexity | 5cc69b86ab62ebf57572fc786f5b348c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Sabre\DAVACL\PrincipalBackend;
  3. class Mock extends AbstractBackend {
  4. public $groupMembers = array();
  5. public $principals;
  6. function __construct() {
  7. $this->principals = array(
  8. array(
  9. 'uri' => 'principals/user1',
  10. '{DAV:}displayname' => 'User 1',
  11. '{http://sabredav.org/ns}email-address' => 'user1.sabredav@sabredav.org',
  12. '{http://sabredav.org/ns}vcard-url' => 'addressbooks/user1/book1/vcard1.vcf',
  13. ),
  14. array(
  15. 'uri' => 'principals/admin',
  16. '{DAV:}displayname' => 'Admin',
  17. ),
  18. array(
  19. 'uri' => 'principals/user2',
  20. '{DAV:}displayname' => 'User 2',
  21. '{http://sabredav.org/ns}email-address' => 'user2.sabredav@sabredav.org',
  22. ),
  23. );
  24. }
  25. function getPrincipalsByPrefix($prefix) {
  26. $prefix = trim($prefix,'/') . '/';
  27. $return = array();
  28. foreach($this->principals as $principal) {
  29. if (strpos($principal['uri'], $prefix)!==0) continue;
  30. $return[] = $principal;
  31. }
  32. return $return;
  33. }
  34. function addPrincipal(array $principal) {
  35. $this->principals[] = $principal;
  36. }
  37. function getPrincipalByPath($path) {
  38. foreach($this->getPrincipalsByPrefix('principals') as $principal) {
  39. if ($principal['uri'] === $path) return $principal;
  40. }
  41. }
  42. function searchPrincipals($prefixPath, array $searchProperties) {
  43. $matches = array();
  44. foreach($this->getPrincipalsByPrefix($prefixPath) as $principal) {
  45. foreach($searchProperties as $key=>$value) {
  46. if (!isset($principal[$key])) {
  47. continue 2;
  48. }
  49. if (mb_stripos($principal[$key],$value, 0, 'UTF-8')===false) {
  50. continue 2;
  51. }
  52. }
  53. $matches[] = $principal['uri'];
  54. }
  55. return $matches;
  56. }
  57. function getGroupMemberSet($path) {
  58. return isset($this->groupMembers[$path]) ? $this->groupMembers[$path] : array();
  59. }
  60. function getGroupMembership($path) {
  61. $membership = array();
  62. foreach($this->groupMembers as $group=>$members) {
  63. if (in_array($path, $members)) $membership[] = $group;
  64. }
  65. return $membership;
  66. }
  67. function setGroupMemberSet($path, array $members) {
  68. $this->groupMembers[$path] = $members;
  69. }
  70. /**
  71. * Updates one ore more webdav properties on a principal.
  72. *
  73. * The list of mutations is supplied as an array. Each key in the array is
  74. * a propertyname, such as {DAV:}displayname.
  75. *
  76. * Each value is the actual value to be updated. If a value is null, it
  77. * must be deleted.
  78. *
  79. * This method should be atomic. It must either completely succeed, or
  80. * completely fail. Success and failure can simply be returned as 'true' or
  81. * 'false'.
  82. *
  83. * It is also possible to return detailed failure information. In that case
  84. * an array such as this should be returned:
  85. *
  86. * array(
  87. * 200 => array(
  88. * '{DAV:}prop1' => null,
  89. * ),
  90. * 201 => array(
  91. * '{DAV:}prop2' => null,
  92. * ),
  93. * 403 => array(
  94. * '{DAV:}prop3' => null,
  95. * ),
  96. * 424 => array(
  97. * '{DAV:}prop4' => null,
  98. * ),
  99. * );
  100. *
  101. * In this previous example prop1 was successfully updated or deleted, and
  102. * prop2 was succesfully created.
  103. *
  104. * prop3 failed to update due to '403 Forbidden' and because of this prop4
  105. * also could not be updated with '424 Failed dependency'.
  106. *
  107. * This last example was actually incorrect. While 200 and 201 could appear
  108. * in 1 response, if there's any error (403) the other properties should
  109. * always fail with 423 (failed dependency).
  110. *
  111. * But anyway, if you don't want to scratch your head over this, just
  112. * return true or false.
  113. *
  114. * @param string $path
  115. * @param array $mutations
  116. * @return array|bool
  117. */
  118. public function updatePrincipal($path, $mutations) {
  119. $value = null;
  120. foreach($this->principals as $principalIndex=>$value) {
  121. if ($value['uri'] === $path) {
  122. $principal = $value;
  123. break;
  124. }
  125. }
  126. if (!$principal) return false;
  127. foreach($mutations as $prop=>$value) {
  128. if (is_null($value) && isset($principal[$prop])) {
  129. unset($principal[$prop]);
  130. } else {
  131. $principal[$prop] = $value;
  132. }
  133. }
  134. $this->principals[$principalIndex] = $principal;
  135. return true;
  136. }
  137. }