Remove legacy class OC_Group and OC_User

* basically a straight replacement of the wrapped code at the calling code parts

Signed-off-by: Morris Jobke <hey@morrisjobke.de>
This commit is contained in:
Morris Jobke 2017-03-03 01:24:27 -06:00
parent 6fd0e7e939
commit a5ba1f7803
No known key found for this signature in database
GPG Key ID: 9CE5ED29E7FCD38A
13 changed files with 141 additions and 548 deletions

View File

@ -42,7 +42,7 @@ abstract class TestCase extends \Test\TestCase {
// reset backend // reset backend
\OC_User::clearBackends(); \OC_User::clearBackends();
\OC_Group::clearBackends(); \OC::$server->getGroupManager()->clearBackends();
// create users // create users
$backend = new \Test\Util\User\Dummy(); $backend = new \Test\Util\User\Dummy();
@ -76,8 +76,8 @@ abstract class TestCase extends \Test\TestCase {
// reset backend // reset backend
\OC_User::clearBackends(); \OC_User::clearBackends();
\OC_User::useBackend('database'); \OC_User::useBackend('database');
\OC_Group::clearBackends(); \OC::$server->getGroupManager()->clearBackends();
\OC_Group::useBackend(new Database()); \OC::$server->getGroupManager()->addBackend(new Database());
parent::tearDownAfterClass(); parent::tearDownAfterClass();
} }
@ -94,9 +94,15 @@ abstract class TestCase extends \Test\TestCase {
} }
if ($create) { if ($create) {
\OC::$server->getUserManager()->createUser($user, $password); $userManager = \OC::$server->getUserManager();
\OC_Group::createGroup('group'); $groupManager = \OC::$server->getGroupManager();
\OC_Group::addToGroup($user, 'group');
$userObject = $userManager->createUser($user, $password);
$group = $groupManager->createGroup('group');
if ($group and $userObject) {
$group->addUser($userObject);
}
} }
self::resetStorage(); self::resetStorage();

View File

