Passed
Push — master ( fb60c6...a721e7 )
by Rutger
03:14
created

UserController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 25
c 0
b 0
f 0
dl 0
loc 65
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A actionSelectAccount() 0 19 3
A actionIndex() 0 4 1
A actionLogin() 0 20 6
1
<?php
2
3
namespace sample\controllers\web;
4
5
use rhertogh\Yii2Oauth2Server\Oauth2Module;
6
use sample\models\AccountSelectionForm;
7
use sample\models\LoginForm;
8
use sample\models\User;
9
use Yii;
10
use yii\web\Controller;
11
12
class UserController extends Controller
13
{
14
    public function actionIndex()
15
    {
16
        return $this->render('index', [
17
            'user' => Yii::$app->user->identity
18
        ]);
19
    }
20
21
    # region Default Yii login action with added support for OpenID Connect reauthentication
22
    /**
23
     * Allow the user to login
24
     * @param bool $reauthenticate
25
     * @param string|null $clientAuthorizationRequestId
26
     * @return string|\yii\web\Response
27
     */
28
    public function actionLogin($reauthenticate = false, $clientAuthorizationRequestId = null)
29
    {
30
        if (!Yii::$app->user->isGuest && !$reauthenticate) {
31
            return $this->goBack();
32
        }
33
34
        $model = new LoginForm();
35
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
0 ignored issues
show
Bug introduced by
It seems like Yii::app->request->post() can also be of type object; however, parameter $data of yii\base\Model::load() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        if ($model->load(/** @scrutinizer ignore-type */ Yii::$app->request->post()) && $model->login()) {
Loading history...
36
            if ($clientAuthorizationRequestId) {
37
                Oauth2Module::getInstance()->setUserAuthenticatedDuringClientAuthRequest(
38
                    $clientAuthorizationRequestId,
39
                    true
40
                );
41
            }
42
            return $this->goBack();
43
        }
44
45
        $model->password = '';
46
        return $this->render('login', [
47
            'model' => $model,
48
        ]);
49
    }
50
    # endregion
51
52
    # region Action to support OpenID Connect account selection
53
    /**
54
     * Allow the user to select an identity
55
     * @param string $clientAuthorizationRequestId
56
     * @return string|\yii\web\Response
57
     */
58
    public function actionSelectAccount($clientAuthorizationRequestId)
59
    {
60
        /** @var User $user */
61
        $user = Yii::$app->user->identity;
62
        $model = new AccountSelectionForm([
63
            'user' => $user,
64
        ]);
65
66
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
0 ignored issues
show
Bug introduced by
It seems like Yii::app->request->post() can also be of type object; however, parameter $data of yii\base\Model::load() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
        if ($model->load(/** @scrutinizer ignore-type */ Yii::$app->request->post()) && $model->validate()) {
Loading history...
67
            Oauth2Module::getInstance()->setClientAuthRequestUserIdentity(
68
                $clientAuthorizationRequestId,
69
                $user->getLinkedIdentity($model->identityId)
70
            );
71
72
            return $this->goBack();
73
        }
74
75
        return $this->render('select-account', [
76
            'model' => $model,
77
        ]);
78
    }
79
    # endregion
80
}
81