1
0

Dummy.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Felix Moeller <mail@felixmoeller.de>
  5. * @author Lukas Reschke <lukas@owncloud.com>
  6. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <icewind@owncloud.com>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Roeland Jago Douma <rullzer@owncloud.com>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @copyright Copyright (c) 2016, ownCloud, Inc.
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace Test\Util\Group;
  30. use Test\Util\User\Dummy as DummyUser;
  31. use OCP\Group\Backend\ABackend;
  32. use OCP\Group\Backend\IDeleteGroupBackend;
  33. use OCP\Group\Backend\IAddToGroupBackend;
  34. use OCP\Group\Backend\IRemoveFromGroupBackend;
  35. use OCP\Group\Backend\ICreateGroupBackend;
  36. use OCP\Group\Backend\ICountUsersBackend;
  37. /**
  38. * Dummy group backend, does not keep state, only for testing use
  39. */
  40. class Dummy extends ABackend implements ICreateGroupBackend, IDeleteGroupBackend, IAddToGroupBackend, IRemoveFromGroupBackend, ICountUsersBackend {
  41. private $groups = [];
  42. /**
  43. * Try to create a new group
  44. * @param string $gid The name of the group to create
  45. * @return bool
  46. *
  47. * Tries to create a new group. If the group name already exists, false will
  48. * be returned.
  49. */
  50. public function createGroup(string $gid): bool {
  51. if (!isset($this->groups[$gid])) {
  52. $this->groups[$gid] = [];
  53. return true;
  54. } else {
  55. return false;
  56. }
  57. }
  58. /**
  59. * delete a group
  60. * @param string $gid gid of the group to delete
  61. * @return bool
  62. *
  63. * Deletes a group and removes it from the group_user-table
  64. */
  65. public function deleteGroup(string $gid): bool {
  66. if (isset($this->groups[$gid])) {
  67. unset($this->groups[$gid]);
  68. return true;
  69. } else {
  70. return false;
  71. }
  72. }
  73. /**
  74. * is user in group?
  75. * @param string $uid uid of the user
  76. * @param string $gid gid of the group
  77. * @return bool
  78. *
  79. * Checks whether the user is member of a group or not.
  80. */
  81. public function inGroup($uid, $gid) {
  82. if (isset($this->groups[$gid])) {
  83. return (array_search($uid, $this->groups[$gid]) !== false);
  84. } else {
  85. return false;
  86. }
  87. }
  88. /**
  89. * Add a user to a group
  90. * @param string $uid Name of the user to add to group
  91. * @param string $gid Name of the group in which add the user
  92. * @return bool
  93. *
  94. * Adds a user to a group.
  95. */
  96. public function addToGroup(string $uid, string $gid): bool {
  97. if (isset($this->groups[$gid])) {
  98. if (array_search($uid, $this->groups[$gid]) === false) {
  99. $this->groups[$gid][] = $uid;
  100. return true;
  101. } else {
  102. return false;
  103. }
  104. } else {
  105. return false;
  106. }
  107. }
  108. /**
  109. * Removes a user from a group
  110. * @param string $uid Name of the user to remove from group
  111. * @param string $gid Name of the group from which remove the user
  112. * @return bool
  113. *
  114. * removes the user from a group.
  115. */
  116. public function removeFromGroup(string $uid, string $gid): bool {
  117. if (isset($this->groups[$gid])) {
  118. if (($index = array_search($uid, $this->groups[$gid])) !== false) {
  119. unset($this->groups[$gid][$index]);
  120. return true;
  121. } else {
  122. return false;
  123. }
  124. } else {
  125. return false;
  126. }
  127. }
  128. /**
  129. * Get all groups a user belongs to
  130. * @param string $uid Name of the user
  131. * @return array an array of group names
  132. *
  133. * This function fetches all groups a user belongs to. It does not check
  134. * if the user exists at all.
  135. */
  136. public function getUserGroups($uid) {
  137. $groups = [];
  138. $allGroups = array_keys($this->groups);
  139. foreach ($allGroups as $group) {
  140. if ($this->inGroup($uid, $group)) {
  141. $groups[] = $group;
  142. }
  143. }
  144. return $groups;
  145. }
  146. /**
  147. * Get a list of all groups
  148. * @param string $search
  149. * @param int $limit
  150. * @param int $offset
  151. * @return array an array of group names
  152. */
  153. public function getGroups($search = '', $limit = -1, $offset = 0) {
  154. if (empty($search)) {
  155. return array_keys($this->groups);
  156. }
  157. $result = [];
  158. foreach (array_keys($this->groups) as $group) {
  159. if (stripos($group, $search) !== false) {
  160. $result[] = $group;
  161. }
  162. }
  163. return $result;
  164. }
  165. /**
  166. * Get a list of all users in a group
  167. * @param string $gid
  168. * @param string $search
  169. * @param int $limit
  170. * @param int $offset
  171. * @return array an array of user IDs
  172. */
  173. public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  174. if (isset($this->groups[$gid])) {
  175. if (empty($search)) {
  176. $length = $limit < 0 ? null : $limit;
  177. return array_slice($this->groups[$gid], $offset, $length);
  178. }
  179. $result = [];
  180. foreach ($this->groups[$gid] as $user) {
  181. if (stripos($user, $search) !== false) {
  182. $result[] = $user;
  183. }
  184. }
  185. return $result;
  186. } else {
  187. return [];
  188. }
  189. }
  190. public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array {
  191. if (isset($this->groups[$gid])) {
  192. if (empty($search)) {
  193. $length = $limit < 0 ? null : $limit;
  194. $users = array_slice($this->groups[$gid], $offset, $length);
  195. return array_map(fn ($user) => new DummyUser($user, ''));
  196. }
  197. $result = [];
  198. foreach ($this->groups[$gid] as $user) {
  199. if (stripos($user, $search) !== false) {
  200. $result[$user] = new DummyUser($user, '');
  201. }
  202. }
  203. return $result;
  204. } else {
  205. return [];
  206. }
  207. }
  208. /**
  209. * get the number of all users in a group
  210. * @param string $gid
  211. * @param string $search
  212. * @param int $limit
  213. * @param int $offset
  214. * @return int
  215. */
  216. public function countUsersInGroup(string $gid, string $search = ''): int {
  217. if (isset($this->groups[$gid])) {
  218. if (empty($search)) {
  219. return count($this->groups[$gid]);
  220. }
  221. $count = 0;
  222. foreach ($this->groups[$gid] as $user) {
  223. if (stripos($user, $search) !== false) {
  224. $count++;
  225. }
  226. }
  227. return $count;
  228. }
  229. return 0;
  230. }
  231. public function groupExists($gid) {
  232. return isset($this->groups[$gid]);
  233. }
  234. }