@ -75,7 +75,7 @@ abstract class TestCase extends \Test\TestCase {
// reset backend // reset backend
\OC_User::clearBackends(); \OC_User::clearBackends();
\OC_Group::clearBackends(); \OC::$server->getGroupManager()->clearBackends();
// clear share hooks // clear share hooks
\OC_Hook::clear('OCP\\Share'); \OC_Hook::clear('OCP\\Share');
@ -103,7 +103,7 @@ abstract class TestCase extends \Test\TestCase {
$groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER3, 'group2'); $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER3, 'group2');
$groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER4, 'group3'); $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER4, 'group3');
$groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_GROUP1); $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_GROUP1);
\OC_Group::useBackend($groupBackend); \OC::$server->getGroupManager()->addBackend($groupBackend);
} }
protected function setUp() { protected function setUp() {
@ -136,7 +136,10 @@ abstract class TestCase extends \Test\TestCase {
if ($user !== null) { $user->delete(); } if ($user !== null) { $user->delete(); }
// delete group // delete group
\OC_Group::deleteGroup(self::TEST_FILES_SHARING_API_GROUP1); $group = \OC::$server->getGroupManager()->get(self::TEST_FILES_SHARING_API_GROUP1);
if ($group) {
$group->delete();
}
\OC_Util::tearDownFS(); \OC_Util::tearDownFS();
\OC_User::setUserId(''); \OC_User::setUserId('');
@ -145,8 +148,8 @@ abstract class TestCase extends \Test\TestCase {
// reset backend // reset backend
\OC_User::clearBackends(); \OC_User::clearBackends();
\OC_User::useBackend('database'); \OC_User::useBackend('database');
\OC_Group::clearBackends(); \OC::$server->getGroupManager()->clearBackends();
\OC_Group::useBackend(new \OC\Group\Database()); \OC::$server->getGroupManager()->addBackend(new \OC\Group\Database());
parent::tearDownAfterClass(); parent::tearDownAfterClass();
} }
@ -163,9 +166,15 @@ abstract class TestCase extends \Test\TestCase {
} }
if ($create) { if ($create) {
\OC::$server->getUserManager()->createUser($user, $password); $userManager = \OC::$server->getUserManager();
\OC_Group::createGroup('group'); $groupManager = \OC::$server->getGroupManager();
\OC_Group::addToGroup($user, 'group');
$userObject = $userManager->createUser($user, $password);
$group = $groupManager->createGroup('group');
if ($group and $userObject) {
$group->addUser($userObject);
}
} }
self::resetStorage(); self::resetStorage();

View File

@ -58,7 +58,7 @@ if(count($configPrefixes) === 1) {
if(count($configPrefixes) > 0) { if(count($configPrefixes) > 0) {
// register user backend // register user backend
OC_User::useBackend($userBackend); OC_User::useBackend($userBackend);
OC_Group::useBackend($groupBackend); \OC::$server->getGroupManager()->addBackend($groupBackend);
} }
\OCP\Util::connectHook( \OCP\Util::connectHook(

View File

@ -570,7 +570,7 @@ class Access extends LDAPUtility implements IUserTools {
$originalTTL = $this->connection->ldapCacheTTL; $originalTTL = $this->connection->ldapCacheTTL;
$this->connection->setConfiguration(array('ldapCacheTTL' => 0)); $this->connection->setConfiguration(array('ldapCacheTTL' => 0));
if(($isUser && !\OCP\User::userExists($intName)) if(($isUser && !\OCP\User::userExists($intName))
|| (!$isUser && !\OC_Group::groupExists($intName))) { || (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))) {
if($mapper->map($fdn, $intName, $uuid)) { if($mapper->map($fdn, $intName, $uuid)) {
$this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL));
return $intName; return $intName;
@ -737,7 +737,7 @@ class Access extends LDAPUtility implements IUserTools {
// Check to be really sure it is unique // Check to be really sure it is unique
// while loop is just a precaution. If a name is not generated within // while loop is just a precaution. If a name is not generated within
// 20 attempts, something else is very wrong. Avoids infinite loop. // 20 attempts, something else is very wrong. Avoids infinite loop.
if(!\OC_Group::groupExists($altName)) { if(!\OC::$server->getGroupManager()->groupExists($altName)) {
return $altName; return $altName;
} }
$altName = $name . '_' . ($lastNo + $attempts); $altName = $name . '_' . ($lastNo + $attempts);

View File

@ -64,7 +64,7 @@ class User_LDAPTest extends TestCase {
parent::setUp(); parent::setUp();
\OC_User::clearBackends(); \OC_User::clearBackends();
\OC_Group::clearBackends(); \OC::$server->getGroupManager()->clearBackends();
} }
/** /**

View File

@ -698,7 +698,7 @@ class OC {
} }
OC_User::useBackend(new \OC\User\Database()); OC_User::useBackend(new \OC\User\Database());
OC_Group::useBackend(new \OC\Group\Database()); \OC::$server->getGroupManager()->addBackend(new \OC\Group\Database());
// Subscribe to the hook // Subscribe to the hook
\OCP\Util::connectHook( \OCP\Util::connectHook(

View File

@ -273,8 +273,18 @@ class Util {
$result = \OCP\User::getUsers(); $result = \OCP\User::getUsers();
} else { } else {
$result = array_merge($result, $users); $result = array_merge($result, $users);
$groupManager = \OC::$server->getGroupManager();
foreach ($groups as $group) { foreach ($groups as $group) {
$result = array_merge($result, \OC_Group::usersInGroup($group)); $groupObject = $groupManager->get($group);
if ($groupObject) {
$foundUsers = $groupObject->searchUsers('', -1, 0);
$userIds = [];
foreach ($foundUsers as $user) {
$userIds[] = $user->getUID();
}
$result = array_merge($result, $userIds);
}
} }
} }

View File

@ -237,8 +237,19 @@ class Share extends Constants {
if (\OCP\DB::isError($result)) { if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR);
} else { } else {
$groupManager = \OC::$server->getGroupManager();
while ($row = $result->fetchRow()) { while ($row = $result->fetchRow()) {
$usersInGroup = \OC_Group::usersInGroup($row['share_with']);
$usersInGroup = [];
$group = $groupManager->get($row['share_with']);
if ($group) {
$users = $group->searchUsers('', -1, 0);
$userIds = array();
foreach ($users as $user) {
$userIds[] = $user->getUID();
}
$usersInGroup = $userIds;
}
$shares = array_merge($shares, $usersInGroup); $shares = array_merge($shares, $usersInGroup);
if ($returnUserPaths) { if ($returnUserPaths) {
foreach ($usersInGroup as $user) { foreach ($usersInGroup as $user) {
@ -468,7 +479,11 @@ class Share extends Constants {
//if didn't found a result than let's look for a group share. //if didn't found a result than let's look for a group share.
if(empty($shares) && $user !== null) { if(empty($shares) && $user !== null) {
$groups = \OC_Group::getUserGroups($user); $userObject = \OC::$server->getUserManager()->get($user);
$groups = [];
if ($userObject) {
$groups = \OC::$server->getGroupManager()->getUserGroupIds($userObject);
}
if (!empty($groups)) { if (!empty($groups)) {
$where = $fileDependentWhere . ' WHERE `' . $column . '` = ? AND `item_type` = ? AND `share_with` in (?)'; $where = $fileDependentWhere . ' WHERE `' . $column . '` = ? AND `item_type` = ? AND `share_with` in (?)';
@ -628,7 +643,18 @@ class Share extends Constants {
if ((int)$item['share_type'] === self::SHARE_TYPE_USER) { if ((int)$item['share_type'] === self::SHARE_TYPE_USER) {
$users[] = $item['share_with']; $users[] = $item['share_with'];
} else if ((int)$item['share_type'] === self::SHARE_TYPE_GROUP) { } else if ((int)$item['share_type'] === self::SHARE_TYPE_GROUP) {
$users = array_merge($users, \OC_Group::usersInGroup($item['share_with']));
$group = \OC::$server->getGroupManager()->get($item['share_with']);
$userIds = [];
if ($group) {
$users = $group->searchUsers('', -1, 0);
foreach ($users as $user) {
$userIds[] = $user->getUID();
}
return $userIds;
}
$users = array_merge($users, $userIds);
} }
} }
} }
@ -740,7 +766,19 @@ class Share extends Constants {
throw new \Exception($message_t); throw new \Exception($message_t);
} }
if ($shareWithinGroupOnly) { if ($shareWithinGroupOnly) {
$inGroup = array_intersect(\OC_Group::getUserGroups($uidOwner), \OC_Group::getUserGroups($shareWith)); $userManager = \OC::$server->getUserManager();
$groupManager = \OC::$server->getGroupManager();
$userOwner = $userManager->get($uidOwner);
$userShareWith = $userManager->get($shareWith);
$groupsOwner = [];
$groupsShareWith = [];
if ($userOwner) {
$groupsOwner = $groupManager->getUserGroupIds($userOwner);
}
if ($userShareWith) {
$groupsShareWith = $groupManager->getUserGroupIds($userShareWith);
}
$inGroup = array_intersect($groupsOwner, $groupsShareWith);
if (empty($inGroup)) { if (empty($inGroup)) {
$message = 'Sharing %s failed, because the user ' $message = 'Sharing %s failed, because the user '
.'%s is not a member of any groups that %s is a member of'; .'%s is not a member of any groups that %s is a member of';
@ -775,19 +813,23 @@ class Share extends Constants {
} }
} }
} else if ($shareType === self::SHARE_TYPE_GROUP) { } else if ($shareType === self::SHARE_TYPE_GROUP) {
if (!\OC_Group::groupExists($shareWith)) { if (!\OC::$server->getGroupManager()->groupExists($shareWith)) {
$message = 'Sharing %s failed, because the group %s does not exist'; $message = 'Sharing %s failed, because the group %s does not exist';
$message_t = $l->t('Sharing %s failed, because the group %s does not exist', array($itemSourceName, $shareWith)); $message_t = $l->t('Sharing %s failed, because the group %s does not exist', array($itemSourceName, $shareWith));
\OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);
throw new \Exception($message_t); throw new \Exception($message_t);
} }
if ($shareWithinGroupOnly && !\OC_Group::inGroup($uidOwner, $shareWith)) { if ($shareWithinGroupOnly && !\OC_Group::inGroup($uidOwner, $shareWith)) {
$group = \OC::$server->getGroupManager()->get($shareWith);
$user = \OC::$server->getUserManager()->get($uidOwner);
if (!$group || !$user || !$group->inGroup($user)) {
$message = 'Sharing %s failed, because ' $message = 'Sharing %s failed, because '
.'%s is not a member of the group %s'; . '%s is not a member of the group %s';
$message_t = $l->t('Sharing %s failed, because %s is not a member of the group %s', array($itemSourceName, $uidOwner, $shareWith)); $message_t = $l->t('Sharing %s failed, because %s is not a member of the group %s', array($itemSourceName, $uidOwner, $shareWith));
\OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $uidOwner, $shareWith), \OCP\Util::DEBUG); \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $uidOwner, $shareWith), \OCP\Util::DEBUG);
throw new \Exception($message_t); throw new \Exception($message_t);
} }
}
// Check if the item source is already shared with the group, either from the same owner or a different user // Check if the item source is already shared with the group, either from the same owner or a different user
// The check for each user in the group is done inside the put() function // The check for each user in the group is done inside the put() function
if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_GROUP, $shareWith, if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_GROUP, $shareWith,
@ -804,7 +846,18 @@ class Share extends Constants {
$group = $shareWith; $group = $shareWith;
$shareWith = array(); $shareWith = array();
$shareWith['group'] = $group; $shareWith['group'] = $group;
$shareWith['users'] = array_diff(\OC_Group::usersInGroup($group), array($uidOwner));
$groupObject = \OC::$server->getGroupManager()->get($group);
$userIds = [];
if ($groupObject) {
$users = $groupObject->searchUsers('', -1, 0);
foreach ($users as $user) {
$userIds[] = $user->getUID();
}
}
$shareWith['users'] = array_diff($userIds, array($uidOwner));
} else if ($shareType === self::SHARE_TYPE_LINK) { } else if ($shareType === self::SHARE_TYPE_LINK) {
$updateExistingShare = false; $updateExistingShare = false;
if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_links', 'yes') == 'yes') { if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_links', 'yes') == 'yes') {
@ -1057,7 +1110,9 @@ class Share extends Constants {
$itemUnshared = true; $itemUnshared = true;
break; break;
} elseif ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) { } elseif ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) {
if (\OC_Group::inGroup($uid, $share['share_with'])) { $group = \OC::$server->getGroupManager()->get($share['share_with']);
$user = \OC::$server->getUserManager()->get($uid);
if ($group && $user && $group->inGroup($user)) {
$groupShare = $share; $groupShare = $share;
} }
} elseif ((int)$share['share_type'] === self::$shareTypeGroupUserUnique && } elseif ((int)$share['share_type'] === self::$shareTypeGroupUserUnique &&
@ -1746,7 +1801,12 @@ class Share extends Constants {
$queryArgs[] = self::SHARE_TYPE_USER; $queryArgs[] = self::SHARE_TYPE_USER;
$queryArgs[] = self::$shareTypeGroupUserUnique; $queryArgs[] = self::$shareTypeGroupUserUnique;
$queryArgs[] = $shareWith; $queryArgs[] = $shareWith;
$groups = \OC_Group::getUserGroups($shareWith);
$user = \OC::$server->getUserManager()->get($shareWith);
$groups = [];
if ($user) {
$groups = \OC::$server->getGroupManager()->getUserGroupIds($user);
}
if (!empty($groups)) { if (!empty($groups)) {
$placeholders = join(',', array_fill(0, count($groups), '?')); $placeholders = join(',', array_fill(0, count($groups), '?'));
$where .= ' OR (`share_type` = ? AND `share_with` IN ('.$placeholders.')) '; $where .= ' OR (`share_type` = ? AND `share_with` IN ('.$placeholders.')) ';
@ -2171,7 +2231,17 @@ class Share extends Constants {
if (isset($shareWith['users'])) { if (isset($shareWith['users'])) {
$users = $shareWith['users']; $users = $shareWith['users'];
} else { } else {
$users = \OC_Group::usersInGroup($shareWith['group']); $group = \OC::$server->getGroupManager()->get($shareWith['group']);
if ($group) {
$users = $group->searchUsers('', -1, 0);
$userIds = [];
foreach ($users as $user) {
$userIds[] = $user->getUID();
}
$users = $userIds;
} else {
$users = [];
}
} }
// remove current user from list // remove current user from list
if (in_array(\OCP\User::getUser(), $users)) { if (in_array(\OCP\User::getUser(), $users)) {

View File

@ -204,9 +204,15 @@ class User implements IUser {
// FIXME: Feels like an hack - suggestions? // FIXME: Feels like an hack - suggestions?
$groupManager = \OC::$server->getGroupManager();
// We have to delete the user from all groups // We have to delete the user from all groups
foreach (\OC::$server->getGroupManager()->getUserGroupIds($this) as $groupId) { foreach ($groupManager->getUserGroupIds($this) as $groupId) {
\OC_Group::removeFromGroup($this->uid, $groupId); $group = $groupManager->get($groupId);
if ($group) {
\OC_Hook::emit("OC_Group", "pre_removeFromGroup", ["run" => true, "uid" => $this->uid, "gid" => $groupId]);
$group->removeUser($this);
\OC_Hook::emit("OC_User", "post_removeFromGroup", ["uid" => $this->uid, "gid" => $groupId]);
}
} }
// Delete the user's keys in preferences // Delete the user's keys in preferences
\OC::$server->getConfig()->deleteAllUserValues($this->uid); \OC::$server->getConfig()->deleteAllUserValues($this->uid);

View File

@ -1,302 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
* @author Bart Visscher <bartv@thisnet.nl>
* @author Björn Schießle <bjoern@schiessle.org>
* @author Georg Ehrke <georg@owncloud.com>
* @author goodkiller <markopraakli@gmail.com>
* @author Jakob Sack <mail@jakobsack.de>
* @author Lukas Reschke <lukas@statuscode.ch>
* @author macjohnny <estebanmarin@gmx.ch>
* @author Michael Gapczynski <GapczynskiM@gmail.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Qingping Hou <dave2008713@gmail.com>
* @author Robin Appelman <robin@icewind.nl>
* @author Robin McCorkell <robin@mccorkell.me.uk>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
/**
* This class provides all methods needed for managing groups.
*
* Note that &run is deprecated and won't work anymore.
* 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 {
/**
* @return \OC\Group\Manager
* @deprecated Use \OC::$server->getGroupManager();
*/
public static function getManager() {
return \OC::$server->getGroupManager();
}
/**
* @return \OC\User\Manager
* @deprecated Use \OC::$server->getUserManager()
*/
private static function getUserManager() {
return \OC::$server->getUserManager();
}
/**
* set the group backend
* @param \OC\Group\Backend $backend The backend to use for user management
* @return bool
*/
public static function useBackend($backend) {
self::getManager()->addBackend($backend);
return true;
}
/**
* remove all used backends
*/
public static function clearBackends() {
self::getManager()->clearBackends();
}
/**
* Try to create a new group
* @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
* @deprecated Use \OC::$server->getGroupManager()->createGroup() instead
*/
public static function createGroup($gid) {
if (self::getManager()->createGroup($gid)) {
return true;
} else {
return false;
}
}
/**
* delete a group
* @param string $gid gid of the group to delete
* @return bool
*
* Deletes a group and removes it from the group_user-table
* @deprecated Use \OC::$server->getGroupManager()->delete() instead
*/
public static function deleteGroup($gid) {
$group = self::getManager()->get($gid);
if ($group) {
if ($group->delete()) {
return true;
}
}
return false;
}
/**
* is user in group?
* @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.
* @deprecated Use \OC::$server->getGroupManager->inGroup($user);
*/
public static function inGroup($uid, $gid) {
$group = self::getManager()->get($gid);
$user = self::getUserManager()->get($uid);
if ($group and $user) {
return $group->inGroup($user);
}
return false;
}
/**
* Add a user to a group
* @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.
* @deprecated Use \OC::$server->getGroupManager->addUser();
*/
public static function addToGroup($uid, $gid) {
$group = self::getManager()->get($gid);
$user = self::getUserManager()->get($uid);
if ($group and $user) {
$group->addUser($user);
return true;
} else {
return false;
}
}
/**
* Removes a user from a group
* @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.
*/
public static function removeFromGroup($uid, $gid) {
$group = self::getManager()->get($gid);
$user = self::getUserManager()->get($uid);
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));
return true;
} else {
return false;
}
}
/**
* Get all groups a user belongs to
* @param string $uid Name of the user
* @return array an array of group names
*
* This function fetches all groups a user belongs to. It does not check
* if the user exists at all.
* @deprecated Use \OC::$server->getGroupManager->getUserGroupIds($user)
*/
public static function getUserGroups($uid) {
$user = self::getUserManager()->get($uid);
if ($user) {
return self::getManager()->getUserGroupIds($user);
} else {
return array();
}
}
/**
* get a list of all groups
* @param string $search
* @param int|null $limit
* @param int|null $offset
* @return array an array of group names
*
* Returns a list with all groups
*/
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();
}
return $groupIds;
}
/**
* check if a group exists
*
* @param string $gid
* @return bool
* @deprecated Use \OC::$server->getGroupManager->groupExists($gid)
*/
public static function groupExists($gid) {
return self::getManager()->groupExists($gid);
}
/**
* 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
*/
public static function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
$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();
}
}
/**
* get a list of all users in several groups
* @param string[] $gids
* @param string $search
* @param int $limit
* @param int $offset
* @return array an array of user ids
*/
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;
}
/**
* 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)
* @deprecated Use \OC::$server->getGroupManager->displayNamesInGroup($gid, $search, $limit, $offset)
*/
public static function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
return self::getManager()->displayNamesInGroup($gid, $search, $limit, $offset);
}
/**
* get a list of all display names in several groups
* @param array $gids
* @param string $search
* @param int $limit
* @param int $offset
* @return array an array of display names (Key) user ids (value)
*/
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) {
// A fix for LDAP users. array_merge loses keys...
$displayNames = $diff + $displayNames;
}
}
return $displayNames;
}
}

