dummy.php 5.1 KB

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