Newer
Older
<?php
/**
* Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
* OPEN 2.0
*
* @see https://www.open2.0.regione.lombardia.it Developers'community
* @license GPLv3
* @license https://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3
*
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
* @package arter\amos\basic\template
* @category CategoryName
* @author Elite Division S.r.l.
*/
namespace backend\controllers;
use common\models\FirstAccessForm;
use common\models\ForgotPasswordForm;
use common\models\LoginForm;
use common\models\User;
use arter\amos\admin\models\UserProfile;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use \yii\helpers\Url;
use yii\web\Controller;
use yii\web\Cookie;
/**
* Site controller
*/
class SiteController extends Controller {
/**
* @inheritdoc
*/
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => [
'login',
'error',
'login_error',
'insert-auth-data',
'inserisci-dati-autenticazione',
'forgot-password',
'expired-login',
'language',
'privacy',
'cookies',
],
'allow' => true,
],
[
'actions' => [
'logout',
'index',
'privacy',
'cookies',
],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
//'logout' => ['post'],
],
],
];
}
/**
* @inheritdoc
*/
public function actions() {
$this->layout = '@vendor/arter/amos-layout/src/views/layouts/main';
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}
/**
* Displays homepage.
*
* @return string
*/
public function actionIndex() {
return $this->render('index');
}
/**
* Login action.
*
* @return string
*/
public function actionLogin() {
return $this->redirect(\Yii::$app->params['platform']['backendUrl'] . "/admin/security/login");
}
/**
* Logout action.
*
* @return string
*/
public function actionLogout() {
$userId = Yii::$app->getUser()->getId();
Yii::$app->user->logout();
if (\Yii::$app->params['template-amos']) {
$ids = \arter\amos\dashboard\models\AmosUserDashboards::find()->andWhere(['user_id' => $userId])->select('id');
\arter\amos\dashboard\models\AmosUserDashboardsWidgetMm::deleteAll(['IN', 'amos_user_dashboards_id', $ids]);
\arter\amos\dashboard\models\AmosUserDashboards::deleteAll(['user_id' => $userId]);
}
return $this->goHome();
}
/**
* Send Login-infos to user
* @param $id User ID
* @return mixed
*/
public function actionSpedisciCredenziali($id) {
$model = UserProfile::findOne($id);
if ($model && $model->user && $model->user->email) {
$model->user->generatePasswordResetToken();
$model->user->save(false);
$soggetto = 'Benvenuto nella piattaforma ' . Yii::$app->name;
if ($model->sesso == 'Femmina') {
$soggetto = 'Benvenuta nella piattaforma ' . Yii::$app->name;
}
$mail = Yii::$app->mailer
->compose(
[
'html' => '@vendor/arter/amos-admin/src/mail/user/credenziali-html',
'text' => '@vendor/arter/amos-admin/src/mail/user/credenziali-text'
], [
'profile' => $model,
])
->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])
->setReplyTo([Yii::$app->params['supportEmail'] => Yii::$app->name])
->setTo($model->user->email)
->setSubject($soggetto);
/* @var $message \yii\swiftmailer\Message */
if ($mail instanceof \yii\swiftmailer\Message && isset($_GET['preview'])) {
$swiftMessage = $mail->getSwiftMessage();
$r = new \ReflectionObject($swiftMessage);
$parentClassThatHasBody = $r->getParentClass()
->getParentClass()
->getParentClass(); //\Swift_Mime_SimpleMimeEntity
$body = $parentClassThatHasBody->getProperty('_immediateChildren');
$body->setAccessible(true);
$children = $body->getValue($swiftMessage);
foreach ($children as $child) {
if ($child instanceof \Swift_MimePart &&
$child->getContentType() == 'text/html'
) {
$html = $child->getBody();
break;
}
}
echo $html;
die;
}
$sent = $mail->send();
if ($sent) {
Yii::$app->session->addFlash('success', Yii::t('amosplatform', 'Credenziali spedite correttamente alla email {email}', ['email' => $model->user->email]));
} else {
Yii::$app->session->addFlash('danger', Yii::t('amosplatform', 'Si è verificato un errore durante la spedizione delle credenziali'));
}
} else {
Yii::$app->session->addFlash('danger', Yii::t('amosplatform', 'L\'utente non esiste o è sprovvisto di email, impossibile spedire le credenziali'));
}
return $this->redirect(Url::previous());
}
/**
* @deprecated
* Login-info choice at register step
* @return string
*/
public function actionInserisciDatiAutenticazione() {
$this->actionInsertAuthData();
}
/**
* Login-info choice at register step
* @return string
*/
public function actionInsertAuthData() {
$this->layout = '@vendor/arter/amos-core/views/layouts/login';
$password_reset_token = null;
$user = null;
$username = null;
if (NULL !== (Yii::$app->getRequest()->getQueryParam('token'))) {
$password_reset_token = Yii::$app->getRequest()->getQueryParam('token');
$user = User::findByPasswordResetToken($password_reset_token);
if ($user) {
$username = $user->username;
}
}
if ($user && !$username) {
if (Yii::$app->request->isPost) {
$model = new FirstAccessForm();
if ($model->load(Yii::$app->request->post())) {
if ($model->verifyUsername($model->username)) {
Yii::$app->getSession()->addFlash('danger', Yii::t('amosplatform', 'Attenzione! La username inserita è già in uso. Sceglierne un'altra.'));
return $this->render('first_access', [
'model' => $model,
]);
} else {
$user->setPassword($model->password);
$user->username = $model->username;
if ($user->validate() && $user->save()) {
Yii::$app->getSession()->addFlash('success', Yii::t('amosplatform', 'Perfetto! Hai scelto correttamente le tue credenziali, ora puoi effettuare l'accesso.'));
$user->removePasswordResetToken();
$user->save();
return $this->redirect(\Yii::$app->getUser()->loginUrl);
} else {
return $this->render('login_error', ['message' => Yii::t('amosplatform', " Errore! Il sito non ha risposto, probabilmente erano in corso operazioni di manutenzione. Riprova più tardi.")]);
}
}
} else {
$model->token = $password_reset_token;
return $this->render('first_access', [
'model' => $model,
]);
}
} else {
$model = new FirstAccessForm();
$model->token = $password_reset_token;
return $this->render('first_access', [
'model' => $model,
]);
}
} else if ($user && $username) {
if (Yii::$app->request->isPost) {
$model = new FirstAccessForm();
if ($model->load(Yii::$app->request->post())) {
$user->setPassword($model->password);
if ($user->validate() && $user->save()) {
Yii::$app->getSession()->addFlash('success', Yii::t('amosplatform', 'Perfetto! Hai scelto correttamente la tua password, ora puoi effettuare l'accesso.'));
$user->removePasswordResetToken();
$user->save();
return $this->redirect(\Yii::$app->getUser()->loginUrl);
} else {
return $this->render('login_error', ['message' => Yii::t('amosplatform', " Errore! Il sito non ha risposto, probabilmente erano in corso operazioni di manutenzione. Riprova più tardi.")]);
}
} else {
$model->token = $password_reset_token;
$model->username = $username;
return $this->render('reset_password', [
'model' => $model,
]);
}
} else {
$model = new FirstAccessForm();
$model->token = $password_reset_token;
$model->username = $username;
return $this->render('reset_password', [
'model' => $model,
]);
}
} else {
return $this->render('login_error', ['message' => Yii::t('amosplatform', " Errore! Il tempo per poter accedere è scaduto. Contatti l'amministratore e si faccia reinviare la mail di accesso.")]);
}
}
/**
* Forgotten password form
* @return string|\yii\web\Response
*/
public function actionForgotPassword() {
$this->layout = '@vendor/arter/amos-core/views/layouts/login';
if (!\Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new ForgotPasswordForm();
if ($model->load(Yii::$app->request->post())) {
Yii::$app->getSession()->addFlash('success', Yii::t('amosplatform', 'Verrà inviata una mail con i dati all\'indirizzo fornito, se presente nel sistema'));
if ($model->username != NULL) {
$dati_utente = $model->verifyUsername($model->username);
if ($dati_utente) {
$this->actionSpedisciCredenziali($dati_utente->id);
}
} else if ($model->codice_fiscale != NULL) {
$dati_utente = $model->verifySocialSecurityNumber($model->codice_fiscale);
if ($dati_utente) {
$this->actionSpedisciCredenziali($dati_utente->id);
}
}
}
return $this->render('forgot_password', [
'model' => $model,
]);
}
public function actionLanguage() {
$data = Yii::$app->request->post();
if (!empty($data['language'])) {
\Yii::$app->language = $data['language'];
$languageCookie = new Cookie([
'name' => 'language',
'value' => $data['language'],
'expire' => time() + 60 * 60 * 24 * 30, // 30 days
]);
Yii::$app->response->cookies->add($languageCookie);
}
if (!empty($data['url'])) {
return $this->redirect([$data['url']]);
}
return $this->redirect(Url::previous());
}
/* public function beforeAction($action) {
if ($action->id == 'language') {
$this->enableCsrfValidation = false;
Yii::$app->controller->enableCsrfValidation = FALSE;
}
return parent::beforeAction($action);
}
*/
/**
* This action render the privacy notice page
* @return string
*/
public function actionPrivacy()
{
return $this->render('privacy');
}
/**
* This action render the cookies page
* @return string
*/
public function actionCookies()
{
return $this->render('cookies');
}
}