Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/www/ui/api/Controllers/LicenseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Fossology\UI\Api\Controllers;

use Fossology\Lib\Application\LicenseCsvExport;
use Fossology\Lib\Auth\Auth;
use Fossology\Lib\Dao\LicenseAcknowledgementDao;
use Fossology\Lib\Dao\LicenseDao;
Expand All @@ -27,6 +28,7 @@
use Fossology\UI\Page\AdminLicenseCandidate;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Psr7\Factory\StreamFactory;

/**
* @class LicenseController
Expand Down Expand Up @@ -878,4 +880,44 @@ public function getSuggestedLicense($request, $response, $args)
}
return $response->withJson($suggestLicense, 200);
}

/**
* Export licenses to CSV file
*
* @param Request $request
* @param ResponseHelper $response
* @param array $args
* @return ResponseHelper
*/
public function exportAdminLicenseToCSV($request, $response, $args)
{
if (!Auth::isAdmin()) {
$error = new Info(403, "You are not allowed to access the endpoint.", InfoType::ERROR);
return $response->withJson($error->getArray(), $error->getCode());
}
$query = $request->getQueryParams();
$rf = 0;
if (array_key_exists('licenseId', $query)) {
$rf = intval($query['licenseId']);
}
if ($rf != 0 &&
(! $this->dbHelper->doesIdExist("license_ref", "rf_pk", $rf) &&
! $this->dbHelper->doesIdExist("license_candidate", "rf_pk", $rf))) {
$error = new Info(404, "License not found.", InfoType::ERROR);
return $response->withJson($error->getArray(), $error->getCode());
}
$dbManager = $this->dbHelper->getDbManager();
$licenseCsvExport = new LicenseCsvExport($dbManager);
$content = $licenseCsvExport->createCsv($rf);
$fileName = "fossology-license-export-" . date("YMj-Gis");
$newResponse = $response->withHeader('Content-type', 'text/csv, charset=UTF-8')
->withHeader('Content-Disposition', 'attachment; filename=' . $fileName . '.csv')
->withHeader('Pragma', 'no-cache')
->withHeader('Cache-Control', 'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0')
->withHeader('Expires', 'Expires: Thu, 19 Nov 1981 08:52:00 GMT');
$sf = new StreamFactory();
return $newResponse->withBody(
$content ? $sf->createStream($content) : $sf->createStream('')
);
}
}
32 changes: 32 additions & 0 deletions src/www/ui/api/documentation/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4029,6 +4029,38 @@ paths:
$ref: '#/components/schemas/Info'
default:
$ref: '#/components/responses/defaultResponse'
/license/export-csv:
get:
operationId: exportLicense
parameters:
- name: licenseId
description: License id to export, 0 for all
in: query
required: false
default: 0
schema:
type: int
tags:
- License
summary: Export a csv license
description: >
Export a csv license
responses:
'200':
description: Successfully exported
content:
text/csv:
schema:
type: string
format: binary
'403':
description: Route is not accessible
content:
application/json:
schema:
$ref: '#/components/schemas/Info'
default:
$ref: '#/components/responses/defaultResponse'
/license/{shortname}:
parameters:
- name: shortname
Expand Down
1 change: 1 addition & 0 deletions src/www/ui/api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ function (\Slim\Routing\RouteCollectorProxy $app) {
function (\Slim\Routing\RouteCollectorProxy $app) {
$app->get('', LicenseController::class . ':getAllLicenses');
$app->post('/import-csv', LicenseController::class . ':handleImportLicense');
$app->get('/export-csv', LicenseController::class . ':exportAdminLicenseToCSV');
$app->post('', LicenseController::class . ':createLicense');
$app->put('/verify/{shortname:.+}', LicenseController::class . ':verifyLicense');
$app->put('/merge/{shortname:.+}', LicenseController::class . ':mergeLicense');
Expand Down