Merge pull request #5567 from nextcloud/public-capabilities

Public capabilities API
This commit is contained in:
Roeland Jago Douma 2017-07-12 13:04:54 +02:00 committed by GitHub
commit 86a496d94a
8 changed files with 112 additions and 6 deletions

View File

@ -23,7 +23,7 @@
namespace OCA\Theming;
use OCP\Capabilities\ICapability;
use OCP\Capabilities\IPublicCapability;
use OCP\IConfig;
use OCP\IURLGenerator;
@ -32,7 +32,7 @@ use OCP\IURLGenerator;
*
* @package OCA\Theming
*/
class Capabilities implements ICapability {
class Capabilities implements IPublicCapability {
/** @var ThemingDefaults */
protected $theming;

View File

@ -80,7 +80,8 @@ class OCSController extends \OCP\AppFramework\OCSController {
}
/**
* @NoAdminRequired
* @PublicPage
*
* @return DataResponse
*/
public function getCapabilities() {
@ -94,7 +95,11 @@ class OCSController extends \OCP\AppFramework\OCSController {
'edition' => '',
);
$result['capabilities'] = $this->capabilitiesManager->getCapabilities();
if($this->userSession->isLoggedIn()) {
$result['capabilities'] = $this->capabilitiesManager->getCapabilities();
} else {
$result['capabilities'] = $this->capabilitiesManager->getCapabilities(true);
}
return new DataResponse($result);
}

View File

@ -66,6 +66,7 @@ return array(
'OCP\\BackgroundJob\\IJob' => $baseDir . '/lib/public/BackgroundJob/IJob.php',
'OCP\\BackgroundJob\\IJobList' => $baseDir . '/lib/public/BackgroundJob/IJobList.php',
'OCP\\Capabilities\\ICapability' => $baseDir . '/lib/public/Capabilities/ICapability.php',
'OCP\\Capabilities\\IPublicCapability' => $baseDir . '/lib/public/Capabilities/IPublicCapability.php',
'OCP\\Command\\IBus' => $baseDir . '/lib/public/Command/IBus.php',
'OCP\\Command\\ICommand' => $baseDir . '/lib/public/Command/ICommand.php',
'OCP\\Comments\\CommentsEntityEvent' => $baseDir . '/lib/public/Comments/CommentsEntityEvent.php',

View File

@ -96,6 +96,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\BackgroundJob\\IJob' => __DIR__ . '/../../..' . '/lib/public/BackgroundJob/IJob.php',
'OCP\\BackgroundJob\\IJobList' => __DIR__ . '/../../..' . '/lib/public/BackgroundJob/IJobList.php',
'OCP\\Capabilities\\ICapability' => __DIR__ . '/../../..' . '/lib/public/Capabilities/ICapability.php',
'OCP\\Capabilities\\IPublicCapability' => __DIR__ . '/../../..' . '/lib/public/Capabilities/IPublicCapability.php',
'OCP\\Command\\IBus' => __DIR__ . '/../../..' . '/lib/public/Command/IBus.php',
'OCP\\Command\\ICommand' => __DIR__ . '/../../..' . '/lib/public/Command/ICommand.php',
'OCP\\Comments\\CommentsEntityEvent' => __DIR__ . '/../../..' . '/lib/public/Comments/CommentsEntityEvent.php',

View File

@ -24,6 +24,7 @@ namespace OC;
use OCP\AppFramework\QueryException;
use OCP\Capabilities\ICapability;
use OCP\Capabilities\IPublicCapability;
use OCP\ILogger;
class CapabilitiesManager {
@ -41,10 +42,11 @@ class CapabilitiesManager {
/**
* Get an array of al the capabilities that are registered at this manager
*
* @param bool $public get public capabilities only
* @throws \InvalidArgumentException
* @return array
*/
public function getCapabilities() {
public function getCapabilities($public = false) {
$capabilities = [];
foreach($this->capabilities as $capability) {
try {
@ -55,7 +57,9 @@ class CapabilitiesManager {
}
if ($c instanceof ICapability) {
$capabilities = array_replace_recursive($capabilities, $c->getCapabilities());
if(!$public || $c instanceof IPublicCapability) {
$capabilities = array_replace_recursive($capabilities, $c->getCapabilities());
}
} else {
throw new \InvalidArgumentException('The given Capability (' . get_class($c) . ') does not implement the ICapability interface');
}

View File

@ -0,0 +1,32 @@
<?php
/**
* @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCP\Capabilities;
/**
* @inheritdoc
*
* @since 13.0.0
*/
interface IPublicCapability extends ICapability {}

View File

@ -85,6 +85,9 @@ class OCSControllerTest extends TestCase {
}
public function testGetCapabilities() {
$this->userSession->expects($this->once())
->method('isLoggedIn')
->willReturn(true);
list($major, $minor, $micro) = \OCP\Util::getVersion();
$result = [];
@ -112,6 +115,38 @@ class OCSControllerTest extends TestCase {
$this->assertEquals($expected, $this->controller->getCapabilities());
}
public function testGetCapabilitiesPublic() {
$this->userSession->expects($this->once())
->method('isLoggedIn')
->willReturn(false);
list($major, $minor, $micro) = \OCP\Util::getVersion();
$result = [];
$result['version'] = array(
'major' => $major,
'minor' => $minor,
'micro' => $micro,
'string' => \OC_Util::getVersionString(),
'edition' => '',
);
$capabilities = [
'foo' => 'bar',
'a' => [
'b' => true,
'c' => 11,
]
];
$this->capabilitiesManager->method('getCapabilities')
->with(true)
->willReturn($capabilities);
$result['capabilities'] = $capabilities;
$expected = new DataResponse($result);
$this->assertEquals($expected, $this->controller->getCapabilities());
}
public function testPersonCheckValid() {
$this->userManager->method('checkPassword')
->with(

View File

@ -24,6 +24,7 @@ namespace Test;
use OC\CapabilitiesManager;
use OCP\AppFramework\QueryException;
use OCP\Capabilities\ICapability;
use OCP\Capabilities\IPublicCapability;
use OCP\ILogger;
class CapabilitiesManagerTest extends TestCase {
@ -35,6 +36,7 @@ class CapabilitiesManagerTest extends TestCase {
private $logger;
public function setUp() {
parent::setUp();
$this->logger = $this->getMockBuilder('OCP\ILogger')->getMock();
$this->manager = new CapabilitiesManager($this->logger);
}
@ -59,6 +61,24 @@ class CapabilitiesManagerTest extends TestCase {
$this->assertEquals(['foo' => 1], $res);
}
/**
* Test a public capabilitie
*/
public function testPublicCapability() {
$this->manager->registerCapability(function() {
return new PublicSimpleCapability1();
});
$this->manager->registerCapability(function() {
return new SimpleCapability2();
});
$this->manager->registerCapability(function() {
return new SimpleCapability3();
});
$res = $this->manager->getCapabilities(true);
$this->assertEquals(['foo' => 1], $res);
}
/**
* Test that we need something that implents ICapability
* @expectedException \InvalidArgumentException
@ -160,6 +180,14 @@ class SimpleCapability3 implements ICapability {
}
}
class PublicSimpleCapability1 implements IPublicCapability {
public function getCapabilities() {
return [
'foo' => 1
];
}
}
class NoCapability {
public function getCapabilities() {
return [