use new group api as backend for the old api
This commit is contained in:
parent
a735105a22
commit
912b151561
2 changed files with 160 additions and 173 deletions
225
lib/group.php
225
lib/group.php
|
@ -34,28 +34,43 @@
|
|||
* post_removeFromGroup(uid, gid)
|
||||
*/
|
||||
class OC_Group {
|
||||
// The backend used for group management
|
||||
/**
|
||||
* @var OC_Group_Interface[]
|
||||
* @var \OC\Group\Manager $manager
|
||||
*/
|
||||
private static $_usedBackends = array();
|
||||
private static $manager;
|
||||
|
||||
/**
|
||||
* @var \OC\User\Manager
|
||||
*/
|
||||
private static $userManager;
|
||||
|
||||
/**
|
||||
* @return \OC\Group\Manager
|
||||
*/
|
||||
public static function getManager() {
|
||||
if (self::$manager) {
|
||||
return self::$manager;
|
||||
}
|
||||
self::$userManager = \OC_User::getManager();
|
||||
self::$manager = new \OC\Group\Manager(self::$userManager);
|
||||
return self::$manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief set the group backend
|
||||
* @param string $backend The backend to use for user managment
|
||||
* @param \OC_Group_Backend $backend The backend to use for user managment
|
||||
* @return bool
|
||||
*/
|
||||
public static function useBackend( $backend ) {
|
||||
if($backend instanceof OC_Group_Interface) {
|
||||
self::$_usedBackends[]=$backend;
|
||||
}
|
||||
public static function useBackend($backend) {
|
||||
self::getManager()->addBackend($backend);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* remove all used backends
|
||||
*/
|
||||
public static function clearBackends() {
|
||||
self::$_usedBackends=array();
|
||||
self::getManager()->clearBackends();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -66,32 +81,13 @@ class OC_Group {
|
|||
* Tries to create a new group. If the group name already exists, false will
|
||||
* be returned. Basic checking of Group name
|
||||
*/
|
||||
public static function createGroup( $gid ) {
|
||||
// No empty group names!
|
||||
if( !$gid ) {
|
||||
return false;
|
||||
}
|
||||
// No duplicate group names
|
||||
if( in_array( $gid, self::getGroups())) {
|
||||
return false;
|
||||
}
|
||||
public static function createGroup($gid) {
|
||||
OC_Hook::emit("OC_Group", "pre_createGroup", array("run" => true, "gid" => $gid));
|
||||
|
||||
$run = true;
|
||||
OC_Hook::emit( "OC_Group", "pre_createGroup", array( "run" => &$run, "gid" => $gid ));
|
||||
|
||||
if($run) {
|
||||
//create the group in the first backend that supports creating groups
|
||||
foreach(self::$_usedBackends as $backend) {
|
||||
if(!$backend->implementsActions(OC_GROUP_BACKEND_CREATE_GROUP))
|
||||
continue;
|
||||
|
||||
$backend->createGroup($gid);
|
||||
OC_Hook::emit( "OC_User", "post_createGroup", array( "gid" => $gid ));
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}else{
|
||||
if (self::getManager()->create($gid)) {
|
||||
OC_Hook::emit("OC_User", "post_createGroup", array("gid" => $gid));
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -103,30 +99,22 @@ class OC_Group {
|
|||
*
|
||||
* Deletes a group and removes it from the group_user-table
|
||||
*/
|
||||
public static function deleteGroup( $gid ) {
|
||||
public static function deleteGroup($gid) {
|
||||
// Prevent users from deleting group admin
|
||||
if( $gid == "admin" ) {
|
||||
if ($gid == "admin") {
|
||||
return false;
|
||||
}
|
||||
|
||||
$run = true;
|
||||
OC_Hook::emit( "OC_Group", "pre_deleteGroup", array( "run" => &$run, "gid" => $gid ));
|
||||
|
||||
if($run) {
|
||||
//delete the group from all backends
|
||||
foreach(self::$_usedBackends as $backend) {
|
||||
if(!$backend->implementsActions(OC_GROUP_BACKEND_DELETE_GROUP))
|
||||
continue;
|
||||
|
||||
$backend->deleteGroup($gid);
|
||||
OC_Hook::emit( "OC_User", "post_deleteGroup", array( "gid" => $gid ));
|
||||
OC_Hook::emit("OC_Group", "pre_deleteGroup", array("run" => true, "gid" => $gid));
|
||||
|
||||
$group = self::getManager()->get($gid);
|
||||
if ($group) {
|
||||
if ($group->delete()) {
|
||||
OC_Hook::emit("OC_User", "post_deleteGroup", array("gid" => $gid));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -137,11 +125,11 @@ class OC_Group {
|
|||
*
|
||||
* Checks whether the user is member of a group or not.
|
||||
*/
|
||||
public static function inGroup( $uid, $gid ) {
|
||||
foreach(self::$_usedBackends as $backend) {
|
||||
if($backend->inGroup($uid, $gid)) {
|
||||
return true;
|
||||
}
|
||||
public static function inGroup($uid, $gid) {
|
||||
$group = self::getManager()->get($gid);
|
||||
$user = self::$userManager->get($uid);
|
||||
if ($group and $user) {
|
||||
return $group->inGroup($user);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -154,33 +142,15 @@ class OC_Group {
|
|||
*
|
||||
* Adds a user to a group.
|
||||
*/
|
||||
public static function addToGroup( $uid, $gid ) {
|
||||
// Does the group exist?
|
||||
if( !OC_Group::groupExists($gid)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Go go go
|
||||
$run = true;
|
||||
OC_Hook::emit( "OC_Group", "pre_addToGroup", array( "run" => &$run, "uid" => $uid, "gid" => $gid ));
|
||||
|
||||
if($run) {
|
||||
$success=false;
|
||||
|
||||
//add the user to the all backends that have the group
|
||||
foreach(self::$_usedBackends as $backend) {
|
||||
if(!$backend->implementsActions(OC_GROUP_BACKEND_ADD_TO_GROUP))
|
||||
continue;
|
||||
|
||||
if($backend->groupExists($gid)) {
|
||||
$success|=$backend->addToGroup($uid, $gid);
|
||||
}
|
||||
}
|
||||
if($success) {
|
||||
OC_Hook::emit( "OC_User", "post_addToGroup", array( "uid" => $uid, "gid" => $gid ));
|
||||
}
|
||||
return $success;
|
||||
}else{
|
||||
public static function addToGroup($uid, $gid) {
|
||||
$group = self::getManager()->get($gid);
|
||||
$user = self::$userManager->get($uid);
|
||||
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 {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -193,21 +163,15 @@ class OC_Group {
|
|||
*
|
||||
* removes the user from a group.
|
||||
*/
|
||||
public static function removeFromGroup( $uid, $gid ) {
|
||||
$run = true;
|
||||
OC_Hook::emit( "OC_Group", "pre_removeFromGroup", array( "run" => &$run, "uid" => $uid, "gid" => $gid ));
|
||||
|
||||
if($run) {
|
||||
//remove the user from the all backends that have the group
|
||||
foreach(self::$_usedBackends as $backend) {
|
||||
if(!$backend->implementsActions(OC_GROUP_BACKEND_REMOVE_FROM_GOUP))
|
||||
continue;
|
||||
|
||||
$backend->removeFromGroup($uid, $gid);
|
||||
OC_Hook::emit( "OC_User", "post_removeFromGroup", array( "uid" => $uid, "gid" => $gid ));
|
||||
}
|
||||
public static function removeFromGroup($uid, $gid) {
|
||||
$group = self::getManager()->get($gid);
|
||||
$user = self::$userManager->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{
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -220,13 +184,18 @@ class OC_Group {
|
|||
* This function fetches all groups a user belongs to. It does not check
|
||||
* if the user exists at all.
|
||||
*/
|
||||
public static function getUserGroups( $uid ) {
|
||||
$groups=array();
|
||||
foreach(self::$_usedBackends as $backend) {
|
||||
$groups=array_merge($backend->getUserGroups($uid), $groups);
|
||||
public static function getUserGroups($uid) {
|
||||
$user = self::$userManager->get($uid);
|
||||
if ($user) {
|
||||
$groups = self::getManager()->getUserGroups($user);
|
||||
$groupIds = array();
|
||||
foreach ($groups as $group) {
|
||||
$groupIds[] = $group->getGID();
|
||||
}
|
||||
return $groupIds;
|
||||
} else {
|
||||
return array();
|
||||
}
|
||||
asort($groups);
|
||||
return $groups;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -235,27 +204,23 @@ class OC_Group {
|
|||
*
|
||||
* Returns a list with all groups
|
||||
*/
|
||||
public static function getGroups($search = '', $limit = -1, $offset = 0) {
|
||||
$groups = array();
|
||||
foreach (self::$_usedBackends as $backend) {
|
||||
$groups = array_merge($backend->getGroups($search, $limit, $offset), $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();
|
||||
}
|
||||
asort($groups);
|
||||
return $groups;
|
||||
return $groupIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if a group exists
|
||||
*
|
||||
* @param string $gid
|
||||
* @return bool
|
||||
*/
|
||||
public static function groupExists($gid) {
|
||||
foreach(self::$_usedBackends as $backend) {
|
||||
if ($backend->groupExists($gid)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return self::getManager()->exists($gid);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -263,11 +228,17 @@ class OC_Group {
|
|||
* @returns array with user ids
|
||||
*/
|
||||
public static function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
|
||||
$users=array();
|
||||
foreach(self::$_usedBackends as $backend) {
|
||||
$users = array_merge($backend->usersInGroup($gid, $search, $limit, $offset), $users);
|
||||
$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();
|
||||
}
|
||||
return $users;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -292,17 +263,17 @@ class OC_Group {
|
|||
* @returns array with display names (value) and user ids(key)
|
||||
*/
|
||||
public static function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
|
||||
$displayNames=array();
|
||||
foreach(self::$_usedBackends as $backend) {
|
||||
if($backend->implementsActions(OC_GROUP_BACKEND_GET_DISPLAYNAME)) {
|
||||
$displayNames = array_merge($backend->displayNamesInGroup($gid, $search, $limit, $offset), $displayNames);
|
||||
} else {
|
||||
$users = $backend->usersInGroup($gid, $search, $limit, $offset);
|
||||
$names = array_combine($users, $users);
|
||||
$displayNames = array_merge($names, $displayNames);
|
||||
$group = self::getManager()->get($gid);
|
||||
if ($group) {
|
||||
$users = $group->searchDisplayName($search . $limit, $offset);
|
||||
$displayNames = array();
|
||||
foreach ($users as $user) {
|
||||
$displayNames[] = $user->getDisplayName();
|
||||
}
|
||||
return $displayNames;
|
||||
} else {
|
||||
return array();
|
||||
}
|
||||
return $displayNames;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,56 +1,61 @@
|
|||
<?php
|
||||
/**
|
||||
* ownCloud
|
||||
*
|
||||
* @author Robin Appelman
|
||||
* @author Bernhard Posselt
|
||||
* @copyright 2012 Robin Appelman icewind@owncloud.com
|
||||
* @copyright 2012 Bernhard Posselt nukeawhale@gmail.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/>.
|
||||
*
|
||||
*/
|
||||
* ownCloud
|
||||
*
|
||||
* @author Robin Appelman
|
||||
* @author Bernhard Posselt
|
||||
* @copyright 2012 Robin Appelman icewind@owncloud.com
|
||||
* @copyright 2012 Bernhard Posselt nukeawhale@gmail.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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
class Test_Group extends PHPUnit_Framework_TestCase {
|
||||
function setUp() {
|
||||
OC_Group::clearBackends();
|
||||
OC_User::clearBackends();
|
||||
}
|
||||
|
||||
function testSingleBackend() {
|
||||
$userBackend = new \OC_User_Dummy();
|
||||
\OC_User::getManager()->registerBackend($userBackend);
|
||||
OC_Group::useBackend(new OC_Group_Dummy());
|
||||
|
||||
$group1=uniqid();
|
||||
$group2=uniqid();
|
||||
|
||||
$group1 = uniqid();
|
||||
$group2 = uniqid();
|
||||
OC_Group::createGroup($group1);
|
||||
OC_Group::createGroup($group2);
|
||||
|
||||
$user1=uniqid();
|
||||
$user2=uniqid();
|
||||
$user1 = uniqid();
|
||||
$user2 = uniqid();
|
||||
$userBackend->createUser($user1, '');
|
||||
$userBackend->createUser($user2, '');
|
||||
|
||||
$this->assertFalse(OC_Group::inGroup($user1, $group1));
|
||||
$this->assertFalse(OC_Group::inGroup($user2, $group1));
|
||||
$this->assertFalse(OC_Group::inGroup($user1, $group2));
|
||||
$this->assertFalse(OC_Group::inGroup($user2, $group2));
|
||||
|
||||
$this->assertTrue((bool)OC_Group::addToGroup($user1, $group1));
|
||||
$this->assertTrue(OC_Group::addToGroup($user1, $group1));
|
||||
|
||||
$this->assertTrue(OC_Group::inGroup($user1, $group1));
|
||||
$this->assertFalse(OC_Group::inGroup($user2, $group1));
|
||||
$this->assertFalse(OC_Group::inGroup($user1, $group2));
|
||||
$this->assertFalse(OC_Group::inGroup($user2, $group2));
|
||||
|
||||
$this->assertFalse((bool)OC_Group::addToGroup($user1, $group1));
|
||||
$this->assertTrue(OC_Group::addToGroup($user1, $group1));
|
||||
|
||||
$this->assertEquals(array($user1), OC_Group::usersInGroup($group1));
|
||||
$this->assertEquals(array(), OC_Group::usersInGroup($group2));
|
||||
|
@ -65,37 +70,37 @@ class Test_Group extends PHPUnit_Framework_TestCase {
|
|||
}
|
||||
|
||||
|
||||
public function testNoEmptyGIDs(){
|
||||
public function testNoEmptyGIDs() {
|
||||
OC_Group::useBackend(new OC_Group_Dummy());
|
||||
$emptyGroup = null;
|
||||
|
||||
$this->assertEquals(false, OC_Group::createGroup($emptyGroup));
|
||||
$this->assertFalse(OC_Group::createGroup($emptyGroup));
|
||||
}
|
||||
|
||||
|
||||
public function testNoGroupsTwice(){
|
||||
public function testNoGroupsTwice() {
|
||||
OC_Group::useBackend(new OC_Group_Dummy());
|
||||
$group = uniqid();
|
||||
OC_Group::createGroup($group);
|
||||
|
||||
$groupCopy = $group;
|
||||
|
||||
$this->assertEquals(false, OC_Group::createGroup($groupCopy));
|
||||
OC_Group::createGroup($groupCopy);
|
||||
$this->assertEquals(array($group), OC_Group::getGroups());
|
||||
}
|
||||
|
||||
|
||||
public function testDontDeleteAdminGroup(){
|
||||
public function testDontDeleteAdminGroup() {
|
||||
OC_Group::useBackend(new OC_Group_Dummy());
|
||||
$adminGroup = 'admin';
|
||||
OC_Group::createGroup($adminGroup);
|
||||
|
||||
$this->assertEquals(false, OC_Group::deleteGroup($adminGroup));
|
||||
$this->assertFalse(OC_Group::deleteGroup($adminGroup));
|
||||
$this->assertEquals(array($adminGroup), OC_Group::getGroups());
|
||||
}
|
||||
|
||||
|
||||
public function testDontAddUserToNonexistentGroup(){
|
||||
public function testDontAddUserToNonexistentGroup() {
|
||||
OC_Group::useBackend(new OC_Group_Dummy());
|
||||
$groupNonExistent = 'notExistent';
|
||||
$user = uniqid();
|
||||
|
@ -105,8 +110,11 @@ class Test_Group extends PHPUnit_Framework_TestCase {
|
|||
}
|
||||
|
||||
|
||||
public function testUsersInGroup(){
|
||||
public function testUsersInGroup() {
|
||||
OC_Group::useBackend(new OC_Group_Dummy());
|
||||
$userBackend = new \OC_User_Dummy();
|
||||
\OC_User::getManager()->registerBackend($userBackend);
|
||||
|
||||
$group1 = uniqid();
|
||||
$group2 = uniqid();
|
||||
$group3 = uniqid();
|
||||
|
@ -117,27 +125,32 @@ class Test_Group extends PHPUnit_Framework_TestCase {
|
|||
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)));
|
||||
OC_Group::usersInGroups(array($group1, $group2, $group3)));
|
||||
|
||||
// FIXME: needs more parameter variation
|
||||
}
|
||||
|
||||
|
||||
|
||||
function testMultiBackend() {
|
||||
$backend1=new OC_Group_Dummy();
|
||||
$backend2=new OC_Group_Dummy();
|
||||
$userBackend = new \OC_User_Dummy();
|
||||
\OC_User::getManager()->registerBackend($userBackend);
|
||||
$backend1 = new OC_Group_Dummy();
|
||||
$backend2 = new OC_Group_Dummy();
|
||||
OC_Group::useBackend($backend1);
|
||||
OC_Group::useBackend($backend2);
|
||||
|
||||
$group1=uniqid();
|
||||
$group2=uniqid();
|
||||
$group1 = uniqid();
|
||||
$group2 = uniqid();
|
||||
OC_Group::createGroup($group1);
|
||||
|
||||
//groups should be added to the first registered backend
|
||||
|
@ -154,20 +167,23 @@ class Test_Group extends PHPUnit_Framework_TestCase {
|
|||
$this->assertTrue(OC_Group::groupExists($group1));
|
||||
$this->assertTrue(OC_Group::groupExists($group2));
|
||||
|
||||
$user1=uniqid();
|
||||
$user2=uniqid();
|
||||
$user1 = uniqid();
|
||||
$user2 = uniqid();
|
||||
|
||||
$userBackend->createUser($user1, '');
|
||||
$userBackend->createUser($user2, '');
|
||||
|
||||
$this->assertFalse(OC_Group::inGroup($user1, $group1));
|
||||
$this->assertFalse(OC_Group::inGroup($user2, $group1));
|
||||
|
||||
|
||||
$this->assertTrue((bool)OC_Group::addToGroup($user1, $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));
|
||||
|
||||
$this->assertFalse((bool)OC_Group::addToGroup($user1, $group1));
|
||||
OC_Group::addToGroup($user1, $group1);
|
||||
|
||||
$this->assertEquals(array($user1), OC_Group::usersInGroup($group1));
|
||||
|
||||
|
|
Loading…
Reference in a new issue