Completed
Push — master ( 4a8618...f2303d )
by Jeff
02:02
created

AuthController::getFromSSO()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php
2
3
namespace app\controllers;
4
5
use Yii;
6
use yii\filters\AccessControl;
7
use app\models\User;
8
use app\models\UserLogin;
9
10
/**
11
 * AuthController implements the authentication methods.
12
 */
13
class AuthController extends BaseController
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function behaviors()
19
    {
20
        return [
21
            'access' => [
22
                'class' => AccessControl::class,
23
                'only' => ['login', 'logout'],
24
                'rules' => [
25
                    ['allow' => true, 'actions' => ['login'], 'roles' => ['?']],
26
                    ['allow' => true, 'actions' => ['logout'], 'roles' => ['@']],
27
                ],
28
            ],
29
        ];
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function init()
36
    {
37
        parent::init();
38
39
        // Catch login event to afterLogin method
40
        Yii::$app->user->on(\yii\web\User::EVENT_AFTER_LOGIN, ['app\models\User', 'afterLogin']);
41
    }
42
43
    /**
44
     * Index redirects to login action.
45
     *
46
     * @return \yii\web\Response|string redirect or render
47
     */
48
    public function actionIndex()
49
    {
50
        return $this->actionLogin();
51
    }
52
53
    /**
54
     * Login an user based on SSO auth if available, else use login form
55
     * with LDAP backend if available or DB.
56
     *
57
     * @return \yii\web\Response|string redirect or render
58
     */
59
    public function actionLogin()
60
    {
61
        if (!Yii::$app->user->isGuest) {
62
            return $this->goBack();
63
        }
64
65
        // SSO auth
66
        $identity = $this->getFromSSO();
67
        if ($identity) {
68
            // Login auto saves in DB
69
            Yii::$app->user->login($identity, Yii::$app->params['cookieDuration']);
70
71
            return $this->goBack();
72
        }
73
74
        // User login form
75
        $model = new UserLogin();
76
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
77
            // Find in DB/LDAP
78
            $identity = User::findIdentity($model->username);
79
            // Authenticate
80
            if ($identity !== null && $identity->authenticate($model->password)) {
81
                Yii::$app->user->enableAutoLogin = $model->remember_me;
82
                // Login auto saves in DB
83
                Yii::$app->user->login($identity, Yii::$app->params['cookieDuration']);
84
85
                return $this->goBack();
86
            }
87
            $model->addError('username', Yii::t('app', 'Username or password incorrect'));
88
        }
89
90
        return $this->render('login', [
91
            'model' => $model,
92
        ]);
93
    }
94
95
    /**
96
     * Check for SSO configuration and try to authenticate user.
97
     *
98
     * @return \app\models\User|null found user
99
     */
100
    private function getFromSSO()
101
    {
102
        // SSO auth
103
        if (Yii::$app->params['useSSO'] && isset($_SERVER[Yii::$app->params['ssoEnvUsername']])) {
104
            $username = $_SERVER[Yii::$app->params['ssoEnvUsername']];
105
106
            // Find in DB/LDAP
107
            return User::findIdentity($username);
108
        }
109
    }
110
111
    /**
112
     * Disconnects current user.
113
     *
114
     * @return \yii\web\Response
115
     */
116
    public function actionLogout()
117
    {
118
        if (Yii::$app->user->isGuest) {
119
            return $this->goBack();
120
        }
121
122
        Yii::$app->user->logout();
123
124
        return $this->goHome();
125
    }
126
}
127