diff options
-rw-r--r-- | lib/private/appframework/dependencyinjection/dicontainer.php | 3 | ||||
-rw-r--r-- | lib/private/group/manager.php | 30 | ||||
-rw-r--r-- | lib/public/appframework/iappcontainer.php | 1 | ||||
-rw-r--r-- | lib/public/igroupmanager.php | 15 | ||||
-rw-r--r-- | tests/lib/group/manager.php | 72 |
5 files changed, 117 insertions, 4 deletions
diff --git a/lib/private/appframework/dependencyinjection/dicontainer.php b/lib/private/appframework/dependencyinjection/dicontainer.php index faab40ce246..dc57ef4c167 100644 --- a/lib/private/appframework/dependencyinjection/dicontainer.php +++ b/lib/private/appframework/dependencyinjection/dicontainer.php @@ -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() { diff --git a/lib/private/group/manager.php b/lib/private/group/manager.php index be7bf972693..8dcf14fc1d2 100644 --- a/lib/private/group/manager.php +++ b/lib/private/group/manager.php @@ -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]; } @@ -184,7 +191,26 @@ class Manager extends PublicEmitter implements IGroupManager { $this->cachedUserGroups[$uid] = $groups; 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 diff --git a/lib/public/appframework/iappcontainer.php b/lib/public/appframework/iappcontainer.php index 2270b17c5b7..cb75bf4026c 100644 --- a/lib/public/appframework/iappcontainer.php +++ b/lib/public/appframework/iappcontainer.php @@ -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 diff --git a/lib/public/igroupmanager.php b/lib/public/igroupmanager.php index dc69044c490..8f468574c6b 100644 --- a/lib/public/igroupmanager.php +++ b/lib/public/igroupmanager.php @@ -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); } diff --git a/tests/lib/group/manager.php b/tests/lib/group/manager.php index f72ea8e912f..e3462caf806 100644 --- a/tests/lib/group/manager.php +++ b/tests/lib/group/manager.php @@ -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 |