Commit fe8d45e3 authored by Thomas Müller's avatar Thomas Müller Committed by GitHub
Browse files

Drop apps management - use the market app. In addition there is an admin...

Drop apps management - use the market app. In addition there is an admin section for local app mgmt (#26763)
parent e4070574
......@@ -68,10 +68,7 @@ API::register('delete', '/cloud/groups/{groupid}', [$groups, 'deleteGroup'], 'pr
API::register('get', '/cloud/groups/{groupid}/subadmins', [$groups, 'getSubAdminsOfGroup'], 'provisioning_api', API::ADMIN_AUTH);
// Apps
$apps = new Apps(
\OC::$server->getAppManager(),
\OC::$server->getOcsClient()
);
$apps = new Apps(\OC::$server->getAppManager());
API::register('get', '/cloud/apps', [$apps, 'getApps'], 'provisioning_api', API::ADMIN_AUTH);
API::register('get', '/cloud/apps/{appid}', [$apps, 'getAppInfo'], 'provisioning_api', API::ADMIN_AUTH);
API::register('post', '/cloud/apps/{appid}', [$apps, 'enable'], 'provisioning_api', API::ADMIN_AUTH);
......
......@@ -25,23 +25,18 @@
namespace OCA\Provisioning_API;
use OC\OCSClient;
use \OC_OCS_Result;
use \OC_App;
class Apps {
/** @var \OCP\App\IAppManager */
private $appManager;
/** @var OCSClient */
private $ocsClient;
/**
* @param \OCP\App\IAppManager $appManager
*/
public function __construct(\OCP\App\IAppManager $appManager,
OCSClient $ocsClient) {
public function __construct(\OCP\App\IAppManager $appManager) {
$this->appManager = $appManager;
$this->ocsClient = $ocsClient;
}
/**
......@@ -49,7 +44,7 @@ class Apps {
* @return OC_OCS_Result
*/
public function getApps($parameters) {
$apps = OC_App::listAllApps(false, true, $this->ocsClient);
$apps = OC_App::listAllApps(false, true);
$list = [];
foreach($apps as $app) {
$list[] = $app['id'];
......
......@@ -27,7 +27,6 @@
namespace OCA\Provisioning_API\Tests;
use OC\OCSClient;
use OCA\Provisioning_API\Apps;
use OCP\API;
use OCP\App\IAppManager;
......@@ -47,8 +46,6 @@ class AppsTest extends TestCase {
private $api;
/** @var IUserSession */
private $userSession;
/** @var OCSClient|\PHPUnit_Framework_MockObject_MockObject */
private $ocsClient;
protected function setUp() {
parent::setUp();
......@@ -56,11 +53,7 @@ class AppsTest extends TestCase {
$this->appManager = \OC::$server->getAppManager();
$this->groupManager = \OC::$server->getGroupManager();
$this->userSession = \OC::$server->getUserSession();
$this->ocsClient = $this->getMockBuilder('OC\OCSClient')
->disableOriginalConstructor()
->getMock();
$this->api = new Apps($this->appManager, $this->ocsClient);
$this->api = new Apps($this->appManager);
}
public function testGetAppInfo() {
......@@ -77,10 +70,6 @@ class AppsTest extends TestCase {
}
public function testGetApps() {
$this->ocsClient
->expects($this->any())
->method($this->anything())
->will($this->returnValue(null));
$user = $this->generateUsers();
$this->groupManager->get('admin')->addUser($user);
$this->userSession->setUser($user);
......@@ -89,7 +78,7 @@ class AppsTest extends TestCase {
$this->assertTrue($result->succeeded());
$data = $result->getData();
$this->assertEquals(count(\OC_App::listAllApps(false, true, $this->ocsClient)), count($data['apps']));
$this->assertEquals(count(\OC_App::listAllApps(false, true)), count($data['apps']));
}
public function testGetAppsEnabled() {
......@@ -101,15 +90,11 @@ class AppsTest extends TestCase {
}
public function testGetAppsDisabled() {
$this->ocsClient
->expects($this->any())
->method($this->anything())
->will($this->returnValue(null));
$_GET['filter'] = 'disabled';
$result = $this->api->getApps(['filter' => 'disabled']);
$this->assertTrue($result->succeeded());
$data = $result->getData();
$apps = \OC_App::listAllApps(false, true, $this->ocsClient);
$apps = \OC_App::listAllApps(false, true);
$list = [];
foreach($apps as $app) {
$list[] = $app['id'];
......
......@@ -81,7 +81,6 @@ class BackgroundJob extends TimedJob {
protected function run($argument) {
$this->checkCoreUpdate();
$this->checkAppUpdates();
}
/**
......@@ -102,20 +101,6 @@ class BackgroundJob extends TimedJob {
}
}
/**
* Check all installed apps for updates
*/
protected function checkAppUpdates() {
$apps = $this->appManager->getInstalledApps();
foreach ($apps as $app) {
$update = $this->isUpdateAvailable($app);
if ($update !== false) {
$url = $this->urlGenerator->linkToRouteAbsolute('settings.AppSettings.viewApps') . '#app-' . $app;
$this->createNotifications($app, $update, $url);
}
}
}
/**
* Create notifications for this app version
*
......@@ -202,12 +187,4 @@ class BackgroundJob extends TimedJob {
protected function getChannel() {
return \OC_Util::getChannel();
}
/**
* @param string $app
* @return string|false
*/
protected function isUpdateAvailable($app) {
return Installer::isUpdateAvailable($app);
}
}
......@@ -88,15 +88,10 @@ class BackgroundJobTest extends TestCase {
}
public function testRun() {
$job = $this->getJob([
'checkCoreUpdate',
'checkAppUpdates',
]);
$job = $this->getJob(['checkCoreUpdate']);
$job->expects($this->once())
->method('checkCoreUpdate');
$job->expects($this->once())
->method('checkAppUpdates');
$this->invokePrivate($job, 'run', [null]);
}
......@@ -174,54 +169,6 @@ class BackgroundJobTest extends TestCase {
$this->invokePrivate($job, 'checkCoreUpdate');
}
public function dataCheckAppUpdates() {
return [
[
['app1', 'app2'],
[
['app1', false],
['app2', '1.9.2'],
],
[
['app2', '1.9.2', 'apps-url#app-app2'],
],
],
];
}
/**
* @dataProvider dataCheckAppUpdates
*
* @param string[] $apps
* @param array $isUpdateAvailable
* @param array $notifications
*/
public function testCheckAppUpdates(array $apps, array $isUpdateAvailable, array $notifications) {
$job = $this->getJob([
'isUpdateAvailable',
'createNotifications',
]);
$this->appManager->expects($this->once())
->method('getInstalledApps')
->willReturn($apps);
$job->expects($this->exactly(sizeof($apps)))
->method('isUpdateAvailable')
->willReturnMap($isUpdateAvailable);
$this->urlGenerator->expects($this->exactly(sizeof($notifications)))
->method('linkToRouteAbsolute')
->with('settings.AppSettings.viewApps')
->willReturn('apps-url');
$mockedMethod = $job->expects($this->exactly(sizeof($notifications)))
->method('createNotifications');
call_user_func_array([$mockedMethod, 'withConsecutive'], $notifications);
$this->invokePrivate($job, 'checkAppUpdates');
}
public function dataCreateNotifications() {
return [
['app1', '1.0.0', 'link1', '1.0.0', false, false, null, null],
......
......@@ -122,22 +122,6 @@
</a>
</li>
<?php endforeach; ?>
<?php
/* show "More apps" link to app administration directly in app navigation, as last entry */
if(OC_User::isAdminUser(OC_User::getUser())):
?>
<li id="apps-management">
<a href="<?php print_unescaped(\OC::$server->getURLGenerator()->linkToRoute('settings.AppSettings.viewApps')); ?>" tabindex="4"
<?php if( $_['appsmanagement_active'] ): ?> class="active"<?php endif; ?>>
<img class="app-icon" alt="" src="<?php print_unescaped(image_path('settings', 'apps.svg')); ?>">
<div class="icon-loading-dark" style="display:none;"></div>
<span>
<?php p($l->t('Apps')); ?>
</span>
</a>
</li>
<?php endif; ?>
</ul>
</div>
</div></nav>
......
......@@ -238,36 +238,6 @@ class Installer {
return OC_App::updateApp($info['id']);
}
/**
* update an app by it's id
*
* @param integer $ocsId
* @return bool
* @throws \Exception
*/
public static function updateAppByOCSId($ocsId) {
$ocsClient = new OCSClient(
\OC::$server->getHTTPClientService(),
\OC::$server->getConfig(),
\OC::$server->getLogger()
);
$appData = $ocsClient->getApplication($ocsId, \OCP\Util::getVersion());
$download = $ocsClient->getApplicationDownload($ocsId, \OCP\Util::getVersion());
if (isset($download['downloadlink']) && trim($download['downloadlink']) !== '') {
$download['downloadlink'] = str_replace(' ', '%20', $download['downloadlink']);
$info = [
'source' => 'http',
'href' => $download['downloadlink'],
'appdata' => $appData
];
} else {
throw new \Exception('Could not fetch app info!');
}
return self::updateApp($info);
}
/**
* @param array $data
* @return array
......@@ -417,52 +387,6 @@ class Installer {
return $info;
}
/**
* Check if an update for the app is available
* @param string $app
* @return string|false false or the version number of the update
*
* The function will check if an update for a version is available
*/
public static function isUpdateAvailable( $app ) {
static $isInstanceReadyForUpdates = null;
if ($isInstanceReadyForUpdates === null) {
$installPath = OC_App::getInstallPath();
if ($installPath === false || $installPath === null) {
$isInstanceReadyForUpdates = false;
} else {
$isInstanceReadyForUpdates = true;
}
}
if ($isInstanceReadyForUpdates === false) {
return false;
}
$ocsid=\OC::$server->getAppConfig()->getValue( $app, 'ocsid', '');
if($ocsid<>'') {
$ocsClient = new OCSClient(
\OC::$server->getHTTPClientService(),
\OC::$server->getConfig(),
\OC::$server->getLogger()
);
$ocsdata = $ocsClient->getApplication($ocsid, \OCP\Util::getVersion());
$ocsversion= (string) $ocsdata['version'];
$currentversion=OC_App::getAppVersion($app);
if (version_compare($ocsversion, $currentversion, '>')) {
return($ocsversion);
}else{
return false;
}
}else{
return false;
}
}
/**
* Check if app is already downloaded
* @param string $name name of the application to remove
......
<?php
/**
* @author Bart Visscher <bartv@thisnet.nl>
* @author Brice Maron <brice@bmaron.net>
* @author Felix Moeller <mail@felixmoeller.de>
* @author Frank Karlitschek <frank@karlitschek.de>
* @author Jarrett <JetUni@users.noreply.github.com>
* @author Joas Schilling <coding@schilljs.com>
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
* @author Kamil Domanski <kdomanski@kdemail.net>
* @author Lukas Reschke <lukas@statuscode.ch>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin McCorkell <robin@mccorkell.me.uk>
* @author Sam Tuke <mail@samtuke.com>
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @copyright Copyright (c) 2016, ownCloud GmbH.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\ILogger;
/**
* Class OCSClient is a class for communication with the ownCloud appstore
*
* @package OC
*/
class OCSClient {
/** @var IClientService */
private $httpClientService;
/** @var IConfig */
private $config;
/** @var ILogger */
private $logger;
/**
* @param IClientService $httpClientService
* @param IConfig $config
* @param ILogger $logger
*/
public function __construct(IClientService $httpClientService,
IConfig $config,
ILogger $logger) {
$this->httpClientService = $httpClientService;
$this->config = $config;
$this->logger = $logger;
}
/**
* Returns whether the AppStore is enabled (i.e. because the AppStore is disabled for EE)
*
* @return bool
*/
public function isAppStoreEnabled() {
// For a regular edition default to true, all others default to false
$default = false;
if (\OC_Util::getEditionString() === '') {
$default = true;
}
return $this->config->getSystemValue('appstoreenabled', $default) === true;
}
/**
* Get the url of the OCS AppStore server.
*
* @return string of the AppStore server
*/
private function getAppStoreUrl() {
return $this->config->getSystemValue('appstoreurl', 'https://api.owncloud.com/v1');
}
/**
* @param string $body
* @param string $action
* @return null|\SimpleXMLElement
*/
private function loadData($body, $action) {
$loadEntities = libxml_disable_entity_loader(true);
$data = @simplexml_load_string($body);
libxml_disable_entity_loader($loadEntities);
if($data === false) {
libxml_clear_errors();
$this->logger->error(
sprintf('Could not get %s, content was no valid XML', $action),
[
'app' => 'core',
]
);
return null;
}
return $data;
}
/**
* Get all the categories from the OCS server
*
* @param array $targetVersion The target ownCloud version
* @return array|null an array of category ids or null
* @note returns NULL if config value appstoreenabled is set to false
* This function returns a list of all the application categories on the OCS server
*/
public function getCategories(array $targetVersion) {
if (!$this->isAppStoreEnabled()) {
return null;
}
$client = $this->httpClientService->newClient();
try {
$response = $client->get(
$this->getAppStoreUrl() . '/content/categories',
[
'timeout' => 20,
'query' => [
'version' => implode('x', $targetVersion),
],
]
);
} catch(\Exception $e) {
$this->logger->error(
sprintf('Could not get categories: %s', $e->getMessage()),
[
'app' => 'core',
]
);
return null;
}
$data = $this->loadData($response->getBody(), 'categories');
if($data === null) {
return null;
}
$tmp = $data->data;
$cats = [];
foreach ($tmp->category as $value) {
$id = (int)$value->id;
$name = (string)$value->name;
$cats[$id] = $name;
}
return $cats;
}
/**
* Get all the applications from the OCS server
* @param array $categories
* @param int $page
* @param string $filter
* @param array $targetVersion The target ownCloud version
* @return array An array of application data
*/
public function getApplications(array $categories, $page, $filter, array $targetVersion) {
if (!$this->isAppStoreEnabled()) {
return [];
}
$client = $this->httpClientService->newClient();
try {
$response = $client->get(
$this->getAppStoreUrl() . '/content/data',
[
'timeout' => 20,
'query' => [
'version' => implode('x', $targetVersion),
'filter' => $filter,
'categories' => implode('x', $categories),
'sortmode' => 'new',
'page' => $page,
'pagesize' => 100,
'approved' => $filter
],
]
);
} catch(\Exception $e) {
$this->logger->error(
sprintf('Could not get applications: %s', $e->getMessage()),
[
'app' => 'core',
]
);
return [];
}
$data = $this->loadData($response->getBody(), 'applications');
if($data === null) {
return [];
}
$tmp = $data->data->content;
$tmpCount = count($tmp);
$apps = [];
for ($i = 0; $i < $tmpCount; $i++) {
$app = [];
$app['id'] = (string)$tmp[$i]->id;
$app['name'] = (string)$tmp[$i]->name;
$app['label'] = (string)$tmp[$i]->label;
$app['version'] = (string)$tmp[$i]->version;
$app['type'] = (string)$tmp[$i]->typeid;
$app['typename'] = (string)$tmp[$i]->typename;
$app['personid'] = (string)$tmp[$i]->personid;
$app['profilepage'] = (string)$tmp[$i]->profilepage;
$app['license'] = (string)$tmp[$i]->license;
$app['detailpage'] = (string)$tmp[$i]->detailpage;
$app['preview'] = (string)$tmp[$i]->smallpreviewpic1;
$app['preview-full'] = (string)$tmp[$i]->previewpic1;
$app['changed'] = strtotime($tmp[$i]->changed);
$app['description'] = (string)$tmp[$i]->description;
$app['score'] = (string)$tmp[$i]->score;
$app['downloads'] = (int)$tmp[$i]->downloads;
$app['level'] = (int)$tmp[$i]->approved;
$apps[] = $app;
}
return $apps;
}
/**
* Get an the applications from the OCS server
*
* @param string $id
* @param array $targetVersion The target ownCloud version
* @return array|null an array of application data or null
*
* This function returns an applications from the OCS server
*/
public function getApplication($id, array $targetVersion) {
if (!$this->isAppStoreEnabled()) {
return null;
}
$client = $this->httpClientService->newClient();
try {
$response = $client->get(
$this->getAppStoreUrl() . '/content/data/' . urlencode($id),
[
'timeout' => 20,
'query' => [
'version' => implode('x', $targetVersion),
],
]
);
} catch(\Exception $e) {
$this->logger->error(
sprintf('Could not get application: %s', $e->getMessage()),
[