-
Notifications
You must be signed in to change notification settings - Fork 2
Add CSV export to RA Management search results #521
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
base: main
Are you sure you want to change the base?
Changes from all commits
2141d25
3bb06ad
8925771
3bda199
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,99 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Copyright 2026 SURFnet bv | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| namespace Surfnet\StepupRa\RaBundle\Command; | ||
|
|
||
| use DateTime; | ||
| use Surfnet\StepupRa\RaBundle\Value\RoleAtInstitution; | ||
| use Symfony\Component\Validator\Constraints as Assert; | ||
|
|
||
| final class ExportRaListingCommand | ||
| { | ||
| /** | ||
| * @var string | ||
| */ | ||
| #[Assert\NotBlank(message: 'ra.search_ra_listing.actor_id.blank')] | ||
| #[Assert\Type('string', message: 'ra.search_ra_listing.actor_id.type')] | ||
| public $actorId; | ||
|
|
||
| /** | ||
| * @var string|null | ||
| */ | ||
| public $name; | ||
|
|
||
| /** | ||
| * @var string|null | ||
| */ | ||
| public $email; | ||
|
|
||
| /** | ||
| * @var string|null | ||
| */ | ||
| public $institution; | ||
|
|
||
| /** | ||
| * @var RoleAtInstitution|null | ||
| */ | ||
| public $roleAtInstitution; | ||
|
|
||
| /** | ||
| * @var string|null | ||
| */ | ||
| #[Assert\Choice(choices: ['name', 'email'], message: 'ra.search_ra_candidates.order_by.invalid_choice')] | ||
| public $orderBy; | ||
|
|
||
| /** | ||
| * @var string|null | ||
| */ | ||
| #[Assert\Choice(choices: ['asc', 'desc'], message: 'ra.search_ra_candidates.order_direction.invalid_choice')] | ||
| public $orderDirection; | ||
|
|
||
| /** | ||
| * Builds the command from a SearchRaListingCommand | ||
| */ | ||
| public static function fromSearchCommand(SearchRaListingCommand $command): ExportRaListingCommand | ||
| { | ||
| $exportCommand = new self; | ||
|
|
||
| $exportCommand->actorId = $command->actorId; | ||
| $exportCommand->name = $command->name; | ||
| $exportCommand->email = $command->email; | ||
| $exportCommand->institution = $command->institution; | ||
| $exportCommand->roleAtInstitution = $command->roleAtInstitution; | ||
| $exportCommand->orderBy = $command->orderBy; | ||
| $exportCommand->orderDirection = $command->orderDirection; | ||
|
|
||
| return $exportCommand; | ||
| } | ||
|
|
||
| public function getFileName(): string | ||
| { | ||
| $date = new DateTime(); | ||
| $date = $date->format('Y-m-d'); | ||
|
|
||
| $role = $this->roleAtInstitution instanceof RoleAtInstitution && $this->roleAtInstitution->hasRole() | ||
| ? $this->roleAtInstitution->getRole() | ||
| : null; | ||
|
|
||
| return match ($role) { | ||
| 'ra' => "ra_export_{$date}", | ||
| 'raa' => "raa_export_{$date}", | ||
| default => "ra-raa-export_{$date}", | ||
| }; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Copyright 2026 SURFnet bv | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| namespace Surfnet\StepupRa\RaBundle\Service; | ||
|
|
||
| use Psr\Log\LoggerInterface; | ||
| use RuntimeException; | ||
| use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RaListing; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Symfony\Component\HttpFoundation\StreamedResponse; | ||
|
|
||
| class RaListingExport | ||
| { | ||
| public function __construct(private readonly LoggerInterface $logger) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @param iterable<RaListing> $raListings | ||
| */ | ||
| public function export(iterable $raListings, string $fileName): StreamedResponse | ||
| { | ||
| $this->logger->notice(sprintf('Starting RA(A) listing export to "%s"', $fileName)); | ||
|
|
||
| $columnNames = $this->getColumnNames(); | ||
|
|
||
| return new StreamedResponse( | ||
| function () use ($raListings, $columnNames, $fileName) { | ||
| $handle = fopen('php://output', 'r+'); | ||
| if ($handle === false) { | ||
| throw new RuntimeException('Unable to open php://output for writing the RA(A) listing export'); | ||
| } | ||
| fputcsv($handle, $columnNames); | ||
|
|
||
| $rowCount = 0; | ||
| foreach ($raListings as $raListing) { | ||
| fputcsv($handle, [ | ||
|
Comment on lines
+48
to
+52
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.
PHP 8.4 deprecates the implicit default (the new tests emit two deprecations on newer PHP), and the legacy Suggested approach: pass |
||
| $this->sanitizeCsvCell($raListing->commonName), | ||
| $this->sanitizeCsvCell($raListing->email), | ||
| $this->sanitizeCsvCell($raListing->institution), | ||
| $this->sanitizeCsvCell($raListing->role), | ||
| $this->sanitizeCsvCell($raListing->raInstitution), | ||
| $this->sanitizeCsvCell($raListing->location), | ||
| $this->sanitizeCsvCell($raListing->contactInformation), | ||
| ]); | ||
| $rowCount++; | ||
| } | ||
|
|
||
| $this->logger->notice(sprintf('Exported %d rows to "%s"', $rowCount, $fileName)); | ||
|
|
||
| fflush($handle); | ||
| fclose($handle); | ||
| }, | ||
| Response::HTTP_OK, | ||
| [ | ||
| 'Content-Type' => 'application/csv', | ||
| 'Content-Disposition' => sprintf('attachment; filename="%s.csv"', $fileName), | ||
| ], | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Neutralizes CSV/spreadsheet formula injection: values coming from identity/profile | ||
| * data are attacker-influenceable and must not be allowed to start a formula when the | ||
| * exported file is opened in Excel/LibreOffice/Google Sheets. The leading "+" is | ||
| * intentionally not treated as a formula trigger here, since Contact Information | ||
| * routinely holds international phone numbers (e.g. "+31 6 12345678") that must be | ||
| * exported unmodified. | ||
| * | ||
| * @see https://owasp.org/www-community/attacks/CSV_Injection | ||
| */ | ||
| private function sanitizeCsvCell(?string $value): ?string | ||
| { | ||
| if ($value === null || $value === '') { | ||
| return $value; | ||
| } | ||
|
|
||
| if (in_array($value[0], ['=', '-', '@', "\t", "\r"], true)) { | ||
| return "'" . $value; | ||
| } | ||
|
|
||
| return $value; | ||
| } | ||
|
|
||
| /** | ||
| * @return string[] | ||
| */ | ||
| private function getColumnNames(): array | ||
| { | ||
| return [ | ||
| 'Common Name', | ||
| 'Email', | ||
| 'Institution', | ||
| 'Role', | ||
| 'RA Institution', | ||
| 'Location', | ||
| 'Contact Information', | ||
| ]; | ||
| } | ||
| } | ||
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.
🤔 The getClickedButton does seem to be officially supported (https://symfony.com/doc/current/forms.html#handling-multiple-submit-buttons)
But it still needs a phpstan exception. Is the
$this->createForm()typehint correct?nitpick