|
1
|
|
|
<?php |
|
2
|
|
|
/* For licensing terms, see /license.txt */ |
|
3
|
|
|
|
|
4
|
|
|
namespace Chamilo\PluginBundle\WhispeakAuth\Controller; |
|
5
|
|
|
|
|
6
|
|
|
use Chamilo\PluginBundle\Entity\WhispeakAuth\LogEvent; |
|
7
|
|
|
use Chamilo\PluginBundle\WhispeakAuth\Request\ApiRequest; |
|
8
|
|
|
use Chamilo\UserBundle\Entity\User; |
|
9
|
|
|
use ChamiloSession; |
|
10
|
|
|
use Display; |
|
11
|
|
|
use Login; |
|
12
|
|
|
use WhispeakAuthPlugin; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class AuthenticationController. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Chamilo\PluginBundle\WhispeakAuth\Controller |
|
18
|
|
|
*/ |
|
19
|
|
|
class AuthenticationController extends BaseController |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @throws \Exception |
|
23
|
|
|
*/ |
|
24
|
|
|
public function index() |
|
25
|
|
|
{ |
|
26
|
|
|
if (!$this->plugin->toolIsEnabled()) { |
|
27
|
|
|
throw new \Exception(get_lang('NotAllowed')); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** @var array $lpQuestionInfo */ |
|
31
|
|
|
$lpQuestionInfo = ChamiloSession::read(WhispeakAuthPlugin::SESSION_QUIZ_QUESTION, []); |
|
32
|
|
|
|
|
33
|
|
|
if (ChamiloSession::read(WhispeakAuthPlugin::SESSION_AUTH_PASSWORD, false)) { |
|
34
|
|
|
ChamiloSession::erase(WhispeakAuthPlugin::SESSION_AUTH_PASSWORD); |
|
35
|
|
|
|
|
36
|
|
|
if (empty($lpQuestionInfo)) { |
|
37
|
|
|
$message = $this->plugin->get_lang('MaxAttemptsReached') |
|
38
|
|
|
.'<br><strong>'.$this->plugin->get_lang('LoginWithUsernameAndPassword').'</strong>'; |
|
39
|
|
|
|
|
40
|
|
|
Display::addFlash( |
|
41
|
|
|
Display::return_message($message, 'warning') |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
header('Location: '.api_get_path(WEB_PLUGIN_PATH).'whispeakauth/authentify_password.php'); |
|
46
|
|
|
exit; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** @var array $lpItemInfo */ |
|
50
|
|
|
$lpItemInfo = ChamiloSession::read(WhispeakAuthPlugin::SESSION_LP_ITEM, []); |
|
51
|
|
|
/** @var \learnpath $oLp */ |
|
52
|
|
|
$oLp = ChamiloSession::read('oLP', null); |
|
53
|
|
|
/** @var \Exercise $objExercise */ |
|
54
|
|
|
$objExercise = ChamiloSession::read('objExercise', null); |
|
55
|
|
|
|
|
56
|
|
|
$isAuthOnLp = !empty($lpItemInfo) && !empty($oLp); |
|
57
|
|
|
$isAuthOnQuiz = !empty($lpQuestionInfo) && !empty($objExercise); |
|
58
|
|
|
$showFullPage = !$isAuthOnLp && !$isAuthOnQuiz; |
|
59
|
|
|
|
|
60
|
|
|
$user = api_get_user_entity( |
|
61
|
|
|
ChamiloSession::read(WhispeakAuthPlugin::SESSION_2FA_USER, 0) ?: api_get_user_id() |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
$showForm = !$user; |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
if ($user) { |
|
|
|
|
|
|
67
|
|
|
if (!WhispeakAuthPlugin::getAuthUidValue($user)) { |
|
68
|
|
|
$message = Display::return_message($this->plugin->get_lang('SpeechAuthNotEnrolled'), 'warning'); |
|
69
|
|
|
|
|
70
|
|
|
if (!empty($lpQuestionInfo) && empty($lpItemInfo)) { |
|
71
|
|
|
echo $message; |
|
72
|
|
|
} else { |
|
73
|
|
|
Display::addFlash($message); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
header('Location: '.api_get_path(WEB_PLUGIN_PATH).'whispeakauth/authentify_password.php'); |
|
77
|
|
|
|
|
78
|
|
|
exit; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (!empty($lpQuestionInfo) && empty($lpItemInfo)) { |
|
83
|
|
|
echo api_get_js('rtc/RecordRTC.js'); |
|
84
|
|
|
echo api_get_js_simple(api_get_path(WEB_PLUGIN_PATH).'whispeakauth/assets/js/RecordAudio.js'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$request = new ApiRequest(); |
|
88
|
|
|
$response = $request->createAuthenticationSessionToken($user); |
|
89
|
|
|
|
|
90
|
|
|
if (empty($response['text'])) { |
|
91
|
|
|
$varNumber = mt_rand(1, 6); |
|
92
|
|
|
$response['text'] = $this->plugin->get_lang("AuthentifySampleText$varNumber"); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
ChamiloSession::write(WhispeakAuthPlugin::SESSION_SENTENCE_TEXT, $response['token']); |
|
96
|
|
|
|
|
97
|
|
|
if (!empty($lpQuestionInfo) && empty($lpItemInfo)) { |
|
98
|
|
|
$template = new \Template('', $showFullPage, $showFullPage, false, true, false); |
|
99
|
|
|
$template->assign('show_form', $showForm); |
|
100
|
|
|
$template->assign('sample_text', $response['text']); |
|
101
|
|
|
|
|
102
|
|
|
echo $template->fetch('whispeakauth/view/authentify_recorder.html.twig'); |
|
103
|
|
|
exit; |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$this->displayPage( |
|
107
|
|
|
$showFullPage, |
|
108
|
|
|
[ |
|
109
|
|
|
'show_form' => $showForm, |
|
110
|
|
|
'sample_text' => $response['text'], |
|
111
|
|
|
] |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @throws \Exception |
|
117
|
|
|
*/ |
|
118
|
|
|
public function ajax() |
|
119
|
|
|
{ |
|
120
|
|
|
$userId = api_get_user_id(); |
|
121
|
|
|
$user2fa = ChamiloSession::read(WhispeakAuthPlugin::SESSION_2FA_USER, 0); |
|
122
|
|
|
|
|
123
|
|
|
if (!empty($user2fa) || !empty($userId)) { |
|
124
|
|
|
$isAllowed = !empty($_FILES['audio']); |
|
125
|
|
|
} else { |
|
126
|
|
|
$isAllowed = !empty($_POST['username']) && !empty($_FILES['audio']); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if (!$isAllowed || !$this->plugin->toolIsEnabled()) { |
|
130
|
|
|
throw new \Exception(get_lang('NotAllowed')); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if (!empty($user2fa)) { |
|
134
|
|
|
$user = api_get_user_entity($user2fa); |
|
135
|
|
|
} elseif (!empty($userId)) { |
|
136
|
|
|
$user = api_get_user_entity($userId); |
|
137
|
|
|
} else { |
|
138
|
|
|
/** @var User|null $user */ |
|
139
|
|
|
$user = \UserManager::getRepository()->findOneBy(['username' => $_POST['username']]); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
if (!$user) { |
|
143
|
|
|
throw new \Exception(get_lang('NotFound')); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$audioFilePath = $this->uploadAudioFile($user); |
|
147
|
|
|
|
|
148
|
|
|
$failedLogins = ChamiloSession::read(WhispeakAuthPlugin::SESSION_FAILED_LOGINS, 0); |
|
149
|
|
|
$maxAttempts = $this->plugin->getMaxAttempts(); |
|
150
|
|
|
|
|
151
|
|
|
if ($maxAttempts && $failedLogins >= $maxAttempts) { |
|
152
|
|
|
throw new \Exception($this->plugin->get_lang('MaxAttemptsReached')); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$token = \ChamiloSession::read(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT); |
|
156
|
|
|
|
|
157
|
|
|
$request = new ApiRequest(); |
|
158
|
|
|
$success = $request->performAuthentication($token, $user, $audioFilePath); |
|
159
|
|
|
|
|
160
|
|
|
\ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT); |
|
161
|
|
|
|
|
162
|
|
|
/** @var array $lpItemInfo */ |
|
163
|
|
|
$lpItemInfo = ChamiloSession::read(WhispeakAuthPlugin::SESSION_LP_ITEM, []); |
|
164
|
|
|
/** @var array $quizQuestionInfo */ |
|
165
|
|
|
$quizQuestionInfo = ChamiloSession::read(WhispeakAuthPlugin::SESSION_QUIZ_QUESTION, []); |
|
166
|
|
|
|
|
167
|
|
|
$message = $this->plugin->get_lang('AuthentifySuccess'); |
|
168
|
|
|
|
|
169
|
|
|
if (!$success) { |
|
170
|
|
|
if (!empty($lpItemInfo)) { |
|
171
|
|
|
$this->plugin->addAttemptInLearningPath( |
|
172
|
|
|
LogEvent::STATUS_FAILED, |
|
173
|
|
|
$user->getId(), |
|
174
|
|
|
$lpItemInfo['lp_item'], |
|
175
|
|
|
$lpItemInfo['lp'] |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
if (!empty($quizQuestionInfo)) { |
|
180
|
|
|
$this->plugin->addAttemptInQuiz( |
|
181
|
|
|
LogEvent::STATUS_FAILED, |
|
182
|
|
|
$user->getId(), |
|
183
|
|
|
$quizQuestionInfo['question'], |
|
184
|
|
|
$quizQuestionInfo['quiz'] |
|
185
|
|
|
); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
if (empty($lpItemInfo) && empty($quizQuestionInfo)) { |
|
189
|
|
|
$this->plugin->addAuthenticationAttempt(LogEvent::STATUS_FAILED, $user->getId()); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$message = $this->plugin->get_lang('AuthentifyFailed'); |
|
193
|
|
|
|
|
194
|
|
|
ChamiloSession::write(WhispeakAuthPlugin::SESSION_FAILED_LOGINS, ++$failedLogins); |
|
195
|
|
|
|
|
196
|
|
|
if ($maxAttempts && $failedLogins >= $maxAttempts) { |
|
197
|
|
|
$message .= PHP_EOL |
|
198
|
|
|
.'<span data-reach-attempts="true">'.$this->plugin->get_lang('MaxAttemptsReached').'</span>' |
|
199
|
|
|
.PHP_EOL |
|
200
|
|
|
.'<br><strong>' |
|
201
|
|
|
.$this->plugin->get_lang('LoginWithUsernameAndPassword') |
|
202
|
|
|
.'</strong>'; |
|
203
|
|
|
|
|
204
|
|
|
if (!empty($user2fa)) { |
|
205
|
|
|
Display::addFlash( |
|
206
|
|
|
Display::return_message($message, 'warning', false) |
|
207
|
|
|
); |
|
208
|
|
|
} |
|
209
|
|
|
} else { |
|
210
|
|
|
$message .= PHP_EOL.$this->plugin->get_lang('TryAgain'); |
|
211
|
|
|
|
|
212
|
|
|
if ('true' === api_get_setting('allow_lostpassword')) { |
|
213
|
|
|
$message .= '<br>' |
|
214
|
|
|
.Display::url( |
|
215
|
|
|
get_lang('LostPassword'), |
|
216
|
|
|
api_get_path(WEB_CODE_PATH).'auth/lostPassword.php', |
|
217
|
|
|
['target' => $lpItemInfo ? '_top' : '_self'] |
|
218
|
|
|
); |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
echo Display::return_message( |
|
224
|
|
|
$message, |
|
225
|
|
|
$success ? 'success' : 'warning', |
|
226
|
|
|
false |
|
227
|
|
|
); |
|
228
|
|
|
|
|
229
|
|
|
if (!$success && $maxAttempts && $failedLogins >= $maxAttempts) { |
|
230
|
|
|
ChamiloSession::erase(WhispeakAuthPlugin::SESSION_FAILED_LOGINS); |
|
231
|
|
|
|
|
232
|
|
|
if (!empty($lpItemInfo)) { |
|
233
|
|
|
echo '<script>window.location.href = "' |
|
234
|
|
|
.api_get_path(WEB_PLUGIN_PATH) |
|
235
|
|
|
.'whispeakauth/authentify_password.php";</script>'; |
|
236
|
|
|
|
|
237
|
|
|
exit; |
|
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
if (!empty($quizQuestionInfo)) { |
|
241
|
|
|
$url = api_get_path(WEB_CODE_PATH).'exercise/exercise_submit.php?'.$quizQuestionInfo['url_params']; |
|
242
|
|
|
|
|
243
|
|
|
ChamiloSession::write(WhispeakAuthPlugin::SESSION_AUTH_PASSWORD, true); |
|
244
|
|
|
|
|
245
|
|
|
echo "<script>window.location.href = '".$url."';</script>"; |
|
246
|
|
|
|
|
247
|
|
|
exit; |
|
|
|
|
|
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
echo '<script>window.location.href = "'.api_get_path(WEB_PATH).'";</script>'; |
|
251
|
|
|
|
|
252
|
|
|
exit; |
|
|
|
|
|
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
if ($success) { |
|
256
|
|
|
ChamiloSession::erase(WhispeakAuthPlugin::SESSION_SENTENCE_TEXT); |
|
257
|
|
|
ChamiloSession::erase(WhispeakAuthPlugin::SESSION_FAILED_LOGINS); |
|
258
|
|
|
|
|
259
|
|
|
if (!empty($lpItemInfo)) { |
|
260
|
|
|
ChamiloSession::erase(WhispeakAuthPlugin::SESSION_LP_ITEM); |
|
261
|
|
|
ChamiloSession::erase(WhispeakAuthPlugin::SESSION_2FA_USER); |
|
262
|
|
|
|
|
263
|
|
|
$this->plugin->addAttemptInLearningPath( |
|
264
|
|
|
LogEvent::STATUS_SUCCESS, |
|
265
|
|
|
$user->getId(), |
|
266
|
|
|
$lpItemInfo['lp_item'], |
|
267
|
|
|
$lpItemInfo['lp'] |
|
268
|
|
|
); |
|
269
|
|
|
|
|
270
|
|
|
echo '<script>window.location.href = "'.$lpItemInfo['src'].'";</script>'; |
|
271
|
|
|
|
|
272
|
|
|
exit; |
|
|
|
|
|
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
if (!empty($quizQuestionInfo)) { |
|
276
|
|
|
$quizQuestionInfo['passed'] = true; |
|
277
|
|
|
$url = api_get_path(WEB_CODE_PATH).'exercise/exercise_submit.php?'.$quizQuestionInfo['url_params']; |
|
278
|
|
|
|
|
279
|
|
|
ChamiloSession::write(WhispeakAuthPlugin::SESSION_QUIZ_QUESTION, $quizQuestionInfo); |
|
280
|
|
|
|
|
281
|
|
|
$this->plugin->addAttemptInQuiz( |
|
282
|
|
|
LogEvent::STATUS_SUCCESS, |
|
283
|
|
|
$user->getId(), |
|
284
|
|
|
$quizQuestionInfo['question'], |
|
285
|
|
|
$quizQuestionInfo['quiz'] |
|
286
|
|
|
); |
|
287
|
|
|
|
|
288
|
|
|
echo '<script>window.location.href = "'.$url.'";</script>'; |
|
289
|
|
|
|
|
290
|
|
|
exit; |
|
|
|
|
|
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
if (empty($lpItemInfo) && empty($quizQuestionInfo)) { |
|
294
|
|
|
$this->plugin->addAuthenticationAttempt(LogEvent::STATUS_SUCCESS, $user->getId()); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
$loggedUser = [ |
|
298
|
|
|
'user_id' => $user->getId(), |
|
299
|
|
|
'status' => $user->getStatus(), |
|
300
|
|
|
'uidReset' => true, |
|
301
|
|
|
]; |
|
302
|
|
|
|
|
303
|
|
|
if (empty($user2fa)) { |
|
304
|
|
|
ChamiloSession::write(WhispeakAuthPlugin::SESSION_2FA_USER, $user->getId()); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
ChamiloSession::erase(WhispeakAuthPlugin::SESSION_FAILED_LOGINS); |
|
308
|
|
|
ChamiloSession::write('_user', $loggedUser); |
|
309
|
|
|
Login::init_user($user->getId(), true); |
|
310
|
|
|
|
|
311
|
|
|
echo '<script>window.location.href = "'.api_get_path(WEB_PATH).'";</script>'; |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* {@inheritdoc} |
|
317
|
|
|
*/ |
|
318
|
|
|
protected function displayPage($isFullPage, array $variables) |
|
319
|
|
|
{ |
|
320
|
|
|
global $htmlHeadXtra; |
|
321
|
|
|
|
|
322
|
|
|
$htmlHeadXtra[] = api_get_js('rtc/RecordRTC.js'); |
|
323
|
|
|
$htmlHeadXtra[] = api_get_js_simple(api_get_path(WEB_PLUGIN_PATH).'whispeakauth/assets/js/RecordAudio.js'); |
|
324
|
|
|
|
|
325
|
|
|
$pageTitle = $this->plugin->get_title(); |
|
326
|
|
|
|
|
327
|
|
|
$template = new \Template($pageTitle, $isFullPage, $isFullPage); |
|
328
|
|
|
|
|
329
|
|
|
foreach ($variables as $key => $value) { |
|
330
|
|
|
$template->assign($key, $value); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
$pageContent = $template->fetch('whispeakauth/view/authentify_recorder.html.twig'); |
|
334
|
|
|
|
|
335
|
|
|
$template->assign('header', $pageTitle); |
|
336
|
|
|
$template->assign('content', $pageContent); |
|
337
|
|
|
$template->display_one_col_template(); |
|
338
|
|
|
} |
|
339
|
|
|
} |
|
340
|
|
|
|