Dummy.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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|bool
  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. }
  100. return false;
  101. }
  102. public function loginName2UserName($loginName) {
  103. if(isset($this->users[strtolower($loginName)])) {
  104. return strtolower($loginName);
  105. }
  106. return false;
  107. }
  108. /**
  109. * Get a list of all users
  110. *
  111. * @param string $search
  112. * @param null|int $limit
  113. * @param null|int $offset
  114. * @return string[] an array of all uids
  115. */
  116. public function getUsers($search = '', $limit = null, $offset = null) {
  117. if (empty($search)) {
  118. return array_keys($this->users);
  119. }
  120. $result = array();
  121. foreach (array_keys($this->users) as $user) {
  122. if (stripos($user, $search) !== false) {
  123. $result[] = $user;
  124. }
  125. }
  126. return $result;
  127. }
  128. /**
  129. * check if a user exists
  130. *
  131. * @param string $uid the username
  132. * @return boolean
  133. */
  134. public function userExists($uid) {
  135. return isset($this->users[$uid]);
  136. }
  137. /**
  138. * @return bool
  139. */
  140. public function hasUserListings() {
  141. return true;
  142. }
  143. /**
  144. * counts the users in the database
  145. *
  146. * @return int|bool
  147. */
  148. public function countUsers() {
  149. return 0;
  150. }
  151. public function setDisplayName($uid, $displayName) {
  152. $this->displayNames[$uid] = $displayName;
  153. }
  154. public function getDisplayName($uid) {
  155. return isset($this->displayNames[$uid])? $this->displayNames[$uid]: $uid;
  156. }
  157. /**
  158. * Backend name to be shown in user management
  159. * @return string the name of the backend to be shown
  160. */
  161. public function getBackendName(){
  162. return 'Dummy';
  163. }
  164. }