Merge pull request #12892 from owncloud/is-addmin

Add isAdmin and isInGroup methods for the group manager
This commit is contained in:
Morris Jobke 2014-12-17 21:40:26 +01:00
commit 3d006207d1
8 changed files with 178 additions and 5 deletions

View File

@ -188,6 +188,7 @@ class DIContainer extends SimpleContainer implements IAppContainer{
}
/**
* @deprecated use IUserSession->isLoggedIn()
* @return boolean
*/
function isLoggedIn() {
@ -195,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];
}
@ -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

View File

@ -137,6 +137,15 @@ class Session implements IUserSession, Emitter {
}
}
/**
* Checks wether the user is logged in
*
* @return bool if logged in
*/
public function isLoggedIn() {
return $this->getUser() !== null;
}
/**
* set the login name
*

View File

@ -31,7 +31,7 @@ use OCP\IContainer;
*
* This container interface provides short cuts for app developers to access predefined app service.
*/
interface IAppContainer extends IContainer{
interface IAppContainer extends IContainer {
/**
* used to return the appname of the set application
@ -57,11 +57,13 @@ interface IAppContainer extends IContainer{
function registerMiddleWare($middleWare);
/**
* @deprecated use IUserSession->isLoggedIn()
* @return boolean
*/
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

@ -3,7 +3,9 @@
* ownCloud
*
* @author Bart Visscher
* @author Bernhard Posselt
* @copyright 2013 Bart Visscher bartv@thisnet.nl
* @copyright 2014 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
@ -62,4 +64,11 @@ interface IUserSession {
* @return \OCP\IUser
*/
public function getUser();
/**
* Checks wether the user is logged in
*
* @return bool if logged in
*/
public function isLoggedIn();
}

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

View File

@ -34,6 +34,46 @@ class Session extends \Test\TestCase {
$this->assertEquals('foo', $user->getUID());
}
public function testIsLoggedIn() {
$session = $this->getMock('\OC\Session\Memory', array(), array(''));
$session->expects($this->once())
->method('get')
->with('user_id')
->will($this->returnValue('foo'));
$backend = $this->getMock('OC_User_Dummy');
$backend->expects($this->once())
->method('userExists')
->with('foo')
->will($this->returnValue(true));
$manager = new \OC\User\Manager();
$manager->registerBackend($backend);
$userSession = new \OC\User\Session($manager, $session);
$isLoggedIn = $userSession->isLoggedIn();
$this->assertTrue($isLoggedIn);
}
public function testNotLoggedIn() {
$session = $this->getMock('\OC\Session\Memory', array(), array(''));
$session->expects($this->once())
->method('get')
->with('user_id')
->will($this->returnValue(null));
$backend = $this->getMock('OC_User_Dummy');
$backend->expects($this->never())
->method('userExists');
$manager = new \OC\User\Manager();
$manager->registerBackend($backend);
$userSession = new \OC\User\Session($manager, $session);
$isLoggedIn = $userSession->isLoggedIn();
$this->assertFalse($isLoggedIn);
}
public function testSetUser() {
$session = $this->getMock('\OC\Session\Memory', array(), array(''));
$session->expects($this->once())