1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LineMob\Core\Mocky\Auth; |
4
|
|
|
|
5
|
|
|
use LineMob\Core\Input; |
6
|
|
|
use LineMob\Core\Mocky\Auth\Command\BaseCommand; |
7
|
|
|
use LineMob\Core\Mocky\Doctrine\Model\User; |
8
|
|
|
use LineMob\Core\Workflow\AbstractWorkflow; |
9
|
|
|
|
10
|
|
|
class AuthenticationWorkflow extends AbstractWorkflow |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc} |
14
|
|
|
*/ |
15
|
|
|
protected function getConfig() |
16
|
|
|
{ |
17
|
|
|
return [ |
18
|
|
|
'name' => 'Authentication', |
19
|
|
|
'marking_store' => [ |
20
|
|
|
'type' => 'multiple_state', |
21
|
|
|
'arguments' => ['state'], |
22
|
|
|
], |
23
|
|
|
'supports' => [ |
24
|
|
|
User::class, |
25
|
|
|
], |
26
|
|
|
'places' => [ |
27
|
|
|
'started', |
28
|
|
|
'wait_for_username', |
29
|
|
|
'wait_for_password', |
30
|
|
|
'wait_for_username_n_password', |
31
|
|
|
'finished', |
32
|
|
|
], |
33
|
|
|
'transitions' => [ |
34
|
|
|
'start' => [ |
35
|
|
|
'from' => 'started', |
36
|
|
|
'to' => ['wait_for_username_n_password', 'wait_for_username'], |
37
|
|
|
], |
38
|
|
|
'enter_username' => [ |
39
|
|
|
'from' => 'wait_for_username', |
40
|
|
|
'to' => 'wait_for_password', |
41
|
|
|
], |
42
|
|
|
'enter_password' => [ |
43
|
|
|
'from' => 'wait_for_password', |
44
|
|
|
'to' => 'finished', |
45
|
|
|
], |
46
|
|
|
'enter_username_n_password' => [ |
47
|
|
|
'from' => 'wait_for_username_n_password', |
48
|
|
|
'to' => 'finished', |
49
|
|
|
], |
50
|
|
|
], |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Input $input |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
private function captureUserAndPassword(Input $input) |
60
|
|
|
{ |
61
|
|
|
$text = trim(preg_replace('|\W+|', ' ', $input->text)); |
62
|
|
|
|
63
|
|
|
return explode(' ', $text); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param BaseCommand $command |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function doApplyStart(BaseCommand $command) |
72
|
|
|
{ |
73
|
|
|
$workflow = $this->registry->get($subject = $command->storage); |
74
|
|
|
|
75
|
|
|
if ($workflow->can($subject, 'start')) { |
76
|
|
|
$workflow->apply($subject, 'start'); |
77
|
|
|
|
78
|
|
|
$command->message->text = 'Please Enter username & password.'; |
79
|
|
|
|
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param BaseCommand $command |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function doApplyEnterUsernameAndPassword(BaseCommand $command) |
92
|
|
|
{ |
93
|
|
|
$workflow = $this->registry->get($subject = $command->storage); |
94
|
|
|
|
95
|
|
|
@list($username, $password) = $this->captureUserAndPassword($command->input); |
96
|
|
|
|
97
|
|
|
$command->message->text = 'Try again ...'; |
98
|
|
|
|
99
|
|
|
if (!$username || !$password || !$workflow->can($subject, 'enter_username_n_password')) { |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($username === 'demo' && $password === 'demo') { |
104
|
|
|
$workflow->apply($subject, 'enter_username_n_password'); |
105
|
|
|
|
106
|
|
|
$command->storage->setLineLastLogin(new \DateTimeImmutable()); |
107
|
|
|
$command->active = false; |
108
|
|
|
$command->message->text = 'Success!'; |
109
|
|
|
|
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param BaseCommand $command |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function doApplyEnterUsername(BaseCommand $command) |
122
|
|
|
{ |
123
|
|
|
$workflow = $this->registry->get($subject = $command->storage); |
124
|
|
|
$username = $this->captureUserAndPassword($command->input)[0]; |
125
|
|
|
|
126
|
|
|
if ($username && $workflow->can($subject, 'enter_username')) { |
127
|
|
|
if ($username === 'demo') { |
128
|
|
|
$workflow->apply($subject, 'enter_username'); |
129
|
|
|
|
130
|
|
|
$command->message->text = 'Please Enter password!'; |
131
|
|
|
} else { |
132
|
|
|
$command->message->text = 'Not found username, Try again ...'; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return true; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param BaseCommand $command |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function doApplyEnterPassword(BaseCommand $command) |
147
|
|
|
{ |
148
|
|
|
$workflow = $this->registry->get($subject = $command->storage); |
149
|
|
|
$password = $this->captureUserAndPassword($command->input)[0]; |
150
|
|
|
|
151
|
|
|
if ($password && $workflow->can($subject, 'enter_password')) { |
152
|
|
|
if ($password === 'demo') { |
153
|
|
|
$workflow->apply($subject, 'enter_password'); |
154
|
|
|
|
155
|
|
|
$command->storage->setLineLastLogin(new \DateTimeImmutable()); |
156
|
|
|
$command->storage->setLineActiveCmd(null); |
157
|
|
|
$command->storage->setLineCommandData([]); |
158
|
|
|
$command->active = false; |
159
|
|
|
$command->message->text = 'Success!'; |
160
|
|
|
} else { |
161
|
|
|
$command->message->text = 'Password not match, Try again ...'; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return true; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|