1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupMiddleware\MiddlewareBundle\Console\Command; |
20
|
|
|
|
21
|
|
|
use Assert\Assertion; |
22
|
|
|
use DateInterval; |
23
|
|
|
use DateTime; |
24
|
|
|
use InvalidArgumentException; |
25
|
|
|
use Symfony\Component\Console\Command\Command; |
26
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
27
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
28
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
29
|
|
|
use Symfony\Component\DependencyInjection\Container; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The EmailVerifiedSecondFactorRemindersCommand can be run to send reminders to token registrants. |
33
|
|
|
* |
34
|
|
|
* The command utilizes a specific service for this task (VerifiedSecondFactorReminderService). Input validation is |
35
|
|
|
* performed on the incoming request parameters. |
36
|
|
|
*/ |
37
|
|
|
final class EmailVerifiedSecondFactorRemindersCommand extends Command |
38
|
|
|
{ |
39
|
|
|
protected function configure() |
40
|
|
|
{ |
41
|
|
|
$this |
42
|
|
|
->setName('middleware:cron:email-reminder') |
43
|
|
|
->setDescription('Sends email reminders to identities with verified tokens more than 7 days old.') |
44
|
|
|
->addArgument('dry-run', InputArgument::OPTIONAL, 'Run in dry mode, not sending any email') |
45
|
|
|
->addArgument( |
46
|
|
|
'date', |
47
|
|
|
InputArgument::OPTIONAL, |
48
|
|
|
'The date (Y-m-d) that should be used for sending reminder email messages, defaults to TODAY - 7' |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
53
|
|
|
{ |
54
|
|
|
/** @var Container $container */ |
55
|
|
|
$container = $this->getApplication()->getKernel()->getContainer(); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$service = $container->get('surfnet_stepup_middleware_middleware.verfied_second_factor_reminder'); |
58
|
|
|
$logger = $container->get('logger'); |
59
|
|
|
|
60
|
|
|
try { |
61
|
|
|
$this->validateInput($input); |
62
|
|
|
} catch (InvalidArgumentException $e) { |
63
|
|
|
$logger->error(sprintf('Invalid arguments passed to the %s', $this->getName()), [$e->getMessage()]); |
64
|
|
|
return 1; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$date = new DateTime(); |
68
|
|
|
$date->sub(new DateInterval('P7D')); |
69
|
|
|
if ($input->hasArgument('date') && !is_null($input->getArgument('date'))) { |
70
|
|
|
$date = DateTime::createFromFormat('Y-m-d', $input->getArgument('date')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$dryRun = false; |
74
|
|
|
if ($input->hasArgument('dry-run') && !is_null($input->getArgument('dry-run'))) { |
75
|
|
|
$dryRun = $input->getArgument('dry-run'); |
76
|
|
|
} |
77
|
|
|
$service->sendReminders($date, $dryRun); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function validateInput(InputInterface $input) |
81
|
|
|
{ |
82
|
|
|
if ($input->hasArgument('date')) { |
83
|
|
|
$date = $input->getArgument('date'); |
84
|
|
|
Assertion::nullOrDate($date, 'Y-m-d', 'Expected date to be a string and formatted as a Y-m-d'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($input->hasArgument('dry-run')) { |
88
|
|
|
$dryRun = $input->getArgument('dry-run'); |
89
|
|
|
Assertion::nullOrBoolean($dryRun, 'Expected dry-run parameter to be a boolean value.'); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: