nextcloud/lib/private/Group/Group.php

303 lines
7.1 KiB
PHP
Raw Normal View History

2013-07-10 02:06:22 +04:00
<?php
/**
2016-07-21 18:07:57 +03:00
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
2016-05-26 20:56:05 +03:00
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
2015-03-26 13:44:34 +03:00
* @author Bart Visscher <bartv@thisnet.nl>
2016-05-26 20:56:05 +03:00
* @author Lukas Reschke <lukas@statuscode.ch>
2015-03-26 13:44:34 +03:00
* @author Morris Jobke <hey@morrisjobke.de>
2016-07-21 19:13:36 +03:00
* @author Robin Appelman <robin@icewind.nl>
2016-01-12 17:02:16 +03:00
* @author Robin McCorkell <robin@mccorkell.me.uk>
2016-07-21 18:07:57 +03:00
* @author Roeland Jago Douma <roeland@famdouma.nl>
2015-03-26 13:44:34 +03: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-10 02:06:22 +04:00
*/
2013-07-10 02:06:22 +04:00
namespace OC\Group;
2014-07-16 16:25:31 +04:00
use OCP\IGroup;
class Group implements IGroup {
/** @var null|string */
protected $displayName;
2013-07-10 02:06:22 +04:00
/**
* @var string $id
*/
2013-07-24 19:54:31 +04:00
private $gid;
2013-07-10 02:06:22 +04:00
/**
* @var \OC\User\User[] $users
*/
2013-12-11 18:47:36 +04:00
private $users = array();
/**
* @var bool $usersLoaded
*/
private $usersLoaded;
2013-07-10 02:06:22 +04:00
/**
* @var \OC\Group\Backend[]|\OC\Group\Database[] $backend
2013-07-10 02:06:22 +04:00
*/
private $backends;
/**
2013-12-12 16:59:00 +04:00
* @var \OC\Hooks\PublicEmitter $emitter
2013-07-10 02:06:22 +04:00
*/
private $emitter;
/**
* @var \OC\User\Manager $userManager
*/
private $userManager;
/**
2013-07-10 19:52:34 +04:00
* @param string $gid
* @param \OC\Group\Backend[] $backends
2013-07-10 02:06:22 +04:00
* @param \OC\User\Manager $userManager
* @param \OC\Hooks\PublicEmitter $emitter
2016-11-30 22:21:44 +03:00
* @param string $displayName
2013-07-10 02:06:22 +04:00
*/
2016-11-30 22:21:44 +03:00
public function __construct($gid, $backends, $userManager, $emitter = null, $displayName = null) {
2013-07-10 19:52:34 +04:00
$this->gid = $gid;
2013-07-10 02:06:22 +04:00
$this->backends = $backends;
$this->userManager = $userManager;
$this->emitter = $emitter;
2016-11-30 22:21:44 +03:00
$this->displayName = $displayName;
2013-07-10 02:06:22 +04:00
}
public function getGID() {
2013-07-10 19:52:34 +04:00
return $this->gid;
2013-07-10 02:06:22 +04:00
}
2016-11-30 22:21:44 +03:00
public function getDisplayName() {
if (is_null($this->displayName)) {
return $this->gid;
}
return $this->displayName;
}
2013-07-10 02:06:22 +04:00
/**
* get all users in the group
*
* @return \OC\User\User[]
*/
public function getUsers() {
2013-12-11 18:47:36 +04:00
if ($this->usersLoaded) {
2013-07-10 02:06:22 +04:00
return $this->users;
}
$userIds = array();
foreach ($this->backends as $backend) {
$diff = array_diff(
2013-07-10 19:52:34 +04:00
$backend->usersInGroup($this->gid),
2013-07-10 02:06:22 +04:00
$userIds
);
if ($diff) {
$userIds = array_merge($userIds, $diff);
}
}
$this->users = $this->getVerifiedUsers($userIds);
2013-12-11 18:47:36 +04:00
$this->usersLoaded = true;
return $this->users;
2013-07-10 02:06:22 +04:00
}
/**
* check if a user is in the group
*
* @param \OC\User\User $user
* @return bool
*/
public function inGroup($user) {
2013-12-11 19:25:41 +04:00
if (isset($this->users[$user->getUID()])) {
return true;
2013-12-11 18:47:36 +04:00
}
2013-07-10 02:06:22 +04:00
foreach ($this->backends as $backend) {
2013-07-10 19:52:34 +04:00
if ($backend->inGroup($user->getUID(), $this->gid)) {
2013-12-11 19:25:41 +04:00
$this->users[$user->getUID()] = $user;
2013-07-10 02:06:22 +04:00
return true;
}
}
return false;
}
/**
* add a user to the group
*
* @param \OC\User\User $user
*/
public function addUser($user) {
if ($this->inGroup($user)) {
return;
}
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user));
}
foreach ($this->backends as $backend) {
if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) {
2013-07-10 19:52:34 +04:00
$backend->addToGroup($user->getUID(), $this->gid);
2013-07-10 02:06:22 +04:00
if ($this->users) {
$this->users[$user->getUID()] = $user;
2013-07-10 02:06:22 +04:00
}
if ($this->emitter) {
2013-07-10 03:59:59 +04:00
$this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user));
2013-07-10 02:06:22 +04:00
}
return;
}
}
}
/**
* remove a user from the group
*
* @param \OC\User\User $user
*/
public function removeUser($user) {
$result = false;
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user));
}
foreach ($this->backends as $backend) {
if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) {
2013-07-10 19:52:34 +04:00
$backend->removeFromGroup($user->getUID(), $this->gid);
2013-07-10 02:06:22 +04:00
$result = true;
}
}
if ($result) {
if ($this->emitter) {
2013-07-10 03:59:59 +04:00
$this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user));
2013-07-10 02:06:22 +04: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 04:01:34 +04:00
public function searchUsers($search, $limit = null, $offset = null) {
2013-07-10 02:06:22 +04:00
$users = array();
foreach ($this->backends as $backend) {
2013-07-10 19:52:34 +04:00
$userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
$users += $this->getVerifiedUsers($userIds);
2013-07-10 02:06:22 +04:00
if (!is_null($limit) and $limit <= 0) {
return array_values($users);
}
}
return array_values($users);
}
/**
* returns the number of users matching the search string
*
* @param string $search
2014-05-11 21:28:45 +04:00
* @return int|bool
*/
public function count($search = '') {
$users = false;
foreach ($this->backends as $backend) {
if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) {
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;
}
2013-07-10 02:06:22 +04: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 04:01:34 +04:00
public function searchDisplayName($search, $limit = null, $offset = null) {
2013-12-11 18:47:36 +04:00
$users = array();
2013-07-10 02:06:22 +04:00
foreach ($this->backends as $backend) {
$userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
$users = $this->getVerifiedUsers($userIds);
2013-07-10 02:06:22 +04:00
if (!is_null($limit) and $limit <= 0) {
return array_values($users);
}
}
return array_values($users);
}
/**
* delete the group
*
* @return bool
*/
public function delete() {
// Prevent users from deleting group admin
if ($this->getGID() === 'admin') {
return false;
}
2013-07-10 02:06:22 +04:00
$result = false;
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'preDelete', array($this));
}
foreach ($this->backends as $backend) {
if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) {
2013-07-10 02:06:22 +04:00
$result = true;
2013-07-10 19:52:34 +04:00
$backend->deleteGroup($this->gid);
2013-07-10 02:06:22 +04:00
}
}
if ($result and $this->emitter) {
2013-07-10 03:59:59 +04:00
$this->emitter->emit('\OC\Group', 'postDelete', array($this));
2013-07-10 02:06:22 +04:00
}
return $result;
}
/**
* returns all the Users from an array that really exists
2013-12-11 18:47:36 +04: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
*/
private function getVerifiedUsers($userIds) {
2013-12-11 18:47:36 +04:00
if (!is_array($userIds)) {
return array();
}
$users = array();
foreach ($userIds as $userId) {
$user = $this->userManager->get($userId);
2013-12-11 18:47:36 +04:00
if (!is_null($user)) {
$users[$userId] = $user;
}
}
return $users;
}
2013-07-10 02:06:22 +04:00
}