Group.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Robin McCorkell <robin@mccorkell.me.uk>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Vincent Petry <pvince81@owncloud.com>
  13. * @author John Molakvoæ <skjnldsv@protonmail.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\Group;
  31. use OCP\GroupInterface;
  32. use OCP\IGroup;
  33. use OCP\IUser;
  34. use OCP\Group\Backend\ICountDisabledInGroup;
  35. class Group implements IGroup {
  36. /** @var null|string */
  37. protected $displayName;
  38. /**
  39. * @var string $id
  40. */
  41. private $gid;
  42. /**
  43. * @var \OC\User\User[] $users
  44. */
  45. private $users = array();
  46. /**
  47. * @var bool $usersLoaded
  48. */
  49. private $usersLoaded;
  50. /**
  51. * @var \OC\Group\Backend[]|\OC\Group\Database[] $backend
  52. */
  53. private $backends;
  54. /**
  55. * @var \OC\Hooks\PublicEmitter $emitter
  56. */
  57. private $emitter;
  58. /**
  59. * @var \OC\User\Manager $userManager
  60. */
  61. private $userManager;
  62. /**
  63. * @param string $gid
  64. * @param \OC\Group\Backend[] $backends
  65. * @param \OC\User\Manager $userManager
  66. * @param \OC\Hooks\PublicEmitter $emitter
  67. * @param string $displayName
  68. */
  69. public function __construct($gid, $backends, $userManager, $emitter = null, $displayName = null) {
  70. $this->gid = $gid;
  71. $this->backends = $backends;
  72. $this->userManager = $userManager;
  73. $this->emitter = $emitter;
  74. $this->displayName = $displayName;
  75. }
  76. public function getGID() {
  77. return $this->gid;
  78. }
  79. public function getDisplayName() {
  80. if (is_null($this->displayName)) {
  81. return $this->gid;
  82. }
  83. return $this->displayName;
  84. }
  85. /**
  86. * get all users in the group
  87. *
  88. * @return \OC\User\User[]
  89. */
  90. public function getUsers() {
  91. if ($this->usersLoaded) {
  92. return $this->users;
  93. }
  94. $userIds = array();
  95. foreach ($this->backends as $backend) {
  96. $diff = array_diff(
  97. $backend->usersInGroup($this->gid),
  98. $userIds
  99. );
  100. if ($diff) {
  101. $userIds = array_merge($userIds, $diff);
  102. }
  103. }
  104. $this->users = $this->getVerifiedUsers($userIds);
  105. $this->usersLoaded = true;
  106. return $this->users;
  107. }
  108. /**
  109. * check if a user is in the group
  110. *
  111. * @param IUser $user
  112. * @return bool
  113. */
  114. public function inGroup(IUser $user) {
  115. if (isset($this->users[$user->getUID()])) {
  116. return true;
  117. }
  118. foreach ($this->backends as $backend) {
  119. if ($backend->inGroup($user->getUID(), $this->gid)) {
  120. $this->users[$user->getUID()] = $user;
  121. return true;
  122. }
  123. }
  124. return false;
  125. }
  126. /**
  127. * add a user to the group
  128. *
  129. * @param IUser $user
  130. */
  131. public function addUser(IUser $user) {
  132. if ($this->inGroup($user)) {
  133. return;
  134. }
  135. if ($this->emitter) {
  136. $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user));
  137. }
  138. foreach ($this->backends as $backend) {
  139. if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) {
  140. $backend->addToGroup($user->getUID(), $this->gid);
  141. if ($this->users) {
  142. $this->users[$user->getUID()] = $user;
  143. }
  144. if ($this->emitter) {
  145. $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user));
  146. }
  147. return;
  148. }
  149. }
  150. }
  151. /**
  152. * remove a user from the group
  153. *
  154. * @param \OC\User\User $user
  155. */
  156. public function removeUser($user) {
  157. $result = false;
  158. if ($this->emitter) {
  159. $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user));
  160. }
  161. foreach ($this->backends as $backend) {
  162. if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) {
  163. $backend->removeFromGroup($user->getUID(), $this->gid);
  164. $result = true;
  165. }
  166. }
  167. if ($result) {
  168. if ($this->emitter) {
  169. $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user));
  170. }
  171. if ($this->users) {
  172. foreach ($this->users as $index => $groupUser) {
  173. if ($groupUser->getUID() === $user->getUID()) {
  174. unset($this->users[$index]);
  175. return;
  176. }
  177. }
  178. }
  179. }
  180. }
  181. /**
  182. * search for users in the group by userid
  183. *
  184. * @param string $search
  185. * @param int $limit
  186. * @param int $offset
  187. * @return \OC\User\User[]
  188. */
  189. public function searchUsers($search, $limit = null, $offset = null) {
  190. $users = array();
  191. foreach ($this->backends as $backend) {
  192. $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
  193. $users += $this->getVerifiedUsers($userIds);
  194. if (!is_null($limit) and $limit <= 0) {
  195. return $users;
  196. }
  197. }
  198. return $users;
  199. }
  200. /**
  201. * returns the number of users matching the search string
  202. *
  203. * @param string $search
  204. * @return int|bool
  205. */
  206. public function count($search = '') {
  207. $users = false;
  208. foreach ($this->backends as $backend) {
  209. if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) {
  210. if($users === false) {
  211. //we could directly add to a bool variable, but this would
  212. //be ugly
  213. $users = 0;
  214. }
  215. $users += $backend->countUsersInGroup($this->gid, $search);
  216. }
  217. }
  218. return $users;
  219. }
  220. /**
  221. * returns the number of disabled users
  222. *
  223. * @return int|bool
  224. */
  225. public function countDisabled() {
  226. $users = false;
  227. foreach ($this->backends as $backend) {
  228. if($backend instanceOf ICountDisabledInGroup) {
  229. if($users === false) {
  230. //we could directly add to a bool variable, but this would
  231. //be ugly
  232. $users = 0;
  233. }
  234. $users += $backend->countDisabledInGroup($this->gid);
  235. }
  236. }
  237. return $users;
  238. }
  239. /**
  240. * search for users in the group by displayname
  241. *
  242. * @param string $search
  243. * @param int $limit
  244. * @param int $offset
  245. * @return \OC\User\User[]
  246. */
  247. public function searchDisplayName($search, $limit = null, $offset = null) {
  248. $users = array();
  249. foreach ($this->backends as $backend) {
  250. $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
  251. $users = $this->getVerifiedUsers($userIds);
  252. if (!is_null($limit) and $limit <= 0) {
  253. return array_values($users);
  254. }
  255. }
  256. return array_values($users);
  257. }
  258. /**
  259. * delete the group
  260. *
  261. * @return bool
  262. */
  263. public function delete() {
  264. // Prevent users from deleting group admin
  265. if ($this->getGID() === 'admin') {
  266. return false;
  267. }
  268. $result = false;
  269. if ($this->emitter) {
  270. $this->emitter->emit('\OC\Group', 'preDelete', array($this));
  271. }
  272. foreach ($this->backends as $backend) {
  273. if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) {
  274. $result = true;
  275. $backend->deleteGroup($this->gid);
  276. }
  277. }
  278. if ($result and $this->emitter) {
  279. $this->emitter->emit('\OC\Group', 'postDelete', array($this));
  280. }
  281. return $result;
  282. }
  283. /**
  284. * returns all the Users from an array that really exists
  285. * @param string[] $userIds an array containing user IDs
  286. * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value
  287. */
  288. private function getVerifiedUsers($userIds) {
  289. if (!is_array($userIds)) {
  290. return array();
  291. }
  292. $users = array();
  293. foreach ($userIds as $userId) {
  294. $user = $this->userManager->get($userId);
  295. if (!is_null($user)) {
  296. $users[$userId] = $user;
  297. }
  298. }
  299. return $users;
  300. }
  301. /**
  302. * @return bool
  303. * @since 14.0.0
  304. */
  305. public function canRemoveUser() {
  306. foreach ($this->backends as $backend) {
  307. if ($backend->implementsActions(GroupInterface::REMOVE_FROM_GOUP)) {
  308. return true;
  309. }
  310. }
  311. return false;
  312. }
  313. /**
  314. * @return bool
  315. * @since 14.0.0
  316. */
  317. public function canAddUser() {
  318. foreach ($this->backends as $backend) {
  319. if ($backend->implementsActions(GroupInterface::ADD_TO_GROUP)) {
  320. return true;
  321. }
  322. }
  323. return false;
  324. }
  325. }