add isAdmin and isInGroup methods for the group manager

This commit is contained in:
Bernhard Posselt 2014-12-16 20:33:13 +01:00
parent 0f535e3866
commit 6779bf113d
5 changed files with 117 additions and 4 deletions

View File

@ -196,8 +196,7 @@ class DIContainer extends SimpleContainer implements IAppContainer{
}
/**
* @deprecated use the groupmanager instead to find out if the user is in
* the admin group
* @deprecated use IGroupManager->isAdmin($userId)
* @return boolean
*/
function isAdminUser() {

View File

@ -170,7 +170,14 @@ class Manager extends PublicEmitter implements IGroupManager {
* @return \OC\Group\Group[]
*/
public function getUserGroups($user) {
$uid = $user->getUID();
return $this->getUserIdGroups($user->getUID());
}
/**
* @param string $uid the user id
* @return \OC\Group\Group[]
*/
public function getUserIdGroups($uid) {
if (isset($this->cachedUserGroups[$uid])) {
return $this->cachedUserGroups[$uid];
}
@ -185,6 +192,25 @@ class Manager extends PublicEmitter implements IGroupManager {
return $this->cachedUserGroups[$uid];
}
/**
* Checks if a userId is in the admin group
* @param string $userId
* @return bool if admin
*/
public function isAdmin($userId) {
return $this->isInGroup($userId, 'admin');
}
/**
* Checks if a userId is in a group
* @param string $userId
* @param group $group
* @return bool if in group
*/
public function isInGroup($userId, $group) {
return array_key_exists($group, $this->getUserIdGroups($userId));
}
/**
* get a list of group ids for a user
* @param \OC\User\User $user

View File

@ -63,6 +63,7 @@ interface IAppContainer extends IContainer {
function isLoggedIn();
/**
* @deprecated use IGroupManager->isAdmin($userId)
* @return boolean
* @deprecated use the groupmanager instead to find out if the user is in
* the admin group

View File

@ -80,4 +80,19 @@ interface IGroupManager {
* @return array an array of display names (value) and user ids (key)
*/
public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0);
/**
* Checks if a userId is in the admin group
* @param string $userId
* @return bool if admin
*/
public function isAdmin($userId);
/**
* Checks if a userId is in a group
* @param string $userId
* @param group $group
* @return bool if in group
*/
public function isInGroup($userId, $group);
}

View File

@ -304,6 +304,78 @@ class Manager extends \Test\TestCase {
$this->assertEquals('group1', $group1->getGID());
}
public function testInGroup() {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
*/
$backend = $this->getMock('\OC_Group_Database');
$backend->expects($this->once())
->method('getUserGroups')
->with('user1')
->will($this->returnValue(array('group1', 'admin', 'group2')));
$backend->expects($this->any())
->method('groupExists')
->will($this->returnValue(true));
/**
* @var \OC\User\Manager $userManager
*/
$userManager = $this->getMock('\OC\User\Manager');
$userBackend = $this->getMock('\OC_User_Backend');
$manager = new \OC\Group\Manager($userManager);
$manager->addBackend($backend);
$this->assertTrue($manager->isInGroup('user1', 'group1'));
}
public function testIsAdmin() {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
*/
$backend = $this->getMock('\OC_Group_Database');
$backend->expects($this->once())
->method('getUserGroups')
->with('user1')
->will($this->returnValue(array('group1', 'admin', 'group2')));
$backend->expects($this->any())
->method('groupExists')
->will($this->returnValue(true));
/**
* @var \OC\User\Manager $userManager
*/
$userManager = $this->getMock('\OC\User\Manager');
$userBackend = $this->getMock('\OC_User_Backend');
$manager = new \OC\Group\Manager($userManager);
$manager->addBackend($backend);
$this->assertTrue($manager->isAdmin('user1'));
}
public function testNotAdmin() {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend
*/
$backend = $this->getMock('\OC_Group_Database');
$backend->expects($this->once())
->method('getUserGroups')
->with('user1')
->will($this->returnValue(array('group1', 'group2')));
$backend->expects($this->any())
->method('groupExists')
->will($this->returnValue(true));
/**
* @var \OC\User\Manager $userManager
*/
$userManager = $this->getMock('\OC\User\Manager');
$userBackend = $this->getMock('\OC_User_Backend');
$manager = new \OC\Group\Manager($userManager);
$manager->addBackend($backend);
$this->assertFalse($manager->isAdmin('user1'));
}
public function testGetUserGroupsMultipleBackends() {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1