2013-07-09 22:06:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Group;
|
|
|
|
|
|
|
|
class Group {
|
|
|
|
/**
|
|
|
|
* @var string $id
|
|
|
|
*/
|
2013-07-24 15:54:31 +00:00
|
|
|
private $gid;
|
2013-07-09 22:06:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\User\User[] $users
|
|
|
|
*/
|
2013-12-11 14:47:36 +00:00
|
|
|
private $users = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool $usersLoaded
|
|
|
|
*/
|
|
|
|
private $usersLoaded;
|
2013-07-09 22:06:22 +00:00
|
|
|
|
|
|
|
/**
|
2014-05-11 17:28:45 +00:00
|
|
|
* @var \OC_Group_Backend[]|\OC_Group_Database[] $backend
|
2013-07-09 22:06:22 +00:00
|
|
|
*/
|
|
|
|
private $backends;
|
|
|
|
|
|
|
|
/**
|
2013-12-12 12:59:00 +00:00
|
|
|
* @var \OC\Hooks\PublicEmitter $emitter
|
2013-07-09 22:06:22 +00:00
|
|
|
*/
|
|
|
|
private $emitter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\User\Manager $userManager
|
|
|
|
*/
|
|
|
|
private $userManager;
|
|
|
|
|
|
|
|
/**
|
2013-07-10 15:52:34 +00:00
|
|
|
* @param string $gid
|
2013-07-09 22:06:22 +00:00
|
|
|
* @param \OC_Group_Backend[] $backends
|
|
|
|
* @param \OC\User\Manager $userManager
|
|
|
|
* @param \OC\Hooks\PublicEmitter $emitter
|
|
|
|
*/
|
2013-07-10 15:52:34 +00:00
|
|
|
public function __construct($gid, $backends, $userManager, $emitter = null) {
|
|
|
|
$this->gid = $gid;
|
2013-07-09 22:06:22 +00:00
|
|
|
$this->backends = $backends;
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->emitter = $emitter;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getGID() {
|
2013-07-10 15:52:34 +00:00
|
|
|
return $this->gid;
|
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
|
|
|
|
*
|
|
|
|
* @param \OC\User\User $user
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function inGroup($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
|
|
|
|
*
|
|
|
|
* @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 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
|
|
|
}
|
|
|
|
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;
|
|
|
|
if ($this->emitter) {
|
|
|
|
$this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user));
|
|
|
|
}
|
|
|
|
foreach ($this->backends as $backend) {
|
2013-07-10 15:52:34 +00:00
|
|
|
if ($backend->implementsActions(OC_GROUP_BACKEND_REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) {
|
|
|
|
$backend->removeFromGroup($user->getUID(), $this->gid);
|
2013-07-09 22:06:22 +00:00
|
|
|
$result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($result) {
|
|
|
|
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) {
|
|
|
|
return array_values($users);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array_values($users);
|
|
|
|
}
|
|
|
|
|
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) {
|
2014-04-08 10:31:11 +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;
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
$result = false;
|
|
|
|
if ($this->emitter) {
|
|
|
|
$this->emitter->emit('\OC\Group', 'preDelete', array($this));
|
|
|
|
}
|
|
|
|
foreach ($this->backends as $backend) {
|
2013-07-18 10:15:34 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($result and $this->emitter) {
|
2013-07-09 23:59:59 +00:00
|
|
|
$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;
|
|
|
|
}
|
2013-07-09 22:06:22 +00:00
|
|
|
}
|