Passed
Push — master ( 02bea5...62dfbf )
by Rutger
03:04
created

UserController::actionSelectAccount()   A

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\User;
9
use Yii;
10
use yii\web\Controller;
11
12
class UserController extends Controller
13
{
14
    # region Default Yii login action with added support for OpenID Connect reauthentication
15
    /**
16
     * Allow the user to login
17
     * @param bool $reauthenticate
18
     * @param string|null $clientAuthorizationRequestId
19
     * @return string|\yii\web\Response
20
     */
21
    public function actionLogin($reauthenticate = false, $clientAuthorizationRequestId = null)
22
    {
23
        if (!Yii::$app->user->isGuest && !$reauthenticate) {
24
            return $this->goBack();
25
        }
26
27
        $model = new LoginForm();
28
        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

28
        if ($model->load(/** @scrutinizer ignore-type */ Yii::$app->request->post()) && $model->login()) {
Loading history...
29
            if ($clientAuthorizationRequestId) {
30
                Oauth2Module::getInstance()->setUserAuthenticatedDuringClientAuthRequest(
31
                    $clientAuthorizationRequestId,
32
                    true
33
                );
34
            }
35
            return $this->goBack();
36
        }
37
38
        $model->password = '';
39
        return $this->render('login', [
40
            'model' => $model,
41
        ]);
42
    }
43
    # endregion
44
45
    # region Action to support OpenID Connect account selection
46
    /**
47
     * Allow the user to select an identity
48
     * @param string $clientAuthorizationRequestId
49
     * @return string|\yii\web\Response
50
     */
51
    public function actionSelectAccount($clientAuthorizationRequestId)
52
    {
53
        /** @var User $user */
54
        $user = Yii::$app->user->identity;
55
        $model = new AccountSelectionForm([
56
            'user' => $user,
57
        ]);
58
59
        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

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