nextcloud/lib/group/group.php

239 lines
5.2 KiB
PHP
Raw Normal View History

2013-07-10 02:06:22 +04: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-10 19:52:34 +04:00
private $fid;
2013-07-10 02:06:22 +04:00
/**
* @var \OC\User\User[] $users
*/
private $users;
/**
* @var \OC_Group_Backend[] | \OC_Group_Database[] $backend
*/
private $backends;
/**
* @var \OC\Hooks\PublicEmitter $emitter;
*/
private $emitter;
/**
* @var \OC\User\Manager $userManager
*/
private $userManager;
/**
2013-07-10 19:52:34 +04:00
* @param string $gid
2013-07-10 02:06:22 +04:00
* @param \OC_Group_Backend[] $backends
* @param \OC\User\Manager $userManager
* @param \OC\Hooks\PublicEmitter $emitter
*/
2013-07-10 19:52:34 +04:00
public function __construct($gid, $backends, $userManager, $emitter = null) {
$this->gid = $gid;
2013-07-10 02:06:22 +04:00
$this->backends = $backends;
$this->userManager = $userManager;
$this->emitter = $emitter;
}
public function getGID() {
2013-07-10 19:52:34 +04:00
return $this->gid;
2013-07-10 02:06:22 +04:00
}
/**
* get all users in the group
*
* @return \OC\User\User[]
*/
public function getUsers() {
if ($this->users) {
return $this->users;
}
$users = array();
$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);
}
}
foreach ($userIds as $userId) {
$users[] = $this->userManager->get($userId);
}
$this->users = $users;
return $users;
}
/**
* check if a user is in the group
*
* @param \OC\User\User $user
* @return bool
*/
public function inGroup($user) {
foreach ($this->backends as $backend) {
2013-07-10 19:52:34 +04:00
if ($backend->inGroup($user->getUID(), $this->gid)) {
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;
}
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) {
2013-07-10 19:52:34 +04: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-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);
2013-07-10 02:06:22 +04:00
if (!is_null($limit)) {
$limit -= count($userIds);
}
if (!is_null($offset)) {
$offset -= count($userIds);
}
foreach ($userIds as $userId) {
$users[$userId] = $this->userManager->get($userId);
}
if (!is_null($limit) and $limit <= 0) {
return array_values($users);
}
}
return array_values($users);
}
/**
* 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-07-10 02:06:22 +04:00
$users = array();
foreach ($this->backends as $backend) {
if ($backend->implementsActions(OC_GROUP_BACKEND_GET_DISPLAYNAME)) {
2013-07-10 19:52:34 +04:00
$userIds = array_keys($backend->displayNamesInGroup($this->gid, $search, $limit, $offset));
2013-07-10 02:06:22 +04:00
} else {
2013-07-10 19:52:34 +04:00
$userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
2013-07-10 02:06:22 +04:00
}
if (!is_null($limit)) {
$limit -= count($userIds);
}
if (!is_null($offset)) {
$offset -= count($userIds);
}
foreach ($userIds as $userId) {
$users[$userId] = $this->userManager->get($userId);
}
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) {
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;
}
}