-
Notifications
You must be signed in to change notification settings - Fork 0
Frontend API documentation, #PG-5031 #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 23 commits
9b982cd
69ae322
0ce4c2e
1b6fbf0
d4251e5
9ec2daf
af93984
03620be
2f90532
9c50541
b5f4470
1d2769a
df0a832
f5130b2
af5b4dc
abd559c
95ccd18
d7c2cd4
cb792d2
2293b70
20758fc
46c3cb2
60735ae
fbb4e1c
2f7fc12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Matomo - free/libre analytics platform | ||
| * | ||
| * @link https://matomo.org | ||
| * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Piwik\Plugins\OpenApiDocs; | ||
|
|
||
| use Piwik\Piwik; | ||
| use Piwik\View; | ||
|
|
||
| class Controller extends \Piwik\Plugin\ControllerAdmin | ||
| { | ||
| public function swagger(): string | ||
| { | ||
| Piwik::checkUserHasSomeViewAccess(); | ||
|
|
||
| $view = new View('@OpenApiDocs/swagger'); | ||
| $this->setBasicVariablesView($view); | ||
|
|
||
| return $view->render(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Matomo - free/libre analytics platform | ||
| * | ||
| * @link https://matomo.org | ||
| * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
| */ | ||
|
|
||
| namespace Piwik\Plugins\OpenApiDocs; | ||
|
|
||
| use Piwik\Menu\MenuAdmin; | ||
| use Piwik\Piwik; | ||
|
|
||
| class Menu extends \Piwik\Plugin\Menu | ||
| { | ||
| public function configureAdminMenu(MenuAdmin $menu): void | ||
| { | ||
| if (!Piwik::hasUserSuperUserAccess()) { | ||
| return; | ||
| } | ||
|
|
||
| $menu->addPlatformItem( | ||
| 'OpenApiDocs_SwaggerApi', | ||
| $this->urlForAction('swagger'), | ||
| 30 | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "OpenApiDocs": { | ||
| "ReportingApiMoreInformation": "More info about the Matomo APIs available in %1$sIntroduction to Matomo API%2$s and the %3$sMatomo API Reference%4$s.", | ||
| "ReportingApiReference": "Reporting API Reference", | ||
| "ReportingApiSummary": "All the data in Matomo is available through simple APIs.", | ||
| "SwaggerApi": "Swagger API", | ||
| "SwaggerPagePluginEmpty": "No plugins are configured in the OpenApiDocs whitelist.", | ||
| "SwaggerPageRequestFailed": "The plugin whitelist could not be loaded.", | ||
| "SwaggerPageSpecLoadFailed": "The OpenAPI spec could not be loaded for this plugin.", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lachiebol We should also create an FAQ, explaining how you can build the missing spec or it will be auto generated if flag is set, so that user's don't reach out to support after activating the plugin.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Would this be a part of the checklist? Adding relevant documentation? |
||
| "SwaggerPageSearchNoResults": "No plugins match your search.", | ||
| "SwaggerPageSearchPlaceholder": "Search by plugin name", | ||
| "UserAuthentication": "User authentication", | ||
| "UserAuthenticationManageTokens": "You can manage your authentication tokens on your security page.", | ||
| "UserAuthenticationUsingTokenAuth": "If you want to request data within a script, a crontab, etc. you need to add the '%3$s' URL parameter to the API calls for URLs that require authentication." | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| {% extends 'admin.twig' %} | ||
|
|
||
| {% set title %}{{ 'OpenApiDocs_SwaggerApi'|translate }}{% endset %} | ||
|
|
||
| {% block content %} | ||
| <div vue-entry="OpenApiDocs.SwaggerPage"></div> | ||
| {% endblock %} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Matomo - free/libre analytics platform | ||
| * | ||
| * @link https://matomo.org | ||
| * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Piwik\Plugins\OpenApiDocs\tests\Integration; | ||
|
|
||
| use Piwik\Access; | ||
| use Piwik\Container\StaticContainer; | ||
| use Piwik\Plugin\Manager; | ||
| use Piwik\Plugins\OpenApiDocs\Controller; | ||
| use Piwik\Tests\Framework\Fixture; | ||
| use Piwik\Tests\Framework\Mock\FakeAccess; | ||
| use Piwik\Tests\Framework\TestCase\IntegrationTestCase; | ||
|
|
||
| /** | ||
| * @group OpenApiDocs | ||
| * @group ControllerTest | ||
| * @group Plugins | ||
| */ | ||
| class ControllerTest extends IntegrationTestCase | ||
| { | ||
| /** | ||
| * @var Controller | ||
| */ | ||
| private $controller; | ||
|
|
||
| /** | ||
| * @var array<string, mixed> | ||
| */ | ||
| private $backupGet; | ||
|
|
||
| /** | ||
| * @var array<string, mixed> | ||
| */ | ||
| private $backupRequest; | ||
|
|
||
| public function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->backupGet = $_GET; | ||
| $this->backupRequest = $_REQUEST; | ||
|
|
||
| Fixture::createSuperUser(); | ||
| if (!Fixture::siteCreated(1)) { | ||
| Fixture::createWebsite('2012-01-01 00:00:00'); | ||
| } | ||
|
|
||
| Fixture::resetTranslations(); | ||
| Fixture::loadAllTranslations(); | ||
|
|
||
| Manager::getInstance()->loadPlugin('OpenApiDocs'); | ||
|
|
||
| $_GET = [ | ||
| 'idSite' => 1, | ||
| 'period' => 'day', | ||
| 'date' => 'today', | ||
| ]; | ||
| $_REQUEST = $_GET; | ||
|
|
||
| $this->controller = new Controller(); | ||
| } | ||
|
|
||
| public function tearDown(): void | ||
| { | ||
| $_GET = $this->backupGet; | ||
| $_REQUEST = $this->backupRequest; | ||
|
|
||
| Fixture::resetTranslations(); | ||
|
|
||
| parent::tearDown(); | ||
| } | ||
|
|
||
| public function testSwaggerRendersAdminPageForViewAccess(): void | ||
| { | ||
| FakeAccess::clearAccess( | ||
| $superUser = false, | ||
| $idSitesAdmin = [0], | ||
| $idSitesView = [1], | ||
| $identity = 'viewAccessUser' | ||
| ); | ||
|
|
||
| $html = $this->controller->swagger(); | ||
|
|
||
| $this->assertNotSame('', $html); | ||
| $this->assertStringContainsString('vue-entry="OpenApiDocs.SwaggerPage"', $html); | ||
| $this->assertStringContainsString('Swagger', $html); | ||
| } | ||
|
|
||
| public function testSwaggerThrowsWhenUserHasNoAccess(): void | ||
| { | ||
| $originalAccess = StaticContainer::getContainer()->get(Access::class); | ||
| StaticContainer::getContainer()->set(Access::class, new FakeAccess(false, [], [], 'noAccess')); | ||
|
|
||
| try { | ||
| $this->expectException(\Exception::class); | ||
| $this->expectExceptionMessage('checkUserHasSomeViewAccess'); | ||
|
|
||
| $this->controller->swagger(); | ||
| } finally { | ||
| StaticContainer::getContainer()->set(Access::class, $originalAccess); | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lachiebol This should check view access right ?