aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Group/Group.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/Group/Group.php')
-rw-r--r--lib/private/Group/Group.php162
1 files changed, 61 insertions, 101 deletions
diff --git a/lib/private/Group/Group.php b/lib/private/Group/Group.php
index 2ef4d2ee23f..6e42fad8b9f 100644
--- a/lib/private/Group/Group.php
+++ b/lib/private/Group/Group.php
@@ -1,52 +1,36 @@
<?php
+
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
- * @author Bart Visscher <bartv@thisnet.nl>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Johannes Leuker <j.leuker@hosting.de>
- * @author John Molakvoæ <skjnldsv@protonmail.com>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Robin McCorkell <robin@mccorkell.me.uk>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Vincent Petry <vincent@nextcloud.com>
- *
- * @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/>
- *
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OC\Group;
use OC\Hooks\PublicEmitter;
+use OC\User\LazyUser;
+use OCP\EventDispatcher\IEventDispatcher;
use OCP\Group\Backend\ICountDisabledInGroup;
use OCP\Group\Backend\IGetDisplayNameBackend;
use OCP\Group\Backend\IHideFromCollaborationBackend;
use OCP\Group\Backend\INamedBackend;
+use OCP\Group\Backend\ISearchableGroupBackend;
use OCP\Group\Backend\ISetDisplayNameBackend;
+use OCP\Group\Events\BeforeGroupChangedEvent;
+use OCP\Group\Events\BeforeGroupDeletedEvent;
+use OCP\Group\Events\BeforeUserAddedEvent;
+use OCP\Group\Events\BeforeUserRemovedEvent;
+use OCP\Group\Events\GroupChangedEvent;
+use OCP\Group\Events\GroupDeletedEvent;
+use OCP\Group\Events\UserAddedEvent;
+use OCP\Group\Events\UserRemovedEvent;
use OCP\GroupInterface;
use OCP\IGroup;
use OCP\IUser;
use OCP\IUserManager;
-use Symfony\Component\EventDispatcher\EventDispatcherInterface;
-use Symfony\Component\EventDispatcher\GenericEvent;
class Group implements IGroup {
- /** @var null|string */
+ /** @var null|string */
protected $displayName;
/** @var string */
@@ -60,23 +44,14 @@ class Group implements IGroup {
/** @var Backend[] */
private $backends;
- /** @var EventDispatcherInterface */
+ /** @var IEventDispatcher */
private $dispatcher;
- /** @var \OC\User\Manager|IUserManager */
+ /** @var \OC\User\Manager|IUserManager */
private $userManager;
/** @var PublicEmitter */
private $emitter;
-
- /**
- * @param string $gid
- * @param Backend[] $backends
- * @param EventDispatcherInterface $dispatcher
- * @param IUserManager $userManager
- * @param PublicEmitter $emitter
- * @param string $displayName
- */
- public function __construct(string $gid, array $backends, EventDispatcherInterface $dispatcher, IUserManager $userManager, PublicEmitter $emitter = null, ?string $displayName = null) {
+ public function __construct(string $gid, array $backends, IEventDispatcher $dispatcher, IUserManager $userManager, ?PublicEmitter $emitter = null, ?string $displayName = null) {
$this->gid = $gid;
$this->backends = $backends;
$this->dispatcher = $dispatcher;
@@ -85,11 +60,11 @@ class Group implements IGroup {
$this->displayName = $displayName;
}
- public function getGID() {
+ public function getGID(): string {
return $this->gid;
}
- public function getDisplayName() {
+ public function getDisplayName(): string {
if (is_null($this->displayName)) {
foreach ($this->backends as $backend) {
if ($backend instanceof IGetDisplayNameBackend) {
@@ -108,10 +83,12 @@ class Group implements IGroup {
public function setDisplayName(string $displayName): bool {
$displayName = trim($displayName);
if ($displayName !== '') {
+ $this->dispatcher->dispatchTyped(new BeforeGroupChangedEvent($this, 'displayName', $displayName, $this->displayName));
foreach ($this->backends as $backend) {
if (($backend instanceof ISetDisplayNameBackend)
&& $backend->setDisplayName($this->gid, $displayName)) {
$this->displayName = $displayName;
+ $this->dispatcher->dispatchTyped(new GroupChangedEvent($this, 'displayName', $displayName, ''));
return true;
}
}
@@ -124,7 +101,7 @@ class Group implements IGroup {
*
* @return \OC\User\User[]
*/
- public function getUsers() {
+ public function getUsers(): array {
if ($this->usersLoaded) {
return $this->users;
}
@@ -151,7 +128,7 @@ class Group implements IGroup {
* @param IUser $user
* @return bool
*/
- public function inGroup(IUser $user) {
+ public function inGroup(IUser $user): bool {
if (isset($this->users[$user->getUID()])) {
return true;
}
@@ -169,14 +146,12 @@ class Group implements IGroup {
*
* @param IUser $user
*/
- public function addUser(IUser $user) {
+ public function addUser(IUser $user): void {
if ($this->inGroup($user)) {
return;
}
- $this->dispatcher->dispatch(IGroup::class . '::preAddUser', new GenericEvent($this, [
- 'user' => $user,
- ]));
+ $this->dispatcher->dispatchTyped(new BeforeUserAddedEvent($this, $user));
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'preAddUser', [$this, $user]);
@@ -184,13 +159,9 @@ class Group implements IGroup {
foreach ($this->backends as $backend) {
if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) {
$backend->addToGroup($user->getUID(), $this->gid);
- if ($this->users) {
- $this->users[$user->getUID()] = $user;
- }
+ $this->users[$user->getUID()] = $user;
- $this->dispatcher->dispatch(IGroup::class . '::postAddUser', new GenericEvent($this, [
- 'user' => $user,
- ]));
+ $this->dispatcher->dispatchTyped(new UserAddedEvent($this, $user));
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'postAddUser', [$this, $user]);
@@ -202,14 +173,10 @@ class Group implements IGroup {
/**
* remove a user from the group
- *
- * @param \OC\User\User $user
*/
- public function removeUser($user) {
+ public function removeUser(IUser $user): void {
$result = false;
- $this->dispatcher->dispatch(IGroup::class . '::preRemoveUser', new GenericEvent($this, [
- 'user' => $user,
- ]));
+ $this->dispatcher->dispatchTyped(new BeforeUserRemovedEvent($this, $user));
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'preRemoveUser', [$this, $user]);
}
@@ -220,9 +187,7 @@ class Group implements IGroup {
}
}
if ($result) {
- $this->dispatcher->dispatch(IGroup::class . '::postRemoveUser', new GenericEvent($this, [
- 'user' => $user,
- ]));
+ $this->dispatcher->dispatchTyped(new UserRemovedEvent($this, $user));
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'postRemoveUser', [$this, $user]);
}
@@ -238,18 +203,23 @@ class Group implements IGroup {
}
/**
- * search for users in the group by userid
- *
- * @param string $search
- * @param int $limit
- * @param int $offset
- * @return \OC\User\User[]
+ * Search for users in the group by userid or display name
+ * @return IUser[]
*/
- public function searchUsers($search, $limit = null, $offset = null) {
+ public function searchUsers(string $search, ?int $limit = null, ?int $offset = null): array {
$users = [];
foreach ($this->backends as $backend) {
- $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
- $users += $this->getVerifiedUsers($userIds);
+ if ($backend instanceof ISearchableGroupBackend) {
+ $users += $backend->searchInGroup($this->gid, $search, $limit ?? -1, $offset ?? 0);
+ } else {
+ $userIds = $backend->usersInGroup($this->gid, $search, $limit ?? -1, $offset ?? 0);
+ $userManager = \OCP\Server::get(IUserManager::class);
+ foreach ($userIds as $userId) {
+ if (!isset($users[$userId])) {
+ $users[$userId] = new LazyUser($userId, $userManager);
+ }
+ }
+ }
if (!is_null($limit) and $limit <= 0) {
return $users;
}
@@ -263,7 +233,7 @@ class Group implements IGroup {
* @param string $search
* @return int|bool
*/
- public function count($search = '') {
+ public function count($search = ''): int|bool {
$users = false;
foreach ($this->backends as $backend) {
if ($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) {
@@ -283,7 +253,7 @@ class Group implements IGroup {
*
* @return int|bool
*/
- public function countDisabled() {
+ public function countDisabled(): int|bool {
$users = false;
foreach ($this->backends as $backend) {
if ($backend instanceof ICountDisabledInGroup) {
@@ -304,18 +274,11 @@ class Group implements IGroup {
* @param string $search
* @param int $limit
* @param int $offset
- * @return \OC\User\User[]
+ * @return IUser[]
+ * @deprecated 27.0.0 Use searchUsers instead (same implementation)
*/
- public function searchDisplayName($search, $limit = null, $offset = null) {
- $users = [];
- foreach ($this->backends as $backend) {
- $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
- $users = $this->getVerifiedUsers($userIds);
- if (!is_null($limit) and $limit <= 0) {
- return array_values($users);
- }
- }
- return array_values($users);
+ public function searchDisplayName(string $search, ?int $limit = null, ?int $offset = null): array {
+ return $this->searchUsers($search, $limit, $offset);
}
/**
@@ -323,7 +286,7 @@ class Group implements IGroup {
*
* @return string[]
*/
- public function getBackendNames() {
+ public function getBackendNames(): array {
$backends = [];
foreach ($this->backends as $backend) {
if ($backend instanceof INamedBackend) {
@@ -337,18 +300,18 @@ class Group implements IGroup {
}
/**
- * delete the group
+ * Delete the group
*
* @return bool
*/
- public function delete() {
+ public function delete(): bool {
// Prevent users from deleting group admin
if ($this->getGID() === 'admin') {
return false;
}
$result = false;
- $this->dispatcher->dispatch(IGroup::class . '::preDelete', new GenericEvent($this));
+ $this->dispatcher->dispatchTyped(new BeforeGroupDeletedEvent($this));
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'preDelete', [$this]);
}
@@ -358,7 +321,7 @@ class Group implements IGroup {
}
}
if ($result) {
- $this->dispatcher->dispatch(IGroup::class . '::postDelete', new GenericEvent($this));
+ $this->dispatcher->dispatchTyped(new GroupDeletedEvent($this));
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'postDelete', [$this]);
}
@@ -371,10 +334,7 @@ class Group implements IGroup {
* @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
*/
- private function getVerifiedUsers($userIds) {
- if (!is_array($userIds)) {
- return [];
- }
+ private function getVerifiedUsers(array $userIds): array {
$users = [];
foreach ($userIds as $userId) {
$user = $this->userManager->get($userId);
@@ -389,7 +349,7 @@ class Group implements IGroup {
* @return bool
* @since 14.0.0
*/
- public function canRemoveUser() {
+ public function canRemoveUser(): bool {
foreach ($this->backends as $backend) {
if ($backend->implementsActions(GroupInterface::REMOVE_FROM_GOUP)) {
return true;
@@ -402,7 +362,7 @@ class Group implements IGroup {
* @return bool
* @since 14.0.0
*/
- public function canAddUser() {
+ public function canAddUser(): bool {
foreach ($this->backends as $backend) {
if ($backend->implementsActions(GroupInterface::ADD_TO_GROUP)) {
return true;
@@ -417,7 +377,7 @@ class Group implements IGroup {
*/
public function hideFromCollaboration(): bool {
return array_reduce($this->backends, function (bool $hide, GroupInterface $backend) {
- return $hide | ($backend instanceof IHideFromCollaborationBackend && $backend->hideGroup($this->gid));
+ return $hide || ($backend instanceof IHideFromCollaborationBackend && $backend->hideGroup($this->gid));
}, false);
}
}