1
0

Dummy.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\Util\User;
  8. use OC\User\Backend;
  9. /**
  10. * dummy user backend, does not keep state, only for testing use
  11. */
  12. class Dummy extends Backend implements \OCP\IUserBackend {
  13. private $users = [];
  14. private $displayNames = [];
  15. /**
  16. * Create a new user
  17. *
  18. * @param string $uid The username of the user to create
  19. * @param string $password The password of the new user
  20. * @return bool
  21. *
  22. * Creates a new user. Basic checking of username is done in OC_User
  23. * itself, not in its subclasses.
  24. */
  25. public function createUser($uid, $password) {
  26. if (isset($this->users[$uid])) {
  27. return false;
  28. } else {
  29. $this->users[$uid] = $password;
  30. return true;
  31. }
  32. }
  33. /**
  34. * delete a user
  35. *
  36. * @param string $uid The username of the user to delete
  37. * @return bool
  38. *
  39. * Deletes a user
  40. */
  41. public function deleteUser($uid) {
  42. if (isset($this->users[$uid])) {
  43. unset($this->users[$uid]);
  44. return true;
  45. } else {
  46. return false;
  47. }
  48. }
  49. /**
  50. * Set password
  51. *
  52. * @param string $uid The username
  53. * @param string $password The new password
  54. * @return bool
  55. *
  56. * Change the password of a user
  57. */
  58. public function setPassword($uid, $password) {
  59. if (isset($this->users[$uid])) {
  60. $this->users[$uid] = $password;
  61. return true;
  62. } else {
  63. return false;
  64. }
  65. }
  66. /**
  67. * Check if the password is correct
  68. *
  69. * @param string $uid The username
  70. * @param string $password The password
  71. * @return string|bool
  72. *
  73. * Check if the password is correct without logging in the user
  74. * returns the user id or false
  75. */
  76. public function checkPassword($uid, $password) {
  77. if (isset($this->users[$uid]) && $this->users[$uid] === $password) {
  78. return $uid;
  79. }
  80. return false;
  81. }
  82. public function loginName2UserName($loginName) {
  83. if (isset($this->users[strtolower($loginName)])) {
  84. return strtolower($loginName);
  85. }
  86. return false;
  87. }
  88. /**
  89. * Get a list of all users
  90. *
  91. * @param string $search
  92. * @param null|int $limit
  93. * @param null|int $offset
  94. * @return string[] an array of all uids
  95. */
  96. public function getUsers($search = '', $limit = null, $offset = null) {
  97. if (empty($search)) {
  98. return array_keys($this->users);
  99. }
  100. $result = [];
  101. foreach (array_keys($this->users) as $user) {
  102. if (stripos($user, $search) !== false) {
  103. $result[] = $user;
  104. }
  105. }
  106. return $result;
  107. }
  108. /**
  109. * check if a user exists
  110. *
  111. * @param string $uid the username
  112. * @return boolean
  113. */
  114. public function userExists($uid) {
  115. return isset($this->users[$uid]);
  116. }
  117. /**
  118. * @return bool
  119. */
  120. public function hasUserListings() {
  121. return true;
  122. }
  123. /**
  124. * counts the users in the database
  125. *
  126. * @return int|bool
  127. */
  128. public function countUsers() {
  129. return 0;
  130. }
  131. public function setDisplayName($uid, $displayName) {
  132. $this->displayNames[$uid] = $displayName;
  133. return true;
  134. }
  135. public function getDisplayName($uid) {
  136. return $this->displayNames[$uid] ?? $uid;
  137. }
  138. /**
  139. * Backend name to be shown in user management
  140. * @return string the name of the backend to be shown
  141. */
  142. public function getBackendName() {
  143. return 'Dummy';
  144. }
  145. }