View File

@ -333,7 +333,9 @@ class OC_User {
* @return bool * @return bool
*/ */
public static function isAdminUser($uid) { public static function isAdminUser($uid) {
if (OC_Group::inGroup($uid, 'admin') && self::$incognitoMode === false) { $group = \OC::$server->getGroupManager()->get('admin');
$user = \OC::$server->getUserManager()->get($uid);
if ($group && $user && $group->inGroup($user) && self::$incognitoMode === false) {
return true; return true;
} }
return false; return false;

View File

@ -40,7 +40,7 @@ OC_Util::checkSubAdminUser();
\OC::$server->getNavigationManager()->setActiveEntry('core_users'); \OC::$server->getNavigationManager()->setActiveEntry('core_users');
$userManager = \OC::$server->getUserManager(); $userManager = \OC::$server->getUserManager();
$groupManager = \OC_Group::getManager(); $groupManager = \OC::$server->getGroupManager();
// Set the sort option: SORT_USERCOUNT or SORT_GROUPNAME // Set the sort option: SORT_USERCOUNT or SORT_GROUPNAME
$sortGroupsBy = \OC\Group\MetaData::SORT_USERCOUNT; $sortGroupsBy = \OC\Group\MetaData::SORT_USERCOUNT;

View File

@ -1,208 +0,0 @@
<?php
/**
* ownCloud
*
* @author Robin Appelman
* @author Bernhard Posselt
* @copyright 2012 Robin Appelman <icewind@owncloud.com>
* @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
*
* 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/>.
*
*/
namespace Test\Group;
use OC_Group;
use OC_User;
/**
* Class LegacyGroupTest
*
* @package Test\Group
* @group DB
*/
class LegacyGroupTest extends \Test\TestCase {
protected function setUp() {
parent::setUp();
OC_Group::clearBackends();
OC_User::clearBackends();
}
public function testSingleBackend() {
$userBackend = new \Test\Util\User\Dummy();
\OC::$server->getUserManager()->registerBackend($userBackend);
OC_Group::useBackend(new \Test\Util\Group\Dummy());
$group1 = $this->getUniqueID();
$group2 = $this->getUniqueID();
OC_Group::createGroup($group1);
OC_Group::createGroup($group2);
$user1 = $this->getUniqueID();
$user2 = $this->getUniqueID();
$userBackend->createUser($user1, '');
$userBackend->createUser($user2, '');
$this->assertFalse(OC_Group::inGroup($user1, $group1), 'Asserting that user1 is not in group1');
$this->assertFalse(OC_Group::inGroup($user2, $group1), 'Asserting that user2 is not in group1');
$this->assertFalse(OC_Group::inGroup($user1, $group2), 'Asserting that user1 is not in group2');
$this->assertFalse(OC_Group::inGroup($user2, $group2), 'Asserting that user2 is not in group2');
$this->assertTrue(OC_Group::addToGroup($user1, $group1));
$this->assertTrue(OC_Group::inGroup($user1, $group1), 'Asserting that user1 is in group1');
$this->assertFalse(OC_Group::inGroup($user2, $group1), 'Asserting that user2 is not in group1');
$this->assertFalse(OC_Group::inGroup($user1, $group2), 'Asserting that user1 is not in group2');
$this->assertFalse(OC_Group::inGroup($user2, $group2), 'Asserting that user2 is not in group2');
$this->assertTrue(OC_Group::addToGroup($user1, $group1));
$this->assertEquals(array($user1), OC_Group::usersInGroup($group1));
$this->assertEquals(array(), OC_Group::usersInGroup($group2));
$this->assertEquals(array($group1), OC_Group::getUserGroups($user1));
$this->assertEquals(array(), OC_Group::getUserGroups($user2));
OC_Group::deleteGroup($group1);
$this->assertEquals(array(), OC_Group::getUserGroups($user1));
$this->assertEquals(array(), OC_Group::usersInGroup($group1));
$this->assertFalse(OC_Group::inGroup($user1, $group1));
}
public function testNoEmptyGIDs() {
OC_Group::useBackend(new \Test\Util\Group\Dummy());
$emptyGroup = null;
$this->assertFalse(OC_Group::createGroup($emptyGroup));
}
public function testNoGroupsTwice() {
OC_Group::useBackend(new \Test\Util\Group\Dummy());
$group = $this->getUniqueID();
OC_Group::createGroup($group);
$groupCopy = $group;
OC_Group::createGroup($groupCopy);
$this->assertEquals(array($group), OC_Group::getGroups());
}
public function testDontDeleteAdminGroup() {
OC_Group::useBackend(new \Test\Util\Group\Dummy());
$adminGroup = 'admin';
OC_Group::createGroup($adminGroup);
$this->assertFalse(OC_Group::deleteGroup($adminGroup));
$this->assertEquals(array($adminGroup), OC_Group::getGroups());
}
public function testDontAddUserToNonexistentGroup() {
OC_Group::useBackend(new \Test\Util\Group\Dummy());
$groupNonExistent = 'notExistent';
$user = $this->getUniqueID();
$this->assertEquals(false, OC_Group::addToGroup($user, $groupNonExistent));
$this->assertEquals(array(), OC_Group::getGroups());
}
public function testUsersInGroup() {
OC_Group::useBackend(new \Test\Util\Group\Dummy());
$userBackend = new \Test\Util\User\Dummy();
\OC::$server->getUserManager()->registerBackend($userBackend);
$group1 = $this->getUniqueID();
$group2 = $this->getUniqueID();
$group3 = $this->getUniqueID();
$user1 = $this->getUniqueID();
$user2 = $this->getUniqueID();
$user3 = $this->getUniqueID();
OC_Group::createGroup($group1);
OC_Group::createGroup($group2);
OC_Group::createGroup($group3);
$userBackend->createUser($user1, '');
$userBackend->createUser($user2, '');
$userBackend->createUser($user3, '');
OC_Group::addToGroup($user1, $group1);
OC_Group::addToGroup($user2, $group1);
OC_Group::addToGroup($user3, $group1);
OC_Group::addToGroup($user3, $group2);
$this->assertEquals(array($user1, $user2, $user3),
OC_Group::usersInGroups(array($group1, $group2, $group3)));
// FIXME: needs more parameter variation
}
public function testMultiBackend() {
$userBackend = new \Test\Util\User\Dummy();
\OC::$server->getUserManager()->registerBackend($userBackend);
$backend1 = new \Test\Util\Group\Dummy();
$backend2 = new \Test\Util\Group\Dummy();
OC_Group::useBackend($backend1);
OC_Group::useBackend($backend2);
$group1 = $this->getUniqueID();
$group2 = $this->getUniqueID();
OC_Group::createGroup($group1);
//groups should be added to the first registered backend
$this->assertEquals(array($group1), $backend1->getGroups());
$this->assertEquals(array(), $backend2->getGroups());
$this->assertEquals(array($group1), OC_Group::getGroups());
$this->assertTrue(OC_Group::groupExists($group1));
$this->assertFalse(OC_Group::groupExists($group2));
$backend1->createGroup($group2);
$this->assertEquals(array($group1, $group2), OC_Group::getGroups());
$this->assertTrue(OC_Group::groupExists($group1));
$this->assertTrue(OC_Group::groupExists($group2));
$user1 = $this->getUniqueID();
$user2 = $this->getUniqueID();
$userBackend->createUser($user1, '');
$userBackend->createUser($user2, '');
$this->assertFalse(OC_Group::inGroup($user1, $group1));
$this->assertFalse(OC_Group::inGroup($user2, $group1));
$this->assertTrue(OC_Group::addToGroup($user1, $group1));
$this->assertTrue(OC_Group::inGroup($user1, $group1));
$this->assertFalse(OC_Group::inGroup($user2, $group1));
$this->assertFalse($backend2->inGroup($user1, $group1));
OC_Group::addToGroup($user1, $group1);
$this->assertEquals(array($user1), OC_Group::usersInGroup($group1));
$this->assertEquals(array($group1), OC_Group::getUserGroups($user1));
$this->assertEquals(array(), OC_Group::getUserGroups($user2));
OC_Group::deleteGroup($group1);
$this->assertEquals(array(), OC_Group::getUserGroups($user1));
$this->assertEquals(array(), OC_Group::usersInGroup($group1));
$this->assertFalse(OC_Group::inGroup($user1, $group1));
}
}