From e3a08584440b071e1caae7c7ff4f404c38b8794b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 14 Jan 2016 15:23:07 +0100 Subject: [PATCH] Check whether the app can be enabled for groups --- lib/private/app/appmanager.php | 21 ++++++++ tests/lib/app/manager.php | 93 ++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) diff --git a/lib/private/app/appmanager.php b/lib/private/app/appmanager.php index 7cb945784b..bf07a2ef54 100644 --- a/lib/private/app/appmanager.php +++ b/lib/private/app/appmanager.php @@ -35,6 +35,18 @@ use OCP\IUserSession; class AppManager implements IAppManager { + /** + * Apps with these types can not be enabled for certain groups only + * @var string[] + */ + protected $protectedAppTypes = [ + 'filesystem', + 'prelogin', + 'authentication', + 'logging', + 'prevent_group_restriction', + ]; + /** @var \OCP\IUserSession */ private $userSession; @@ -197,8 +209,17 @@ class AppManager implements IAppManager { * * @param string $appId * @param \OCP\IGroup[] $groups + * @throws \Exception if app can't be enabled for groups */ public function enableAppForGroups($appId, $groups) { + $info = $this->getAppInfo($appId); + if (!empty($info['types'])) { + $protectedTypes = array_intersect($this->protectedAppTypes, $info['types']); + if (!empty($protectedTypes)) { + throw new \Exception("$appId can't be enabled for groups."); + } + } + $groupIds = array_map(function ($group) { /** @var \OCP\IGroup $group */ return $group->getGID(); diff --git a/tests/lib/app/manager.php b/tests/lib/app/manager.php index a3e55c6b89..f82f1049ce 100644 --- a/tests/lib/app/manager.php +++ b/tests/lib/app/manager.php @@ -13,6 +13,12 @@ use OC\Group\Group; use OC\User\User; use Test\TestCase; +/** + * Class Manager + * + * @package Test\App + * @group DB + */ class Manager extends TestCase { /** * @return \OCP\IAppConfig | \PHPUnit_Framework_MockObject_MockObject @@ -116,6 +122,93 @@ class Manager extends TestCase { $this->assertEquals('["group1","group2"]', $this->appConfig->getValue('test', 'enabled', 'no')); } + public function dataEnableAppForGroupsAllowedTypes() { + return [ + [[]], + [[ + 'types' => [], + ]], + [[ + 'types' => ['nickvergessen'], + ]], + ]; + } + + /** + * @dataProvider dataEnableAppForGroupsAllowedTypes + * + * @param array $appInfo + */ + public function testEnableAppForGroupsAllowedTypes(array $appInfo) { + $groups = array( + new Group('group1', array(), null), + new Group('group2', array(), null) + ); + $this->expectClearCache(); + + /** @var \OC\App\AppManager|\PHPUnit_Framework_MockObject_MockObject $manager */ + $manager = $this->getMockBuilder('OC\App\AppManager') + ->setConstructorArgs([ + $this->userSession, $this->appConfig, $this->groupManager, $this->cacheFactory + ]) + ->setMethods([ + 'getAppInfo' + ]) + ->getMock(); + + $manager->expects($this->once()) + ->method('getAppInfo') + ->with('test') + ->willReturn($appInfo); + + $manager->enableAppForGroups('test', $groups); + $this->assertEquals('["group1","group2"]', $this->appConfig->getValue('test', 'enabled', 'no')); + } + + public function dataEnableAppForGroupsForbiddenTypes() { + return [ + ['filesystem'], + ['prelogin'], + ['authentication'], + ['logging'], + ['prevent_group_restriction'], + ]; + } + + /** + * @dataProvider dataEnableAppForGroupsForbiddenTypes + * + * @param string $type + * + * @expectedException \Exception + * @expectedExceptionMessage test can't be enabled for groups. + */ + public function testEnableAppForGroupsForbiddenTypes($type) { + $groups = array( + new Group('group1', array(), null), + new Group('group2', array(), null) + ); + + /** @var \OC\App\AppManager|\PHPUnit_Framework_MockObject_MockObject $manager */ + $manager = $this->getMockBuilder('OC\App\AppManager') + ->setConstructorArgs([ + $this->userSession, $this->appConfig, $this->groupManager, $this->cacheFactory + ]) + ->setMethods([ + 'getAppInfo' + ]) + ->getMock(); + + $manager->expects($this->once()) + ->method('getAppInfo') + ->with('test') + ->willReturn([ + 'types' => [$type], + ]); + + $manager->enableAppForGroups('test', $groups); + } + public function testIsInstalledEnabled() { $this->appConfig->setValue('test', 'enabled', 'yes'); $this->assertTrue($this->manager->isInstalled('test'));