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;
|
|
|
|
|
|
|
|
use OC\Hooks\PublicEmitter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Manager
|
|
|
|
*
|
|
|
|
* Hooks available in scope \OC\Group:
|
|
|
|
* - preAddUser(\OC\Group\Group $group, \OC\User\User $user)
|
2013-07-10 03:59:59 +04:00
|
|
|
* - postAddUser(\OC\Group\Group $group, \OC\User\User $user)
|
2013-07-10 02:06:22 +04:00
|
|
|
* - preRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
|
2013-07-10 03:59:59 +04:00
|
|
|
* - postRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
|
2013-07-10 02:06:22 +04:00
|
|
|
* - preDelete(\OC\Group\Group $group)
|
2013-07-10 03:59:59 +04:00
|
|
|
* - postDelete(\OC\Group\Group $group)
|
2013-07-10 02:06:22 +04:00
|
|
|
* - preCreate(string $groupId)
|
2013-07-10 03:59:59 +04:00
|
|
|
* - postCreate(\OC\Group\Group $group)
|
2013-07-10 02:06:22 +04:00
|
|
|
*
|
|
|
|
* @package OC\Group
|
|
|
|
*/
|
|
|
|
class Manager extends PublicEmitter {
|
|
|
|
/**
|
|
|
|
* @var \OC_Group_Backend[] | \OC_Group_Database[] $backends
|
|
|
|
*/
|
|
|
|
private $backends = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\User\Manager $userManager
|
|
|
|
*/
|
|
|
|
private $userManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\Group\Group[]
|
|
|
|
*/
|
2014-02-22 01:52:48 +04:00
|
|
|
private $cachedGroups = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\Group\Group[]
|
|
|
|
*/
|
|
|
|
private $cachedUserGroups = array();
|
2013-07-10 02:06:22 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \OC\User\Manager $userManager
|
|
|
|
*/
|
|
|
|
public function __construct($userManager) {
|
|
|
|
$this->userManager = $userManager;
|
2013-07-10 04:09:42 +04:00
|
|
|
$cache = & $this->cachedGroups;
|
2013-07-10 03:59:59 +04:00
|
|
|
$this->listen('\OC\Group', 'postDelete', function ($group) use (&$cache) {
|
2013-07-10 02:06:22 +04:00
|
|
|
/**
|
|
|
|
* @var \OC\Group\Group $group
|
|
|
|
*/
|
2013-07-10 03:55:38 +04:00
|
|
|
unset($cache[$group->getGID()]);
|
2013-07-10 02:06:22 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \OC_Group_Backend $backend
|
|
|
|
*/
|
|
|
|
public function addBackend($backend) {
|
|
|
|
$this->backends[] = $backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function clearBackends() {
|
|
|
|
$this->backends = array();
|
|
|
|
$this->cachedGroups = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $gid
|
|
|
|
* @return \OC\Group\Group
|
|
|
|
*/
|
|
|
|
public function get($gid) {
|
|
|
|
if (isset($this->cachedGroups[$gid])) {
|
|
|
|
return $this->cachedGroups[$gid];
|
|
|
|
}
|
2014-02-14 17:25:45 +04:00
|
|
|
return $this->getGroupObject($gid);
|
2013-07-10 02:06:22 +04:00
|
|
|
}
|
|
|
|
|
2013-07-10 04:09:42 +04:00
|
|
|
protected function getGroupObject($gid) {
|
2013-07-18 14:15:34 +04:00
|
|
|
$backends = array();
|
|
|
|
foreach ($this->backends as $backend) {
|
|
|
|
if ($backend->groupExists($gid)) {
|
|
|
|
$backends[] = $backend;
|
|
|
|
}
|
|
|
|
}
|
2014-02-14 17:25:45 +04:00
|
|
|
if (count($backends) === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-07-18 14:15:34 +04:00
|
|
|
$this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this);
|
2013-07-10 02:06:22 +04:00
|
|
|
return $this->cachedGroups[$gid];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $gid
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-07-10 04:17:24 +04:00
|
|
|
public function groupExists($gid) {
|
2013-07-10 02:06:22 +04:00
|
|
|
return !is_null($this->get($gid));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $gid
|
|
|
|
* @return \OC\Group\Group
|
|
|
|
*/
|
2013-07-10 04:17:24 +04:00
|
|
|
public function createGroup($gid) {
|
2013-07-10 02:06:22 +04:00
|
|
|
if (!$gid) {
|
|
|
|
return false;
|
2014-02-14 18:07:08 +04:00
|
|
|
} else if ($group = $this->get($gid)) {
|
|
|
|
return $group;
|
2013-07-10 02:06:22 +04:00
|
|
|
} else {
|
|
|
|
$this->emit('\OC\Group', 'preCreate', array($gid));
|
|
|
|
foreach ($this->backends as $backend) {
|
|
|
|
if ($backend->implementsActions(OC_GROUP_BACKEND_CREATE_GROUP)) {
|
|
|
|
$backend->createGroup($gid);
|
2013-07-10 04:09:42 +04:00
|
|
|
$group = $this->getGroupObject($gid);
|
2013-07-10 03:59:59 +04:00
|
|
|
$this->emit('\OC\Group', 'postCreate', array($group));
|
2013-07-10 02:06:22 +04:00
|
|
|
return $group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $search
|
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
|
|
|
* @return \OC\Group\Group[]
|
|
|
|
*/
|
2013-07-10 04:17:24 +04:00
|
|
|
public function search($search, $limit = null, $offset = null) {
|
2013-07-10 02:06:22 +04:00
|
|
|
$groups = array();
|
|
|
|
foreach ($this->backends as $backend) {
|
|
|
|
$groupIds = $backend->getGroups($search, $limit, $offset);
|
|
|
|
if (!is_null($limit)) {
|
|
|
|
$limit -= count($groupIds);
|
|
|
|
}
|
|
|
|
if (!is_null($offset)) {
|
|
|
|
$offset -= count($groupIds);
|
|
|
|
}
|
|
|
|
foreach ($groupIds as $groupId) {
|
2014-02-22 01:52:48 +04:00
|
|
|
$groups[$groupId] = $this->get($groupId);
|
2013-07-10 02:06:22 +04:00
|
|
|
}
|
|
|
|
if (!is_null($limit) and $limit <= 0) {
|
|
|
|
return array_values($groups);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array_values($groups);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \OC\User\User $user
|
|
|
|
* @return \OC\Group\Group[]
|
|
|
|
*/
|
|
|
|
public function getUserGroups($user) {
|
2014-02-22 01:52:48 +04:00
|
|
|
$uid = $user->getUID();
|
|
|
|
if (isset($this->cachedUserGroups[$uid])) {
|
|
|
|
return $this->cachedUserGroups[$uid];
|
|
|
|
}
|
2013-07-10 02:06:22 +04:00
|
|
|
$groups = array();
|
|
|
|
foreach ($this->backends as $backend) {
|
2014-02-22 01:52:48 +04:00
|
|
|
$groupIds = $backend->getUserGroups($uid);
|
2013-07-10 02:06:22 +04:00
|
|
|
foreach ($groupIds as $groupId) {
|
2014-02-22 01:52:48 +04:00
|
|
|
$groups[$groupId] = $this->get($groupId);
|
2013-07-10 02:06:22 +04:00
|
|
|
}
|
|
|
|
}
|
2014-02-22 01:52:48 +04:00
|
|
|
$this->cachedUserGroups[$uid] = array_values($groups);
|
|
|
|
return $this->cachedUserGroups[$uid];
|
2013-07-10 02:06:22 +04:00
|
|
|
}
|
|
|
|
}
|