Dummy.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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\Group;
  8. use OCP\Group\Backend\ABackend;
  9. use OCP\Group\Backend\IAddToGroupBackend;
  10. use OCP\Group\Backend\ICountUsersBackend;
  11. use OCP\Group\Backend\ICreateGroupBackend;
  12. use OCP\Group\Backend\IDeleteGroupBackend;
  13. use OCP\Group\Backend\IRemoveFromGroupBackend;
  14. use Test\Util\User\Dummy as DummyUser;
  15. /**
  16. * Dummy group backend, does not keep state, only for testing use
  17. */
  18. class Dummy extends ABackend implements ICreateGroupBackend, IDeleteGroupBackend, IAddToGroupBackend, IRemoveFromGroupBackend, ICountUsersBackend {
  19. private $groups = [];
  20. /**
  21. * Try to create a new group
  22. * @param string $gid The name of the group to create
  23. * @return bool
  24. *
  25. * Tries to create a new group. If the group name already exists, false will
  26. * be returned.
  27. */
  28. public function createGroup(string $gid): bool {
  29. if (!isset($this->groups[$gid])) {
  30. $this->groups[$gid] = [];
  31. return true;
  32. } else {
  33. return false;
  34. }
  35. }
  36. /**
  37. * delete a group
  38. * @param string $gid gid of the group to delete
  39. * @return bool
  40. *
  41. * Deletes a group and removes it from the group_user-table
  42. */
  43. public function deleteGroup(string $gid): bool {
  44. if (isset($this->groups[$gid])) {
  45. unset($this->groups[$gid]);
  46. return true;
  47. } else {
  48. return false;
  49. }
  50. }
  51. /**
  52. * is user in group?
  53. * @param string $uid uid of the user
  54. * @param string $gid gid of the group
  55. * @return bool
  56. *
  57. * Checks whether the user is member of a group or not.
  58. */
  59. public function inGroup($uid, $gid) {
  60. if (isset($this->groups[$gid])) {
  61. return (array_search($uid, $this->groups[$gid]) !== false);
  62. } else {
  63. return false;
  64. }
  65. }
  66. /**
  67. * Add a user to a group
  68. * @param string $uid Name of the user to add to group
  69. * @param string $gid Name of the group in which add the user
  70. * @return bool
  71. *
  72. * Adds a user to a group.
  73. */
  74. public function addToGroup(string $uid, string $gid): bool {
  75. if (isset($this->groups[$gid])) {
  76. if (array_search($uid, $this->groups[$gid]) === false) {
  77. $this->groups[$gid][] = $uid;
  78. return true;
  79. } else {
  80. return false;
  81. }
  82. } else {
  83. return false;
  84. }
  85. }
  86. /**
  87. * Removes a user from a group
  88. * @param string $uid Name of the user to remove from group
  89. * @param string $gid Name of the group from which remove the user
  90. * @return bool
  91. *
  92. * removes the user from a group.
  93. */
  94. public function removeFromGroup(string $uid, string $gid): bool {
  95. if (isset($this->groups[$gid])) {
  96. if (($index = array_search($uid, $this->groups[$gid])) !== false) {
  97. unset($this->groups[$gid][$index]);
  98. return true;
  99. } else {
  100. return false;
  101. }
  102. } else {
  103. return false;
  104. }
  105. }
  106. /**
  107. * Get all groups a user belongs to
  108. * @param string $uid Name of the user
  109. * @return array an array of group names
  110. *
  111. * This function fetches all groups a user belongs to. It does not check
  112. * if the user exists at all.
  113. */
  114. public function getUserGroups($uid) {
  115. $groups = [];
  116. $allGroups = array_keys($this->groups);
  117. foreach ($allGroups as $group) {
  118. if ($this->inGroup($uid, $group)) {
  119. $groups[] = $group;
  120. }
  121. }
  122. return $groups;
  123. }
  124. /**
  125. * Get a list of all groups
  126. * @param string $search
  127. * @param int $limit
  128. * @param int $offset
  129. * @return array an array of group names
  130. */
  131. public function getGroups($search = '', $limit = -1, $offset = 0) {
  132. if (empty($search)) {
  133. return array_keys($this->groups);
  134. }
  135. $result = [];
  136. foreach (array_keys($this->groups) as $group) {
  137. if (stripos($group, $search) !== false) {
  138. $result[] = $group;
  139. }
  140. }
  141. return $result;
  142. }
  143. /**
  144. * Get a list of all users in a group
  145. * @param string $gid
  146. * @param string $search
  147. * @param int $limit
  148. * @param int $offset
  149. * @return array an array of user IDs
  150. */
  151. public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  152. if (isset($this->groups[$gid])) {
  153. if (empty($search)) {
  154. $length = $limit < 0 ? null : $limit;
  155. return array_slice($this->groups[$gid], $offset, $length);
  156. }
  157. $result = [];
  158. foreach ($this->groups[$gid] as $user) {
  159. if (stripos($user, $search) !== false) {
  160. $result[] = $user;
  161. }
  162. }
  163. return $result;
  164. } else {
  165. return [];
  166. }
  167. }
  168. public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array {
  169. if (isset($this->groups[$gid])) {
  170. if (empty($search)) {
  171. $length = $limit < 0 ? null : $limit;
  172. $users = array_slice($this->groups[$gid], $offset, $length);
  173. return array_map(fn ($user) => new DummyUser($user, ''));
  174. }
  175. $result = [];
  176. foreach ($this->groups[$gid] as $user) {
  177. if (stripos($user, $search) !== false) {
  178. $result[$user] = new DummyUser($user, '');
  179. }
  180. }
  181. return $result;
  182. } else {
  183. return [];
  184. }
  185. }
  186. /**
  187. * get the number of all users in a group
  188. * @param string $gid
  189. * @param string $search
  190. * @param int $limit
  191. * @param int $offset
  192. * @return int
  193. */
  194. public function countUsersInGroup(string $gid, string $search = ''): int {
  195. if (isset($this->groups[$gid])) {
  196. if (empty($search)) {
  197. return count($this->groups[$gid]);
  198. }
  199. $count = 0;
  200. foreach ($this->groups[$gid] as $user) {
  201. if (stripos($user, $search) !== false) {
  202. $count++;
  203. }
  204. }
  205. return $count;
  206. }
  207. return 0;
  208. }
  209. public function groupExists($gid) {
  210. return isset($this->groups[$gid]);
  211. }
  212. }