Dummy.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * @author Andreas Fischer <bantu@owncloud.com>
  4. * @author Arthur Schiwon <blizzz@owncloud.com>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <icewind@owncloud.com>
  8. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @copyright Copyright (c) 2015, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace Test\Util\User;
  28. use \OC\User\Backend;
  29. /**
  30. * dummy user backend, does not keep state, only for testing use
  31. */
  32. class Dummy extends Backend implements \OCP\IUserBackend {
  33. private $users = array();
  34. private $displayNames = array();
  35. /**
  36. * Create a new user
  37. *
  38. * @param string $uid The username of the user to create
  39. * @param string $password The password of the new user
  40. * @return bool
  41. *
  42. * Creates a new user. Basic checking of username is done in OC_User
  43. * itself, not in its subclasses.
  44. */
  45. public function createUser($uid, $password) {
  46. if (isset($this->users[$uid])) {
  47. return false;
  48. } else {
  49. $this->users[$uid] = $password;
  50. return true;
  51. }
  52. }
  53. /**
  54. * delete a user
  55. *
  56. * @param string $uid The username of the user to delete
  57. * @return bool
  58. *
  59. * Deletes a user
  60. */
  61. public function deleteUser($uid) {
  62. if (isset($this->users[$uid])) {
  63. unset($this->users[$uid]);
  64. return true;
  65. } else {
  66. return false;
  67. }
  68. }
  69. /**
  70. * Set password
  71. *
  72. * @param string $uid The username
  73. * @param string $password The new password
  74. * @return bool
  75. *
  76. * Change the password of a user
  77. */
  78. public function setPassword($uid, $password) {
  79. if (isset($this->users[$uid])) {
  80. $this->users[$uid] = $password;
  81. return true;
  82. } else {
  83. return false;
  84. }
  85. }
  86. /**
  87. * Check if the password is correct
  88. *
  89. * @param string $uid The username
  90. * @param string $password The password
  91. * @return string
  92. *
  93. * Check if the password is correct without logging in the user
  94. * returns the user id or false
  95. */
  96. public function checkPassword($uid, $password) {
  97. if (isset($this->users[$uid]) && $this->users[$uid] === $password) {
  98. return $uid;
  99. } else {
  100. return false;
  101. }
  102. }
  103. /**
  104. * Get a list of all users
  105. *
  106. * @param string $search
  107. * @param null|int $limit
  108. * @param null|int $offset
  109. * @return string[] an array of all uids
  110. */
  111. public function getUsers($search = '', $limit = null, $offset = null) {
  112. if (empty($search)) {
  113. return array_keys($this->users);
  114. }
  115. $result = array();
  116. foreach (array_keys($this->users) as $user) {
  117. if (stripos($user, $search) !== false) {
  118. $result[] = $user;
  119. }
  120. }
  121. return $result;
  122. }
  123. /**
  124. * check if a user exists
  125. *
  126. * @param string $uid the username
  127. * @return boolean
  128. */
  129. public function userExists($uid) {
  130. return isset($this->users[$uid]);
  131. }
  132. /**
  133. * @return bool
  134. */
  135. public function hasUserListings() {
  136. return true;
  137. }
  138. /**
  139. * counts the users in the database
  140. *
  141. * @return int|bool
  142. */
  143. public function countUsers() {
  144. return 0;
  145. }
  146. public function setDisplayName($uid, $displayName) {
  147. $this->displayNames[$uid] = $displayName;
  148. }
  149. public function getDisplayName($uid) {
  150. return isset($this->displayNames[$uid])? $this->displayNames[$uid]: $uid;
  151. }
  152. /**
  153. * Backend name to be shown in user management
  154. * @return string the name of the backend to be shown
  155. */
  156. public function getBackendName(){
  157. return 'Dummy';
  158. }
  159. }