Dummy.php 5.3 KB

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