1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
<?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)
* - postAddUser(\OC\Group\Group $group, \OC\User\User $user)
* - preRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
* - postRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
* - preDelete(\OC\Group\Group $group)
* - postDelete(\OC\Group\Group $group)
* - preCreate(string $groupId)
* - postCreate(\OC\Group\Group $group)
*
* @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[]
*/
private $cachedGroups;
/**
* @param \OC\User\Manager $userManager
*/
public function __construct($userManager) {
$this->userManager = $userManager;
$cache = & $this->cachedGroups;
$this->listen('\OC\Group', 'postDelete', function ($group) use (&$cache) {
/**
* @var \OC\Group\Group $group
*/
unset($cache[$group->getGID()]);
});
}
/**
* @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];
}
foreach ($this->backends as $backend) {
if ($backend->groupExists($gid)) {
return $this->getGroupObject($gid);
}
}
return null;
}
protected function getGroupObject($gid) {
$backends = array();
foreach ($this->backends as $backend) {
if ($backend->groupExists($gid)) {
$backends[] = $backend;
}
}
$this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this);
return $this->cachedGroups[$gid];
}
/**
* @param string $gid
* @return bool
*/
public function groupExists($gid) {
return !is_null($this->get($gid));
}
/**
* @param string $gid
* @return \OC\Group\Group
*/
public function createGroup($gid) {
if (!$gid) {
return false;
} else if ($this->groupExists($gid)) {
return $this->get($gid);
} else {
$this->emit('\OC\Group', 'preCreate', array($gid));
foreach ($this->backends as $backend) {
if ($backend->implementsActions(OC_GROUP_BACKEND_CREATE_GROUP)) {
$backend->createGroup($gid);
$group = $this->getGroupObject($gid);
$this->emit('\OC\Group', 'postCreate', array($group));
return $group;
}
}
return null;
}
}
/**
* @param string $search
* @param int $limit
* @param int $offset
* @return \OC\Group\Group[]
*/
public function search($search, $limit = null, $offset = null) {
$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) {
$groups[$groupId] = $this->getGroupObject($groupId);
}
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) {
$groups = array();
foreach ($this->backends as $backend) {
$groupIds = $backend->getUserGroups($user->getUID());
foreach ($groupIds as $groupId) {
$groups[$groupId] = $this->getGroupObject($groupId);
}
}
return array_values($groups);
}
}
|