1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LineMob\Core\Mocky\Auth\Middleware; |
4
|
|
|
|
5
|
|
|
use League\Tactician\Middleware; |
6
|
|
|
use LineMob\Core\Input; |
7
|
|
|
use LineMob\Core\Mocky\Auth\Command\LoginCommand; |
8
|
|
|
use LineMob\Core\Mocky\Doctrine\Model\User; |
9
|
|
|
use LineMob\Core\Template\TextTemplate; |
10
|
|
|
use Symfony\Component\Workflow\DefinitionBuilder; |
11
|
|
|
use Symfony\Component\Workflow\MarkingStore\MultipleStateMarkingStore; |
12
|
|
|
use Symfony\Component\Workflow\Registry; |
13
|
|
|
use Symfony\Component\Workflow\Transition; |
14
|
|
|
use Symfony\Component\Workflow\Workflow; |
15
|
|
|
|
16
|
|
|
class AuthenticationMiddleware implements Middleware |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Registry |
20
|
|
|
*/ |
21
|
|
|
private $registry; |
22
|
|
|
|
23
|
|
|
public function __construct(Registry $registry) |
24
|
|
|
{ |
25
|
|
|
$this->registry = $registry; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function execute($command, callable $next) |
32
|
|
|
{ |
33
|
|
|
if (!$subject = $command->storage) { |
34
|
|
|
throw new \LogicException("Require storage before using this AuthenticationMiddleware!"); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (!$command instanceof LoginCommand) { |
38
|
|
|
return $next($command); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$command->active = true; |
42
|
|
|
$command->message = new TextTemplate(); |
43
|
|
|
|
44
|
|
|
$workflow = $this->registry->get($subject); |
45
|
|
|
|
46
|
|
|
if ($workflow->can($subject, 'start')) { |
47
|
|
|
$workflow->apply($subject, 'start'); |
48
|
|
|
|
49
|
|
|
$command->message->text = 'Please Enter username & password.'; |
50
|
|
|
|
51
|
|
|
return $next($command); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
@list($username, $password) = $this->captureUserAndPassword($command->input); |
55
|
|
|
|
56
|
|
|
if ($username && $password && $workflow->can($subject, 'enter_username_n_password')) { |
57
|
|
View Code Duplication |
if ($username === 'demo' && $password === 'demo') { |
|
|
|
|
58
|
|
|
$workflow->apply($subject, 'enter_username_n_password'); |
59
|
|
|
|
60
|
|
|
$command->storage->setLineLastLogin(new \DateTimeImmutable()); |
|
|
|
|
61
|
|
|
$command->active = false; |
62
|
|
|
$command->message->text = 'Success!'; |
63
|
|
|
} else { |
64
|
|
|
$command->message->text = 'Try again ...'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $next($command); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($username && $workflow->can($subject, 'enter_username')) { |
71
|
|
|
if ($username === 'demo') { |
72
|
|
|
$workflow->apply($subject, 'enter_username'); |
73
|
|
|
|
74
|
|
|
$command->message->text = 'Please Enter password!'; |
75
|
|
|
} else { |
76
|
|
|
$command->message->text = 'Not found username, Try again ...'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $next($command); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$password = $username; |
83
|
|
|
|
84
|
|
|
if ($password && $workflow->can($subject, 'enter_password')) { |
85
|
|
View Code Duplication |
if ($password === 'demo') { |
|
|
|
|
86
|
|
|
$workflow->apply($subject, 'enter_password'); |
87
|
|
|
|
88
|
|
|
$command->storage->setLineLastLogin(new \DateTimeImmutable()); |
89
|
|
|
$command->active = false; |
90
|
|
|
$command->message->text = 'Success!'; |
91
|
|
|
} else { |
92
|
|
|
$command->message->text = 'Password not match, Try again ...'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $next($command); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
throw new \LogicException("Unknown case!"); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function captureUserAndPassword(Input $input) |
102
|
|
|
{ |
103
|
|
|
$text = trim(preg_replace('|\W+|', ' ', $input->text)); |
104
|
|
|
|
105
|
|
|
return explode(' ', $text); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.