2013-07-09 22:06:22 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 16:13:36 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 14:02:16 +00:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2016-07-21 15:07:57 +00:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2018-05-16 10:32:56 +00:00
|
|
|
* @author John Molakvoæ <skjnldsv@protonmail.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2013-07-09 22:06:22 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2013-07-09 22:06:22 +00:00
|
|
|
namespace OC\Group;
|
|
|
|
|
2019-05-23 14:58:58 +00:00
|
|
|
use OCP\Group\Backend\IGetDisplayNameBackend;
|
2019-02-22 15:36:25 +00:00
|
|
|
use OCP\Group\Backend\IHideFromCollaborationBackend;
|
2019-02-22 12:07:26 +00:00
|
|
|
use OC\Hooks\PublicEmitter;
|
2019-09-20 09:33:02 +00:00
|
|
|
use OCP\Group\Backend\ISetDisplayNameBackend;
|
2018-06-19 14:23:02 +00:00
|
|
|
use OCP\GroupInterface;
|
2014-07-16 12:25:31 +00:00
|
|
|
use OCP\IGroup;
|
2017-07-19 13:44:38 +00:00
|
|
|
use OCP\IUser;
|
2018-05-16 10:32:56 +00:00
|
|
|
use OCP\Group\Backend\ICountDisabledInGroup;
|
2019-02-22 12:07:26 +00:00
|
|
|
use OCP\IUserManager;
|
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent;
|
2014-07-16 12:25:31 +00:00
|
|
|
|
|
|
|
class Group implements IGroup {
|
2016-12-22 17:53:29 +00:00
|
|
|
/** @var null|string */
|
|
|
|
protected $displayName;
|
|
|
|
|
2019-02-22 12:07:26 +00:00
|
|
|
/** @var string */
|
2013-07-24 15:54:31 +00:00
|
|
|
private $gid;
|
2013-07-09 22:06:22 +00:00
|
|
|
|
2019-02-22 12:07:26 +00:00
|
|
|
/** @var \OC\User\User[] */
|
2013-12-11 14:47:36 +00:00
|
|
|
private $users = array();
|
|
|
|
|
2019-02-22 12:07:26 +00:00
|
|
|
/** @var bool */
|
2013-12-11 14:47:36 +00:00
|
|
|
private $usersLoaded;
|
2013-07-09 22:06:22 +00:00
|
|
|
|
2019-02-22 12:07:26 +00:00
|
|
|
/** @var Backend[] */
|
2013-07-09 22:06:22 +00:00
|
|
|
private $backends;
|
2019-02-22 12:07:26 +00:00
|
|
|
/** @var EventDispatcherInterface */
|
|
|
|
private $dispatcher;
|
|
|
|
/** @var \OC\User\Manager|IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
/** @var PublicEmitter */
|
2013-07-09 22:06:22 +00:00
|
|
|
private $emitter;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-07-10 15:52:34 +00:00
|
|
|
* @param string $gid
|
2019-02-22 12:07:26 +00:00
|
|
|
* @param Backend[] $backends
|
|
|
|
* @param EventDispatcherInterface $dispatcher
|
|
|
|
* @param IUserManager $userManager
|
|
|
|
* @param PublicEmitter $emitter
|
2016-11-30 19:21:44 +00:00
|
|
|
* @param string $displayName
|
2013-07-09 22:06:22 +00:00
|
|
|
*/
|
2019-02-22 12:07:26 +00:00
|
|
|
public function __construct(string $gid, array $backends, EventDispatcherInterface $dispatcher, IUserManager $userManager, PublicEmitter $emitter = null, ?string $displayName = null) {
|
2013-07-10 15:52:34 +00:00
|
|
|
$this->gid = $gid;
|
2013-07-09 22:06:22 +00:00
|
|
|
$this->backends = $backends;
|
2019-02-22 12:07:26 +00:00
|
|
|
$this->dispatcher = $dispatcher;
|
2013-07-09 22:06:22 +00:00
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->emitter = $emitter;
|
2016-11-30 19:21:44 +00:00
|
|
|
$this->displayName = $displayName;
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getGID() {
|
2013-07-10 15:52:34 +00:00
|
|
|
return $this->gid;
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|
|
|
|
|
2016-11-30 19:21:44 +00:00
|
|
|
public function getDisplayName() {
|
|
|
|
if (is_null($this->displayName)) {
|
2019-05-23 14:58:58 +00:00
|
|
|
foreach ($this->backends as $backend) {
|
|
|
|
if ($backend instanceof IGetDisplayNameBackend) {
|
|
|
|
$displayName = $backend->getDisplayName($this->gid);
|
|
|
|
if (trim($displayName) !== '') {
|
|
|
|
$this->displayName = $displayName;
|
|
|
|
return $this->displayName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-30 19:21:44 +00:00
|
|
|
return $this->gid;
|
|
|
|
}
|
|
|
|
return $this->displayName;
|
|
|
|
}
|
|
|
|
|
2019-09-20 09:33:02 +00:00
|
|
|
public function setDisplayName(string $displayName): bool {
|
|
|
|
$displayName = trim($displayName);
|
|
|
|
if ($displayName !== '') {
|
|
|
|
foreach ($this->backends as $backend) {
|
|
|
|
if (($backend instanceof ISetDisplayNameBackend)
|
|
|
|
&& $backend->setDisplayName($this->gid, $displayName)) {
|
|
|
|
$this->displayName = $displayName;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-09 22:06:22 +00:00
|
|
|
/**
|
|
|
|
* get all users in the group
|
|
|
|
*
|
|
|
|
* @return \OC\User\User[]
|
|
|
|
*/
|
|
|
|
public function getUsers() {
|
2013-12-11 14:47:36 +00:00
|
|
|
if ($this->usersLoaded) {
|
2013-07-09 22:06:22 +00:00
|
|
|
return $this->users;
|
|
|
|
}
|
|
|
|
|
|
|
|
$userIds = array();
|
|
|
|
foreach ($this->backends as $backend) {
|
|
|
|
$diff = array_diff(
|
2013-07-10 15:52:34 +00:00
|
|
|
$backend->usersInGroup($this->gid),
|
2013-07-09 22:06:22 +00:00
|
|
|
$userIds
|
|
|
|
);
|
|
|
|
if ($diff) {
|
|
|
|
$userIds = array_merge($userIds, $diff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-26 15:46:31 +00:00
|
|
|
$this->users = $this->getVerifiedUsers($userIds);
|
2013-12-11 14:47:36 +00:00
|
|
|
$this->usersLoaded = true;
|
2013-08-26 15:46:31 +00:00
|
|
|
return $this->users;
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if a user is in the group
|
|
|
|
*
|
2017-07-19 13:44:38 +00:00
|
|
|
* @param IUser $user
|
2013-07-09 22:06:22 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-07-19 13:44:38 +00:00
|
|
|
public function inGroup(IUser $user) {
|
2013-12-11 15:25:41 +00:00
|
|
|
if (isset($this->users[$user->getUID()])) {
|
|
|
|
return true;
|
2013-12-11 14:47:36 +00:00
|
|
|
}
|
2013-07-09 22:06:22 +00:00
|
|
|
foreach ($this->backends as $backend) {
|
2013-07-10 15:52:34 +00:00
|
|
|
if ($backend->inGroup($user->getUID(), $this->gid)) {
|
2013-12-11 15:25:41 +00:00
|
|
|
$this->users[$user->getUID()] = $user;
|
2013-07-09 22:06:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add a user to the group
|
|
|
|
*
|
2017-07-19 13:44:38 +00:00
|
|
|
* @param IUser $user
|
2013-07-09 22:06:22 +00:00
|
|
|
*/
|
2017-07-19 13:44:38 +00:00
|
|
|
public function addUser(IUser $user) {
|
2013-07-09 22:06:22 +00:00
|
|
|
if ($this->inGroup($user)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-22 12:07:26 +00:00
|
|
|
$this->dispatcher->dispatch(IGroup::class . '::preAddUser', new GenericEvent($this, [
|
|
|
|
'user' => $user,
|
|
|
|
]));
|
|
|
|
|
2013-07-09 22:06:22 +00:00
|
|
|
if ($this->emitter) {
|
|
|
|
$this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user));
|
|
|
|
}
|
|
|
|
foreach ($this->backends as $backend) {
|
2016-05-17 14:06:44 +00:00
|
|
|
if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) {
|
2013-07-10 15:52:34 +00:00
|
|
|
$backend->addToGroup($user->getUID(), $this->gid);
|
2013-07-09 22:06:22 +00:00
|
|
|
if ($this->users) {
|
2013-08-26 15:46:31 +00:00
|
|
|
$this->users[$user->getUID()] = $user;
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|
2019-02-22 12:07:26 +00:00
|
|
|
|
|
|
|
$this->dispatcher->dispatch(IGroup::class . '::postAddUser', new GenericEvent($this, [
|
|
|
|
'user' => $user,
|
|
|
|
]));
|
|
|
|
|
2013-07-09 22:06:22 +00:00
|
|
|
if ($this->emitter) {
|
2013-07-09 23:59:59 +00:00
|
|
|
$this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user));
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove a user from the group
|
|
|
|
*
|
|
|
|
* @param \OC\User\User $user
|
|
|
|
*/
|
|
|
|
public function removeUser($user) {
|
|
|
|
$result = false;
|
2019-02-22 12:07:26 +00:00
|
|
|
$this->dispatcher->dispatch(IGroup::class . '::preRemoveUser', new GenericEvent($this, [
|
|
|
|
'user' => $user,
|
|
|
|
]));
|
2013-07-09 22:06:22 +00:00
|
|
|
if ($this->emitter) {
|
|
|
|
$this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user));
|
|
|
|
}
|
|
|
|
foreach ($this->backends as $backend) {
|
2016-05-17 14:06:44 +00:00
|
|
|
if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) {
|
2013-07-10 15:52:34 +00:00
|
|
|
$backend->removeFromGroup($user->getUID(), $this->gid);
|
2013-07-09 22:06:22 +00:00
|
|
|
$result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($result) {
|
2019-02-22 12:07:26 +00:00
|
|
|
$this->dispatcher->dispatch(IGroup::class . '::postRemoveUser', new GenericEvent($this, [
|
|
|
|
'user' => $user,
|
|
|
|
]));
|
2013-07-09 22:06:22 +00:00
|
|
|
if ($this->emitter) {
|
2013-07-09 23:59:59 +00:00
|
|
|
$this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user));
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|
|
|
|
if ($this->users) {
|
|
|
|
foreach ($this->users as $index => $groupUser) {
|
|
|
|
if ($groupUser->getUID() === $user->getUID()) {
|
|
|
|
unset($this->users[$index]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* search for users in the group by userid
|
|
|
|
*
|
|
|
|
* @param string $search
|
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
|
|
|
* @return \OC\User\User[]
|
|
|
|
*/
|
2013-07-10 00:01:34 +00:00
|
|
|
public function searchUsers($search, $limit = null, $offset = null) {
|
2013-07-09 22:06:22 +00:00
|
|
|
$users = array();
|
|
|
|
foreach ($this->backends as $backend) {
|
2013-07-10 15:52:34 +00:00
|
|
|
$userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
|
2013-08-26 15:46:31 +00:00
|
|
|
$users += $this->getVerifiedUsers($userIds);
|
2013-07-09 22:06:22 +00:00
|
|
|
if (!is_null($limit) and $limit <= 0) {
|
2018-06-15 20:52:30 +00:00
|
|
|
return $users;
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-15 20:52:30 +00:00
|
|
|
return $users;
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|
|
|
|
|
2014-04-04 16:56:14 +00:00
|
|
|
/**
|
|
|
|
* returns the number of users matching the search string
|
|
|
|
*
|
|
|
|
* @param string $search
|
2014-05-11 17:28:45 +00:00
|
|
|
* @return int|bool
|
2014-04-04 16:56:14 +00:00
|
|
|
*/
|
2014-05-13 14:49:25 +00:00
|
|
|
public function count($search = '') {
|
2014-04-04 16:56:14 +00:00
|
|
|
$users = false;
|
|
|
|
foreach ($this->backends as $backend) {
|
2016-05-17 14:06:44 +00:00
|
|
|
if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) {
|
2014-04-04 16:56:14 +00:00
|
|
|
if($users === false) {
|
|
|
|
//we could directly add to a bool variable, but this would
|
|
|
|
//be ugly
|
|
|
|
$users = 0;
|
|
|
|
}
|
|
|
|
$users += $backend->countUsersInGroup($this->gid, $search);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
|
2018-05-16 10:32:56 +00:00
|
|
|
/**
|
|
|
|
* returns the number of disabled users
|
|
|
|
*
|
|
|
|
* @return int|bool
|
|
|
|
*/
|
|
|
|
public function countDisabled() {
|
|
|
|
$users = false;
|
|
|
|
foreach ($this->backends as $backend) {
|
|
|
|
if($backend instanceOf ICountDisabledInGroup) {
|
|
|
|
if($users === false) {
|
|
|
|
//we could directly add to a bool variable, but this would
|
|
|
|
//be ugly
|
|
|
|
$users = 0;
|
|
|
|
}
|
|
|
|
$users += $backend->countDisabledInGroup($this->gid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
|
2013-07-09 22:06:22 +00:00
|
|
|
/**
|
|
|
|
* search for users in the group by displayname
|
|
|
|
*
|
|
|
|
* @param string $search
|
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
|
|
|
* @return \OC\User\User[]
|
|
|
|
*/
|
2013-07-10 00:01:34 +00:00
|
|
|
public function searchDisplayName($search, $limit = null, $offset = null) {
|
2013-12-11 14:47:36 +00:00
|
|
|
$users = array();
|
2013-07-09 22:06:22 +00:00
|
|
|
foreach ($this->backends as $backend) {
|
2014-03-14 12:51:17 +00:00
|
|
|
$userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
|
2013-08-26 15:46:31 +00:00
|
|
|
$users = $this->getVerifiedUsers($userIds);
|
2013-07-09 22:06:22 +00:00
|
|
|
if (!is_null($limit) and $limit <= 0) {
|
|
|
|
return array_values($users);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array_values($users);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* delete the group
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function delete() {
|
2014-12-04 13:15:55 +00:00
|
|
|
// Prevent users from deleting group admin
|
|
|
|
if ($this->getGID() === 'admin') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-09 22:06:22 +00:00
|
|
|
$result = false;
|
2019-02-22 12:07:26 +00:00
|
|
|
$this->dispatcher->dispatch(IGroup::class . '::preDelete', new GenericEvent($this));
|
2013-07-09 22:06:22 +00:00
|
|
|
if ($this->emitter) {
|
|
|
|
$this->emitter->emit('\OC\Group', 'preDelete', array($this));
|
|
|
|
}
|
|
|
|
foreach ($this->backends as $backend) {
|
2016-05-17 14:06:44 +00:00
|
|
|
if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) {
|
2013-07-09 22:06:22 +00:00
|
|
|
$result = true;
|
2013-07-10 15:52:34 +00:00
|
|
|
$backend->deleteGroup($this->gid);
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-22 12:07:26 +00:00
|
|
|
if ($result) {
|
|
|
|
$this->dispatcher->dispatch(IGroup::class . '::postDelete', new GenericEvent($this));
|
|
|
|
if ($this->emitter) {
|
|
|
|
$this->emitter->emit('\OC\Group', 'postDelete', array($this));
|
|
|
|
}
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2013-08-26 15:46:31 +00:00
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* returns all the Users from an array that really exists
|
2013-12-11 14:47:36 +00:00
|
|
|
* @param string[] $userIds an array containing user IDs
|
|
|
|
* @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value
|
2013-08-26 15:46:31 +00:00
|
|
|
*/
|
|
|
|
private function getVerifiedUsers($userIds) {
|
2013-12-11 14:47:36 +00:00
|
|
|
if (!is_array($userIds)) {
|
2013-08-26 15:46:31 +00:00
|
|
|
return array();
|
|
|
|
}
|
|
|
|
$users = array();
|
|
|
|
foreach ($userIds as $userId) {
|
|
|
|
$user = $this->userManager->get($userId);
|
2013-12-11 14:47:36 +00:00
|
|
|
if (!is_null($user)) {
|
2013-08-26 15:46:31 +00:00
|
|
|
$users[$userId] = $user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $users;
|
|
|
|
}
|
2018-06-19 14:23:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
|
|
|
public function canRemoveUser() {
|
|
|
|
foreach ($this->backends as $backend) {
|
|
|
|
if ($backend->implementsActions(GroupInterface::REMOVE_FROM_GOUP)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
|
|
|
public function canAddUser() {
|
|
|
|
foreach ($this->backends as $backend) {
|
|
|
|
if ($backend->implementsActions(GroupInterface::ADD_TO_GROUP)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-22 15:36:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
* @since 16.0.0
|
|
|
|
*/
|
|
|
|
public function hideFromCollaboration(): bool {
|
|
|
|
return array_reduce($this->backends, function(bool $hide, GroupInterface $backend) {
|
|
|
|
return $hide | ($backend instanceof IHideFromCollaborationBackend && $backend->hideGroup($this->gid));
|
|
|
|
}, false);
|
|
|
|
}
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|