From abc675d87e6ffc49fad608d5b2e8d280e385cc61 Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Tue, 9 Feb 2016 13:06:48 +0100 Subject: [PATCH] Move update notification code into app Moves the update notification code in a single app. This is required since we want to use SSO for the new updater and for this have some code running in ownCloud as well (and we don't want that in core neccessarily). This app can provide that in the future, right now it's only the update notification itself. Will continue working on the SSO right away but wanted to keep the PR small. Furthermore also makes some more code unit-testable... --- apps/updatenotification/appinfo/app.php | 39 +++++++++ apps/updatenotification/appinfo/info.xml | 13 +++ .../updatenotification/js/notification.js | 4 +- apps/updatenotification/lib/updatechecker.php | 67 +++++++++++++++ .../tests/UpdateCheckerTest.php | 86 +++++++++++++++++++ .../features/provisioning-v1.feature | 1 + core/shipped.json | 2 +- core/templates/layout.user.php | 6 +- lib/private/templatelayout.php | 25 ------ 9 files changed, 210 insertions(+), 33 deletions(-) create mode 100644 apps/updatenotification/appinfo/app.php create mode 100644 apps/updatenotification/appinfo/info.xml rename core/js/update-notification.js => apps/updatenotification/js/notification.js (88%) create mode 100644 apps/updatenotification/lib/updatechecker.php create mode 100644 apps/updatenotification/tests/UpdateCheckerTest.php diff --git a/apps/updatenotification/appinfo/app.php b/apps/updatenotification/appinfo/app.php new file mode 100644 index 0000000000..d5e973be52 --- /dev/null +++ b/apps/updatenotification/appinfo/app.php @@ -0,0 +1,39 @@ + + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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 + * + */ + +if(\OC::$server->getConfig()->getSystemValue('updatechecker', true) === true) { + $updater = new \OC\Updater( + \OC::$server->getHTTPHelper(), + \OC::$server->getConfig(), + \OC::$server->getIntegrityCodeChecker() + ); + $updateChecker = new \OCA\UpdateNotification\UpdateChecker( + $updater + ); + + $userObject = \OC::$server->getUserSession()->getUser(); + if($userObject !== null) { + if(\OC::$server->getGroupManager()->isAdmin($userObject->getUID()) && $updateChecker->getUpdateState() !== []) { + \OCP\Util::addScript('updatenotification', 'notification'); + OC_Hook::connect('\OCP\Config', 'js', $updateChecker, 'getJavaScript'); + } + } +} diff --git a/apps/updatenotification/appinfo/info.xml b/apps/updatenotification/appinfo/info.xml new file mode 100644 index 0000000000..0bfdd861a2 --- /dev/null +++ b/apps/updatenotification/appinfo/info.xml @@ -0,0 +1,13 @@ + + + updatenotification + Update notification + Displays update notifications for ownCloud. + AGPL + Lukas Reschke + 0.1.0 + + + + + diff --git a/core/js/update-notification.js b/apps/updatenotification/js/notification.js similarity index 88% rename from core/js/update-notification.js rename to apps/updatenotification/js/notification.js index 42baa7f4c2..9d22bcb230 100644 --- a/core/js/update-notification.js +++ b/apps/updatenotification/js/notification.js @@ -15,8 +15,8 @@ */ $(document).ready(function(){ var head = $('html > head'), - version = head.data('update-version'), - docLink = head.data('update-link'), + version = oc_updateState.updateVersion, + docLink = oc_updateState.updateLink, text = t('core', '{version} is available. Get more information on how to update.', {version: version}), element = $('').attr('href', docLink).text(text); diff --git a/apps/updatenotification/lib/updatechecker.php b/apps/updatenotification/lib/updatechecker.php new file mode 100644 index 0000000000..965e21617e --- /dev/null +++ b/apps/updatenotification/lib/updatechecker.php @@ -0,0 +1,67 @@ + + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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 + * + */ + +namespace OCA\UpdateNotification; + +use OC\Updater; + +class UpdateChecker { + /** @var Updater */ + private $updater; + + /** + * @param Updater $updater + */ + public function __construct(Updater $updater) { + $this->updater = $updater; + } + + /** + * @return array + */ + public function getUpdateState() { + $data = $this->updater->check(); + $result = []; + + if(isset($data['version']) && $data['version'] !== '' && $data['version'] !== []) { + $result['updateAvailable'] = true; + $result['updateVersion'] = $data['versionstring']; + if(substr($data['web'], 0, 8) === 'https://') { + $result['updateLink'] = $data['web']; + } + + return $result; + } + + return []; + } + + /** + * @param array $data + */ + public function getJavaScript(array $data) { + $data['array']['oc_updateState'] = json_encode([ + 'updateAvailable' => true, + 'updateVersion' => $this->getUpdateState()['updateVersion'], + 'updateLink' => isset($this->getUpdateState()['updateLink']) ? $this->getUpdateState()['updateLink'] : '', + ]); + } +} diff --git a/apps/updatenotification/tests/UpdateCheckerTest.php b/apps/updatenotification/tests/UpdateCheckerTest.php new file mode 100644 index 0000000000..9591758c4c --- /dev/null +++ b/apps/updatenotification/tests/UpdateCheckerTest.php @@ -0,0 +1,86 @@ + + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @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 + * + */ + +namespace OCA\UpdateNotification\Tests; + +use OC\Updater; +use OCA\UpdateNotification\UpdateChecker; +use Test\TestCase; + +class UpdateCheckerTest extends TestCase { + /** @var Updater */ + private $updater; + /** @var UpdateChecker */ + private $updateChecker; + + public function setUp() { + parent::setUp(); + + $this->updater = $this->getMockBuilder('\OC\Updater') + ->disableOriginalConstructor()->getMock(); + $this->updateChecker = new UpdateChecker($this->updater); + } + + public function testGetUpdateStateWithUpdateAndInvalidLink() { + $this->updater + ->expects($this->once()) + ->method('check') + ->willReturn([ + 'version' => 123, + 'versionstring' => 'ownCloud 123', + 'web'=> 'javascript:alert(1)', + ]); + + $expected = [ + 'updateAvailable' => true, + 'updateVersion' => 'ownCloud 123', + ]; + $this->assertSame($expected, $this->updateChecker->getUpdateState()); + } + + public function testGetUpdateStateWithUpdateAndValidLink() { + $this->updater + ->expects($this->once()) + ->method('check') + ->willReturn([ + 'version' => 123, + 'versionstring' => 'ownCloud 123', + 'web'=> 'https://owncloud.org/myUrl', + ]); + + $expected = [ + 'updateAvailable' => true, + 'updateVersion' => 'ownCloud 123', + 'updateLink' => 'https://owncloud.org/myUrl', + ]; + $this->assertSame($expected, $this->updateChecker->getUpdateState()); + } + + public function testGetUpdateStateWithoutUpdate() { + $this->updater + ->expects($this->once()) + ->method('check') + ->willReturn([]); + + $expected = []; + $this->assertSame($expected, $this->updateChecker->getUpdateState()); + } +} diff --git a/build/integration/features/provisioning-v1.feature b/build/integration/features/provisioning-v1.feature index af177b713d..04a706f387 100644 --- a/build/integration/features/provisioning-v1.feature +++ b/build/integration/features/provisioning-v1.feature @@ -291,6 +291,7 @@ Feature: provisioning | files_versions | | provisioning_api | | systemtags | + | updatenotification | Scenario: get app info Given As an "admin" diff --git a/core/shipped.json b/core/shipped.json index b74f2f28c4..d15f3ba3ca 100644 --- a/core/shipped.json +++ b/core/shipped.json @@ -31,7 +31,7 @@ "sharepoint", "systemtags", "templateeditor", - "updater", + "updatenotification", "user_external", "user_ldap", "user_shibboleth", diff --git a/core/templates/layout.user.php b/core/templates/layout.user.php index 7905f5b7f3..f79defe100 100644 --- a/core/templates/layout.user.php +++ b/core/templates/layout.user.php @@ -2,11 +2,7 @@ - - data-update-version="" data-update-link="" - - > + <?php diff --git a/lib/private/templatelayout.php b/lib/private/templatelayout.php index bc66c0dfb1..7166b23d4c 100644 --- a/lib/private/templatelayout.php +++ b/lib/private/templatelayout.php @@ -70,31 +70,6 @@ class TemplateLayout extends \OC_Template { $this->assign('bodyid', 'body-user'); } - // Update notification - if($this->config->getSystemValue('updatechecker', true) === true && - \OC_User::isAdminUser(\OC_User::getUser())) { - $updater = new \OC\Updater( - \OC::$server->getHTTPHelper(), - \OC::$server->getConfig(), - \OC::$server->getIntegrityCodeChecker(), - \OC::$server->getLogger() - ); - $data = $updater->check(); - - if(isset($data['version']) && $data['version'] != '' and $data['version'] !== Array()) { - $this->assign('updateAvailable', true); - $this->assign('updateVersion', $data['versionstring']); - if(substr($data['web'], 0, 8) === 'https://') { - $this->assign('updateLink', $data['web']); - } - \OCP\Util::addScript('core', 'update-notification'); - } else { - $this->assign('updateAvailable', false); // No update available or not an admin user - } - } else { - $this->assign('updateAvailable', false); // Update check is disabled - } - // Code integrity notification $integrityChecker = \OC::$server->getIntegrityCodeChecker(); if(!$integrityChecker->hasPassedCheck()) {