Skip to content

Commit

Permalink
API Update API to reflect changes to CLI interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Sep 17, 2024
1 parent c3ae8f8 commit fc3320e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 51 deletions.
6 changes: 3 additions & 3 deletions docs/en/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
95 changes: 47 additions & 48 deletions src/Task/RealMeSetupTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()) {
Expand All @@ -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('<error>' . $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()
),
];
}

/**
Expand Down Expand Up @@ -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...'
));
}

/**
Expand All @@ -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');
Expand All @@ -189,7 +200,7 @@ private function outputMetadataXmlContent($forEnv)
)
);

Deprecation::withNoReplacement(fn() => $this->message($message));
$this->output->writeln($message);
}

/**
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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)
)
Expand Down

0 comments on commit fc3320e

Please sign in to comment.