Blame view
sources/lib/private/group.php
7.59 KB
|
03e52840d
|
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 |
<?php
/**
* ownCloud
*
* @author Frank Karlitschek
* @copyright 2012 Frank Karlitschek frank@owncloud.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* This class provides all methods needed for managing groups.
*
* Hooks provided:
* pre_createGroup(&run, gid)
* post_createGroup(gid)
* pre_deleteGroup(&run, gid)
* post_deleteGroup(gid)
* pre_addToGroup(&run, uid, gid)
* post_addToGroup(uid, gid)
* pre_removeFromGroup(&run, uid, gid)
* post_removeFromGroup(uid, gid)
*/
class OC_Group {
|
|
31b7f2792
|
37 38 |
/** |
|
f7d878ff1
|
39 |
* @return \OC\Group\Manager |
|
31b7f2792
|
40 |
*/ |
|
f7d878ff1
|
41 42 43 |
public static function getManager() {
return \OC::$server->getGroupManager();
}
|
|
31b7f2792
|
44 45 |
/** |
|
f7d878ff1
|
46 |
* @return \OC\User\Manager |
|
31b7f2792
|
47 |
*/ |
|
f7d878ff1
|
48 49 |
private static function getUserManager() {
return \OC::$server->getUserManager();
|
|
31b7f2792
|
50 |
} |
|
03e52840d
|
51 52 |
/** |
|
6d9380f96
|
53 54 |
* set the group backend * @param \OC_Group_Backend $backend The backend to use for user managment |
|
03e52840d
|
55 56 |
* @return bool */ |
|
31b7f2792
|
57 58 59 |
public static function useBackend($backend) {
self::getManager()->addBackend($backend);
return true;
|
|
03e52840d
|
60 61 62 63 64 65 |
}
/**
* remove all used backends
*/
public static function clearBackends() {
|
|
31b7f2792
|
66 |
self::getManager()->clearBackends(); |
|
03e52840d
|
67 68 69 |
} /** |
|
6d9380f96
|
70 |
* Try to create a new group |
|
03e52840d
|
71 72 73 74 75 76 |
* @param string $gid The name of the group to create * @return bool * * Tries to create a new group. If the group name already exists, false will * be returned. Basic checking of Group name */ |
|
31b7f2792
|
77 78 |
public static function createGroup($gid) {
OC_Hook::emit("OC_Group", "pre_createGroup", array("run" => true, "gid" => $gid));
|
|
03e52840d
|
79 |
|
|
31b7f2792
|
80 81 82 83 |
if (self::getManager()->createGroup($gid)) {
OC_Hook::emit("OC_User", "post_createGroup", array("gid" => $gid));
return true;
} else {
|
|
03e52840d
|
84 85 86 87 88 |
return false; } } /** |
|
6d9380f96
|
89 |
* delete a group |
|
03e52840d
|
90 91 92 93 94 |
* @param string $gid gid of the group to delete * @return bool * * Deletes a group and removes it from the group_user-table */ |
|
31b7f2792
|
95 |
public static function deleteGroup($gid) {
|
|
03e52840d
|
96 |
// Prevent users from deleting group admin |
|
31b7f2792
|
97 |
if ($gid == "admin") {
|
|
03e52840d
|
98 99 |
return false; } |
|
31b7f2792
|
100 |
OC_Hook::emit("OC_Group", "pre_deleteGroup", array("run" => true, "gid" => $gid));
|
|
03e52840d
|
101 |
|
|
31b7f2792
|
102 103 104 105 |
$group = self::getManager()->get($gid);
if ($group) {
if ($group->delete()) {
OC_Hook::emit("OC_User", "post_deleteGroup", array("gid" => $gid));
|
|
03e52840d
|
106 107 |
return true; } |
|
03e52840d
|
108 |
} |
|
31b7f2792
|
109 |
return false; |
|
03e52840d
|
110 111 112 |
} /** |
|
6d9380f96
|
113 |
* is user in group? |
|
03e52840d
|
114 115 116 117 118 119 |
* @param string $uid uid of the user * @param string $gid gid of the group * @return bool * * Checks whether the user is member of a group or not. */ |
|
31b7f2792
|
120 121 |
public static function inGroup($uid, $gid) {
$group = self::getManager()->get($gid);
|
|
f7d878ff1
|
122 |
$user = self::getUserManager()->get($uid); |
|
31b7f2792
|
123 124 |
if ($group and $user) {
return $group->inGroup($user);
|
|
03e52840d
|
125 126 127 128 129 |
} return false; } /** |
|
6d9380f96
|
130 |
* Add a user to a group |
|
03e52840d
|
131 132 133 134 135 136 |
* @param string $uid Name of the user to add to group * @param string $gid Name of the group in which add the user * @return bool * * Adds a user to a group. */ |
|
31b7f2792
|
137 138 |
public static function addToGroup($uid, $gid) {
$group = self::getManager()->get($gid);
|
|
f7d878ff1
|
139 |
$user = self::getUserManager()->get($uid); |
|
31b7f2792
|
140 141 142 143 144 145 |
if ($group and $user) {
OC_Hook::emit("OC_Group", "pre_addToGroup", array("run" => true, "uid" => $uid, "gid" => $gid));
$group->addUser($user);
OC_Hook::emit("OC_User", "post_addToGroup", array("uid" => $uid, "gid" => $gid));
return true;
} else {
|
|
03e52840d
|
146 147 148 149 150 |
return false; } } /** |
|
6d9380f96
|
151 |
* Removes a user from a group |
|
03e52840d
|
152 153 154 155 156 157 |
* @param string $uid Name of the user to remove from group * @param string $gid Name of the group from which remove the user * @return bool * * removes the user from a group. */ |
|
31b7f2792
|
158 159 |
public static function removeFromGroup($uid, $gid) {
$group = self::getManager()->get($gid);
|
|
f7d878ff1
|
160 |
$user = self::getUserManager()->get($uid); |
|
31b7f2792
|
161 162 163 164 |
if ($group and $user) {
OC_Hook::emit("OC_Group", "pre_removeFromGroup", array("run" => true, "uid" => $uid, "gid" => $gid));
$group->removeUser($user);
OC_Hook::emit("OC_User", "post_removeFromGroup", array("uid" => $uid, "gid" => $gid));
|
|
03e52840d
|
165 |
return true; |
|
31b7f2792
|
166 |
} else {
|
|
03e52840d
|
167 168 169 170 171 |
return false; } } /** |
|
6d9380f96
|
172 |
* Get all groups a user belongs to |
|
03e52840d
|
173 |
* @param string $uid Name of the user |
|
6d9380f96
|
174 |
* @return array an array of group names |
|
03e52840d
|
175 176 177 178 |
* * This function fetches all groups a user belongs to. It does not check * if the user exists at all. */ |
|
31b7f2792
|
179 |
public static function getUserGroups($uid) {
|
|
f7d878ff1
|
180 |
$user = self::getUserManager()->get($uid); |
|
31b7f2792
|
181 |
if ($user) {
|
|
6d9380f96
|
182 |
return self::getManager()->getUserGroupIds($user); |
|
31b7f2792
|
183 184 |
} else {
return array();
|
|
03e52840d
|
185 |
} |
|
03e52840d
|
186 187 188 |
} /** |
|
6d9380f96
|
189 190 191 192 193 |
* get a list of all groups * @param string $search * @param int|null $limit * @param int|null $offset * @return array an array of group names |
|
03e52840d
|
194 195 196 |
* * Returns a list with all groups */ |
|
31b7f2792
|
197 198 199 200 201 |
public static function getGroups($search = '', $limit = null, $offset = null) {
$groups = self::getManager()->search($search, $limit, $offset);
$groupIds = array();
foreach ($groups as $group) {
$groupIds[] = $group->getGID();
|
|
03e52840d
|
202 |
} |
|
31b7f2792
|
203 |
return $groupIds; |
|
03e52840d
|
204 205 206 207 |
} /** * check if a group exists |
|
31b7f2792
|
208 |
* |
|
03e52840d
|
209 210 211 212 |
* @param string $gid
* @return bool
*/
public static function groupExists($gid) {
|
|
31b7f2792
|
213 |
return self::getManager()->groupExists($gid); |
|
03e52840d
|
214 215 216 |
} /** |
|
6d9380f96
|
217 218 219 220 221 222 |
* get a list of all users in a group * @param string $gid * @param string $search * @param int $limit * @param int $offset * @return array an array of user ids |
|
03e52840d
|
223 224 |
*/
public static function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
|
|
31b7f2792
|
225 226 227 228 229 230 231 232 233 234 |
$group = self::getManager()->get($gid);
if ($group) {
$users = $group->searchUsers($search, $limit, $offset);
$userIds = array();
foreach ($users as $user) {
$userIds[] = $user->getUID();
}
return $userIds;
} else {
return array();
|
|
03e52840d
|
235 |
} |
|
03e52840d
|
236 237 238 |
} /** |
|
6d9380f96
|
239 240 |
* get a list of all users in several groups * @param string[] $gids |
|
03e52840d
|
241 242 243 |
* @param string $search * @param int $limit * @param int $offset |
|
6d9380f96
|
244 |
* @return array an array of user ids |
|
03e52840d
|
245 246 247 248 249 250 251 252 253 254 255 |
*/
public static function usersInGroups($gids, $search = '', $limit = -1, $offset = 0) {
$users = array();
foreach ($gids as $gid) {
// TODO Need to apply limits to groups as total
$users = array_merge(array_diff(self::usersInGroup($gid, $search, $limit, $offset), $users), $users);
}
return $users;
}
/**
|
|
6d9380f96
|
256 257 258 259 260 261 |
* get a list of all display names in a group * @param string $gid * @param string $search * @param int $limit * @param int $offset * @return array an array of display names (value) and user ids(key) |
|
03e52840d
|
262 263 |
*/
public static function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
|
|
837968727
|
264 |
return self::getManager()->displayNamesInGroup($gid, $search, $limit, $offset); |
|
03e52840d
|
265 266 267 |
} /** |
|
6d9380f96
|
268 |
* get a list of all display names in several groups |
|
03e52840d
|
269 270 271 272 |
* @param array $gids * @param string $search * @param int $limit * @param int $offset |
|
6d9380f96
|
273 |
* @return array an array of display names (Key) user ids (value) |
|
03e52840d
|
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
*/
public static function displayNamesInGroups($gids, $search = '', $limit = -1, $offset = 0) {
$displayNames = array();
foreach ($gids as $gid) {
// TODO Need to apply limits to groups as total
$diff = array_diff(
self::displayNamesInGroup($gid, $search, $limit, $offset),
$displayNames
);
if ($diff) {
$displayNames = array_merge($diff, $displayNames);
}
}
return $displayNames;
}
}
|