Merge pull request #12031 from owncloud/app-manager
Add \OC\App\Manager to handle enabling/disabling appsremotes/origin/fix-10825
commit
36528c6ef6
@ -0,0 +1,138 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com> |
||||
* This file is licensed under the Affero General Public License version 3 or |
||||
* later. |
||||
* See the COPYING-README file. |
||||
*/ |
||||
|
||||
namespace OC\App; |
||||
|
||||
use OCP\App\IAppManager; |
||||
use OCP\IAppConfig; |
||||
use OCP\IGroupManager; |
||||
use OCP\IUserSession; |
||||
|
||||
class AppManager implements IAppManager { |
||||
/** |
||||
* @var \OCP\IUserSession |
||||
*/ |
||||
private $userSession; |
||||
|
||||
/** |
||||
* @var \OCP\IAppConfig |
||||
*/ |
||||
private $appConfig; |
||||
|
||||
/** |
||||
* @var \OCP\IGroupManager |
||||
*/ |
||||
private $groupManager; |
||||
|
||||
/** |
||||
* @var string[] $appId => $enabled |
||||
*/ |
||||
private $installedAppsCache; |
||||
|
||||
/** |
||||
* @param \OCP\IUserSession $userSession |
||||
* @param \OCP\IAppConfig $appConfig |
||||
* @param \OCP\IGroupManager $groupManager |
||||
*/ |
||||
public function __construct(IUserSession $userSession, IAppConfig $appConfig, IGroupManager $groupManager) { |
||||
$this->userSession = $userSession; |
||||
$this->appConfig = $appConfig; |
||||
$this->groupManager = $groupManager; |
||||
} |
||||
|
||||
/** |
||||
* @return string[] $appId => $enabled |
||||
*/ |
||||
private function getInstalledApps() { |
||||
if (!$this->installedAppsCache) { |
||||
$values = $this->appConfig->getValues(false, 'enabled'); |
||||
$this->installedAppsCache = array_filter($values, function ($value) { |
||||
return $value !== 'no'; |
||||
}); |
||||
ksort($this->installedAppsCache); |
||||
} |
||||
return $this->installedAppsCache; |
||||
} |
||||
|
||||
/** |
||||
* Check if an app is enabled for user |
||||
* |
||||
* @param string $appId |
||||
* @param \OCP\IUser $user (optional) if not defined, the currently logged in user will be used |
||||
* @return bool |
||||
*/ |
||||
public function isEnabledForUser($appId, $user = null) { |
||||
if (is_null($user)) { |
||||
$user = $this->userSession->getUser(); |
||||
} |
||||
$installedApps = $this->getInstalledApps(); |
||||
if (isset($installedApps[$appId])) { |
||||
$enabled = $installedApps[$appId]; |
||||
if ($enabled === 'yes') { |
||||
return true; |
||||
} elseif (is_null($user)) { |
||||
return false; |
||||
} else { |
||||
$groupIds = json_decode($enabled); |
||||
$userGroups = $this->groupManager->getUserGroupIds($user); |
||||
foreach ($userGroups as $groupId) { |
||||
if (array_search($groupId, $groupIds) !== false) { |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
} else { |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Check if an app is installed in the instance |
||||
* |
||||
* @param string $appId |
||||
* @return bool |
||||
*/ |
||||
public function isInstalled($appId) { |
||||
$installedApps = $this->getInstalledApps(); |
||||
return isset($installedApps[$appId]); |
||||
} |
||||
|
||||
/** |
||||
* Enable an app for every user |
||||
* |
||||
* @param string $appId |
||||
*/ |
||||
public function enableApp($appId) { |
||||
$this->appConfig->setValue($appId, 'enabled', 'yes'); |
||||
} |
||||
|
||||
/** |
||||
* Enable an app only for specific groups |
||||
* |
||||
* @param string $appId |
||||
* @param \OCP\IGroup[] $groups |
||||
*/ |
||||
public function enableAppForGroups($appId, $groups) { |
||||
$groupIds = array_map(function ($group) { |
||||
/** @var \OCP\IGroup $group */ |
||||
return $group->getGID(); |
||||
}, $groups); |
||||
$this->appConfig->setValue($appId, 'enabled', json_encode($groupIds)); |
||||
} |
||||
|
||||
/** |
||||
* Disable an app for every user |
||||
* |
||||
* @param string $appId |
||||
*/ |
||||
public function disableApp($appId) { |
||||
$this->appConfig->setValue($appId, 'enabled', 'no'); |
||||
} |
||||
} |
@ -0,0 +1,51 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com> |
||||
* This file is licensed under the Affero General Public License version 3 or |
||||
* later. |
||||
* See the COPYING-README file. |
||||
*/ |
||||
|
||||
namespace OCP\App; |
||||
|
||||
interface IAppManager { |
||||
/** |
||||
* Check if an app is enabled for user |
||||
* |
||||
* @param string $appId |
||||
* @param \OCP\IUser $user (optional) if not defined, the currently loggedin user will be used |
||||
* @return bool |
||||
*/ |
||||
public function isEnabledForUser($appId, $user = null); |
||||
|
||||
/** |
||||
* Check if an app is installed in the instance |
||||
* |
||||
* @param string $appId |
||||
* @return bool |
||||
*/ |
||||
public function isInstalled($appId); |
||||
|
||||
/** |
||||
* Enable an app for every user |
||||
* |
||||
* @param string $appId |
||||
*/ |
||||
public function enableApp($appId); |
||||
|
||||
/** |
||||
* Enable an app only for specific groups |
||||
* |
||||
* @param string $appId |
||||
* @param \OCP\IGroup[] $groups |
||||
*/ |
||||
public function enableAppForGroups($appId, $groups); |
||||
|
||||
/** |
||||
* Disable an app for every user |
||||
* |
||||
* @param string $appId |
||||
*/ |
||||
public function disableApp($appId); |
||||
} |
@ -0,0 +1,195 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com> |
||||
* This file is licensed under the Affero General Public License version 3 or |
||||
* later. |
||||
* See the COPYING-README file. |
||||
*/ |
||||
|
||||
namespace Test\App; |
||||
|
||||
use OC\Group\Group; |
||||
use OC\User\User; |
||||
|
||||
class Manager extends \PHPUnit_Framework_TestCase { |
||||
/** |
||||
* @return \OCP\IAppConfig | \PHPUnit_Framework_MockObject_MockObject |
||||
*/ |
||||
protected function getAppConfig() { |
||||
$appConfig = array(); |
||||
$config = $this->getMockBuilder('\OCP\IAppConfig') |
||||
->disableOriginalConstructor() |
||||
->getMock(); |
||||
|
||||
$config->expects($this->any()) |
||||
->method('getValue') |
||||
->will($this->returnCallback(function ($app, $key, $default) use (&$appConfig) { |
||||
return (isset($appConfig[$app]) and isset($appConfig[$app][$key])) ? $appConfig[$app][$key] : $default; |
||||
})); |
||||
$config->expects($this->any()) |
||||
->method('setValue') |
||||
->will($this->returnCallback(function ($app, $key, $value) use (&$appConfig) { |
||||
if (!isset($appConfig[$app])) { |
||||
$appConfig[$app] = array(); |
||||
} |
||||
$appConfig[$app][$key] = $value; |
||||
})); |
||||
$config->expects($this->any()) |
||||
->method('getValues') |
||||
->will($this->returnCallback(function ($app, $key) use (&$appConfig) { |
||||
if ($app) { |
||||
return $appConfig[$app]; |
||||
} else { |
||||
$values = array(); |
||||
foreach ($appConfig as $app => $appData) { |
||||
if (isset($appData[$key])) { |
||||
$values[$app] = $appData[$key]; |
||||
} |
||||
} |
||||
return $values; |
||||
} |
||||
})); |
||||
|
||||
return $config; |
||||
} |
||||
|
||||
public function testEnableApp() { |
||||
$userSession = $this->getMock('\OCP\IUserSession'); |
||||
$groupManager = $this->getMock('\OCP\IGroupManager'); |
||||
$appConfig = $this->getAppConfig(); |
||||
$manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); |
||||
$manager->enableApp('test'); |
||||
$this->assertEquals('yes', $appConfig->getValue('test', 'enabled', 'no')); |
||||
} |
||||
|
||||
public function testDisableApp() { |
||||
$userSession = $this->getMock('\OCP\IUserSession'); |
||||
$groupManager = $this->getMock('\OCP\IGroupManager'); |
||||
$appConfig = $this->getAppConfig(); |
||||
$manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); |
||||
$manager->disableApp('test'); |
||||
$this->assertEquals('no', $appConfig->getValue('test', 'enabled', 'no')); |
||||
} |
||||
|
||||
public function testEnableAppForGroups() { |
||||
$groups = array( |
||||
new Group('group1', array(), null), |
||||
new Group('group2', array(), null) |
||||
); |
||||
$groupManager = $this->getMock('\OCP\IGroupManager'); |
||||
$userSession = $this->getMock('\OCP\IUserSession'); |
||||
$appConfig = $this->getAppConfig(); |
||||
$manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); |
||||
$manager->enableAppForGroups('test', $groups); |
||||
$this->assertEquals('["group1","group2"]', $appConfig->getValue('test', 'enabled', 'no')); |
||||
} |
||||
|
||||
public function testIsInstalledEnabled() { |
||||
$userSession = $this->getMock('\OCP\IUserSession'); |
||||
$groupManager = $this->getMock('\OCP\IGroupManager'); |
||||
$appConfig = $this->getAppConfig(); |
||||
$manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); |
||||
$appConfig->setValue('test', 'enabled', 'yes'); |
||||
$this->assertTrue($manager->isInstalled('test')); |
||||
} |
||||
|
||||
public function testIsInstalledDisabled() { |
||||
$userSession = $this->getMock('\OCP\IUserSession'); |
||||
$groupManager = $this->getMock('\OCP\IGroupManager'); |
||||
$appConfig = $this->getAppConfig(); |
||||
$manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); |
||||
$appConfig->setValue('test', 'enabled', 'no'); |
||||
$this->assertFalse($manager->isInstalled('test')); |
||||
} |
||||
|
||||
public function testIsInstalledEnabledForGroups() { |
||||
$userSession = $this->getMock('\OCP\IUserSession'); |
||||
$groupManager = $this->getMock('\OCP\IGroupManager'); |
||||
$appConfig = $this->getAppConfig(); |
||||
$manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); |
||||
$appConfig->setValue('test', 'enabled', '["foo"]'); |
||||
$this->assertTrue($manager->isInstalled('test')); |
||||
} |
||||
|
||||
public function testIsEnabledForUserEnabled() { |
||||
$userSession = $this->getMock('\OCP\IUserSession'); |
||||
$groupManager = $this->getMock('\OCP\IGroupManager'); |
||||
$appConfig = $this->getAppConfig(); |
||||
$manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); |
||||
$appConfig->setValue('test', 'enabled', 'yes'); |
||||
$user = new User('user1', null); |
||||
$this->assertTrue($manager->isEnabledForUser('test', $user)); |
||||
} |
||||
|
||||
public function testIsEnabledForUserDisabled() { |
||||
$userSession = $this->getMock('\OCP\IUserSession'); |
||||
$groupManager = $this->getMock('\OCP\IGroupManager'); |
||||
$appConfig = $this->getAppConfig(); |
||||
$manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); |
||||
$appConfig->setValue('test', 'enabled', 'no'); |
||||
$user = new User('user1', null); |
||||
$this->assertFalse($manager->isEnabledForUser('test', $user)); |
||||
} |
||||
|
||||
public function testIsEnabledForUserEnabledForGroup() { |
||||
$userSession = $this->getMock('\OCP\IUserSession'); |
||||
$groupManager = $this->getMock('\OCP\IGroupManager'); |
||||
$user = new User('user1', null); |
||||
|
||||
$groupManager->expects($this->once()) |
||||
->method('getUserGroupIds') |
||||
->with($user) |
||||
->will($this->returnValue(array('foo', 'bar'))); |
||||
|
||||
$appConfig = $this->getAppConfig(); |
||||
$manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); |
||||
$appConfig->setValue('test', 'enabled', '["foo"]'); |
||||
$this->assertTrue($manager->isEnabledForUser('test', $user)); |
||||
} |
||||
|
||||
public function testIsEnabledForUserDisabledForGroup() { |
||||
$userSession = $this->getMock('\OCP\IUserSession'); |
||||
$groupManager = $this->getMock('\OCP\IGroupManager'); |
||||
$user = new User('user1', null); |
||||
|
||||
$groupManager->expects($this->once()) |
||||
->method('getUserGroupIds') |
||||
->with($user) |
||||
->will($this->returnValue(array('bar'))); |
||||
|
||||
$appConfig = $this->getAppConfig(); |
||||
$manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); |
||||
$appConfig->setValue('test', 'enabled', '["foo"]'); |
||||
$this->assertFalse($manager->isEnabledForUser('test', $user)); |
||||
} |
||||
|
||||
public function testIsEnabledForUserLoggedOut() { |
||||
$userSession = $this->getMock('\OCP\IUserSession'); |
||||
$groupManager = $this->getMock('\OCP\IGroupManager'); |
||||
|
||||
$appConfig = $this->getAppConfig(); |
||||
$manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); |
||||
$appConfig->setValue('test', 'enabled', '["foo"]'); |
||||
$this->assertFalse($manager->IsEnabledForUser('test')); |
||||
} |
||||
|
||||
public function testIsEnabledForUserLoggedIn() { |
||||
$userSession = $this->getMock('\OCP\IUserSession'); |
||||
$groupManager = $this->getMock('\OCP\IGroupManager'); |
||||
$user = new User('user1', null); |
||||
|
||||
$userSession->expects($this->once()) |
||||
->method('getUser') |
||||
->will($this->returnValue($user)); |
||||
$groupManager->expects($this->once()) |
||||
->method('getUserGroupIds') |
||||
->with($user) |
||||
->will($this->returnValue(array('foo', 'bar'))); |
||||
|
||||
$appConfig = $this->getAppConfig(); |
||||
$manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager); |
||||
$appConfig->setValue('test', 'enabled', '["foo"]'); |
||||
$this->assertTrue($manager->isEnabledForUser('test')); |
||||
} |
||||
} |
Loading…
Reference in new issue