SecurityController::actionIndex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\user\web\user\controllers;
14
15
use rhosocial\user\forms\ChangePasswordForm;
16
use Yii;
17
use yii\filters\AccessControl;
18
use yii\web\Controller;
19
20
/**
21
 * @version 1.0
22
 * @author vistart <[email protected]>
23
 */
24
class SecurityController extends Controller
25
{
26
    const SESSION_KEY_CHANGE_PASSWORD_RESULT = 'session_key_change_password_result';
27
    const SESSION_KEY_CHANGE_PASSWORD_MESSAGE = 'session_key_change_password_message';
28
    const CHANGE_PASSWORD_SUCCESS = 'success';
29
    const CHANGE_PASSWORD_FAILED = 'failed';
30
31
    public $changePasswordSuccessMessage;
32
33
    public $changePasswordFailedMessage;
34
35
    public $layout = 'security';
36
37
    protected function initMessages()
38
    {
39
        if (empty($this->changePasswordFailedMessage)) {
40
            $this->changePasswordFailedMessage = Yii::t('user', 'Password Not Changed.');
41
        }
42
        if (empty($this->changePasswordSuccessMessage)) {
43
            $this->changePasswordSuccessMessage = Yii::t('user', 'Password Changed.');
44
        }
45
    }
46
47
    public function init()
48
    {
49
        $this->initMessages();
50
        parent::init();
51
    }
52
53
    public function behaviors()
54
    {
55
        return [
56
            'access' => [
57
                'class' => AccessControl::class,
58
                'rules' => [
59
                    [
60
                        'actions' => ['index', 'change-password', 'login-log'],
61
                        'allow' => true,
62
                        'roles' => ['@'],
63
                    ]
64
                ],
65
            ],
66
        ];
67
    }
68
69
    public function actionIndex()
70
    {
71
        return $this->render('index');
72
    }
73
74
    public function actionChangePassword()
75
    {
76
        $model = new ChangePasswordForm(['user' => Yii::$app->user->identity]);
77
        if ($model->load(Yii::$app->request->post())) {
78
            if ($model->changePassword()) {
79
                Yii::$app->session->setFlash(self::SESSION_KEY_CHANGE_PASSWORD_RESULT, self::CHANGE_PASSWORD_SUCCESS);
80
                Yii::$app->session->setFlash(self::SESSION_KEY_CHANGE_PASSWORD_MESSAGE, $this->changePasswordSuccessMessage);
81
                return $this->redirect(['change-password']);
82
            }
83
            Yii::$app->session->setFlash(self::SESSION_KEY_CHANGE_PASSWORD_RESULT, self::CHANGE_PASSWORD_FAILED);
84
            Yii::$app->session->setFlash(self::SESSION_KEY_CHANGE_PASSWORD_MESSAGE, $this->changePasswordFailedMessage);
85
            $model->clearAttributes();
86
        }
87
        return $this->render('change-password', ['model' => $model]);
88
    }
89
90
    public function actionLoginLog()
91
    {
92
        return $this->render('login-log');
93
    }
94
}
95