UserController::actionSelectAccount()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 19
rs 9.9332
cc 3
nc 2
nop 1
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\RegisterForm;
9
use sample\models\User;
10
use Yii;
11
use yii\web\Controller;
12
use yii\web\Response;
13
use yii\widgets\ActiveForm;
14
15
class UserController extends Controller
16
{
17
    public function actionIndex()
18
    {
19
        return $this->render('index', [
20
            'user' => Yii::$app->user->identity
21
        ]);
22
    }
23
24
    # region Default Yii login action with added support for OpenID Connect reauthentication
25
    /**
26
     * Allow the user to login
27
     * @param bool $reauthenticate
28
     * @param string|null $clientAuthorizationRequestId
29
     * @return string|\yii\web\Response
30
     */
31
    public function actionLogin($reauthenticate = false, $clientAuthorizationRequestId = null)
32
    {
33
        if (!Yii::$app->user->isGuest && !$reauthenticate) {
34
            return $this->goBack();
35
        }
36
37
        $model = new LoginForm();
38
        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

38
        if ($model->load(/** @scrutinizer ignore-type */ Yii::$app->request->post()) && $model->login()) {
Loading history...
39
            if ($clientAuthorizationRequestId) {
40
                Oauth2Module::getInstance()->setUserAuthenticatedDuringClientAuthRequest(
41
                    $clientAuthorizationRequestId,
42
                    true
43
                );
44
            }
45
            return $this->goBack();
46
        }
47
48
        $model->password = '';
49
        return $this->render('login', [
50
            'model' => $model,
51
        ]);
52
    }
53
    # endregion
54
55
    # region Sample register action.
56
    /**
57
     * @return \yii\web\Response|array|string
58
     */
59
    public function actionRegister()
60
    {
61
        $model = new RegisterForm();
62
        if ($model->load(Yii::$app->request->post()))
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

62
        if ($model->load(/** @scrutinizer ignore-type */ Yii::$app->request->post()))
Loading history...
63
        {
64
            if (Yii::$app->request->isAjax) {
65
                Yii::$app->response->format = Response::FORMAT_JSON;
66
                return ActiveForm::validate($model);
67
            }
68
69
            $user = $model->register();
70
            if ($user) {
71
                Yii::$app->user->login($user);
0 ignored issues
show
Bug introduced by
The method login() does not exist on null. ( Ignorable by Annotation )

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

71
                Yii::$app->user->/** @scrutinizer ignore-call */ 
72
                                 login($user);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
                return $this->goBack();
73
            }
74
        }
75
76
        return $this->render('register', [
77
            'model' => $model,
78
        ]);
79
    }
80
    # endregion
81
82
    # region Action to support OpenID Connect account selection
83
    /**
84
     * Allow the user to select an identity
85
     * @param string $clientAuthorizationRequestId
86
     * @return string|\yii\web\Response
87
     */
88
    public function actionSelectAccount($clientAuthorizationRequestId)
89
    {
90
        /** @var User $user */
91
        $user = Yii::$app->user->identity;
92
        $model = new AccountSelectionForm([
93
            'user' => $user,
94
        ]);
95
96
        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

96
        if ($model->load(/** @scrutinizer ignore-type */ Yii::$app->request->post()) && $model->validate()) {
Loading history...
97
            Oauth2Module::getInstance()->setClientAuthRequestUserIdentity(
98
                $clientAuthorizationRequestId,
99
                $user->getLinkedIdentity($model->identityId)
100
            );
101
102
            return $this->goBack();
103
        }
104
105
        return $this->render('select-account', [
106
            'model' => $model,
107
        ]);
108
    }
109
    # endregion
110
}
111