From fc3320e96d84ac579040b67adf652d5cb4523374 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Fri, 6 Sep 2024 14:51:11 +1200 Subject: [PATCH] API Update API to reflect changes to CLI interaction --- docs/en/configuration.md | 6 +-- src/Task/RealMeSetupTask.php | 95 ++++++++++++++++++------------------ 2 files changed, 50 insertions(+), 51 deletions(-) diff --git a/docs/en/configuration.md b/docs/en/configuration.md index ca3bee1..434cfa6 100644 --- a/docs/en/configuration.md +++ b/docs/en/configuration.md @@ -150,7 +150,7 @@ environment. In this environment, RealMe provide all SSL certificates required t - Add a new line as line 1 of the file with the following: `-----BEGIN CERTIFICATE-----` - Add a new line as the last line of the file with the following: `-----END CERTIFICATE-----` - Ensure your `realme.yml` [configuration](docs/en/configuration.md) is complete (see above). -- Run the RealMe build task to validate your configuration and get the XML metadata to provide to MTS: `vendor/bin/sake dev/tasks/RealMeSetupTask forEnv=mts` +- Run the RealMe build task to validate your configuration and get the XML metadata to provide to MTS: `vendor/bin/sake tasks:RealMeSetupTask --forEnv=mts` - Save the XML output from the above task to an XML file, and upload this to MTS: - For a 'logon' integration, submit here: [MTS logon metadata upload](https://mtscloud.realme.govt.nz/Login/home). - For an 'assert' integration, submit here: [MTS assert metadata upload](https://mtscloud.realme.govt.nz/Assertion/home). @@ -232,7 +232,7 @@ To setup syncing, you **must** be using the `login` type of authentication and h on `Member` (or a subclass of it) and then tell the module to sync with the database via the following configuration in realme.yml. You can also include `login_member_after_authentication` which will automatically login a user (as a Silverstripe `Member` object) after successful RealMe authentication. - + ```yaml SilverStripe\Security\Member: extensions: @@ -243,7 +243,7 @@ SilverStripe\RealMe\RealMeService: login_member_after_authentication: true ``` -Run a `dev/build` to ensure the configuration changes are accounted for. +Run `sake db:build --flush` to ensure the configuration changes are accounted for. When a RealMe login completes with success, a new member will be synced based on the RealMe FLT. If no member matching the FLT is found, a new member will be created. _Note this is not supported for `assert`, as the FIT is transient (changes each diff --git a/src/Task/RealMeSetupTask.php b/src/Task/RealMeSetupTask.php index 04159d6..5aa0f20 100644 --- a/src/Task/RealMeSetupTask.php +++ b/src/Task/RealMeSetupTask.php @@ -4,14 +4,15 @@ use Exception; -use SilverStripe\Control\HTTPRequest; -use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Manifest\ModuleLoader; use SilverStripe\RealMe\RealMeService; use SilverStripe\Control\Director; use SilverStripe\Control\Controller; use SilverStripe\Dev\BuildTask; -use SilverStripe\Dev\Deprecation; +use SilverStripe\PolyExecution\PolyOutput; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; /** * Class RealMeSetupTask @@ -26,15 +27,16 @@ */ class RealMeSetupTask extends BuildTask { - private static $segment = 'RealMeSetupTask'; + protected static string $commandName = 'RealMeSetupTask'; private static $dependencies = [ 'Service' => '%$' . RealMeService::class, ]; - protected $title = "RealMe Setup Task"; + protected string $title = "RealMe Setup Task"; - protected $description = 'Validates a realme configuration & creates the resources needed to integrate with realme'; + protected static string $description = 'Validates a realme configuration & creates the resources ' + . 'needed to integrate with realme'; /** * @var RealMeService @@ -48,13 +50,14 @@ class RealMeSetupTask extends BuildTask */ private $errors = array(); + private PolyOutput $output; + /** * Run this setup task. See class phpdoc for the full description of what this does - * - * @param HTTPRequest $request */ - public function run($request) + protected function execute(InputInterface $input, PolyOutput $output): int { + $this->output = $output; try { // Ensure we are running on the command-line, and not running in a browser if (false === Director::is_cli()) { @@ -65,24 +68,37 @@ public function run($request) } // Validate all required values exist - $forEnv = $request->getVar('forEnv'); + $forEnv = $input->getOption('forEnv'); // Throws an exception if there was a problem with the config. $this->validateInputs($forEnv); $this->outputMetadataXmlContent($forEnv); - Deprecation::withNoReplacement(function () use ($forEnv) { - $this->message(PHP_EOL . _t( - RealMeSetupTask::class . '.BUILD_FINISH', - 'RealMe setup complete. Please copy the XML into a file for upload to the {env} environment or ' . - 'DIA to complete the integration', - array('env' => $forEnv) - )); - }); + $this->output->writeln(['', _t( + RealMeSetupTask::class . '.BUILD_FINISH', + 'RealMe setup complete. Please copy the XML into a file for upload to the {env} environment or DIA ' . + 'to complete the integration', + array('env' => $forEnv) + )]); } catch (Exception $e) { - Deprecation::withNoReplacement(fn() => $this->message($e->getMessage() . PHP_EOL)); + $this->output->writeln('' . $e->getMessage() . ''); + return Command::FAILURE; } + return Command::SUCCESS; + } + + public function getOptions(): array + { + return [ + new InputOption( + 'forEnv', + null, + InputOption::VALUE_REQUIRED, + 'The RealMe environment to set up', + suggestedValues: $this->service->getAllowedRealMeEnvironments() + ), + ]; } /** @@ -138,13 +154,10 @@ private function validateInputs($forEnv) )); } - - Deprecation::withNoReplacement(function () { - $this->message(_t( - RealMeSetupTask::class . '.VALIDATION_SUCCESS', - 'Validation succeeded, continuing with setup...' - )); - }); + $this->output->writeln(_t( + RealMeSetupTask::class . '.VALIDATION_SUCCESS', + 'Validation succeeded, continuing with setup...' + )); } /** @@ -155,14 +168,12 @@ private function validateInputs($forEnv) private function outputMetadataXmlContent($forEnv) { // Output metadata XML so that it can be sent to RealMe via the agency - Deprecation::withNoReplacement(function () use ($forEnv) { - $this->message(_t( - RealMeSetupTask::class . '.OUPUT_PREFIX', - 'Metadata XML is listed below for the \'{env}\' RealMe environment, this should be sent to the ' . - 'agency so they can pass it on to RealMe Operations staff', - ['env' => $forEnv] - ) . PHP_EOL . PHP_EOL); - }); + $this->output->writeln(_t( + RealMeSetupTask::class . '.OUPUT_PREFIX', + 'Metadata XML is listed below for the \'{env}\' RealMe environment, this should be sent to the agency so ' . + 'they can pass it on to RealMe Operations staff', + ['env' => $forEnv] + ) . PHP_EOL . PHP_EOL); $configDir = $this->getConfigurationTemplateDir(); $templateFile = Controller::join_links($configDir, 'metadata.xml'); @@ -189,7 +200,7 @@ private function outputMetadataXmlContent($forEnv) ) ); - Deprecation::withNoReplacement(fn() => $this->message($message)); + $this->output->writeln($message); } /** @@ -231,18 +242,6 @@ private function getConfigurationTemplateDir() return $path . '/templates/saml-conf'; } - /** - * Output a message to the console - * @param string $message - * @return void - * @deprecated 5.5.0 Will be replaced with new $output parameter in the run() method - */ - private function message($message) - { - Deprecation::notice('5.5.0', 'Will be replaced with new $output parameter in the run() method'); - echo $message . PHP_EOL; - } - /** * Thin wrapper around is_readable(), used mainly so we can test this class completely * @@ -381,7 +380,7 @@ private function validateRealMeEnvironments($forEnv) $this->errors[] = _t( RealMeSetupTask::class . '.ERR_ENV_NOT_SPECIFIED', 'The RealMe environment was not specified on the cli It must be one of: {allowedEnvs} ' . - 'e.g. vendor/bin/sake dev/tasks/RealMeSetupTask forEnv=mts', + 'e.g. vendor/bin/sake tasks:RealMeSetupTask --forEnv=mts', array( 'allowedEnvs' => join(', ', $allowedEnvs) )