UserComponent::accountSelectionRequired()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace sample\components;
4
5
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\user\Oauth2OidcUserComponentInterface;
6
use Yii;
7
use yii\db\Exception;
8
use yii\web\User;
9
10
// phpcs:disable Generic.Files.LineLength.TooLong -- Sample documentation
11
class UserComponent extends User implements
12
    Oauth2OidcUserComponentInterface # Optional interface, only required when 'Open ID Connect' is used.
13
{
14
    // phpcs:enable Generic.Files.LineLength.TooLong
15
16
    # region Oauth2OidcUserComponentInterface
17
    /**
18
     * @inheritDoc
19
     */
20
    public function reauthenticationRequired($clientAuthorizationRequest)
21
    {
22
        return Yii::$app->response->redirect([
23
            'user/login',
24
            'reauthenticate' => true,
25
            'clientAuthorizationRequestId' => $clientAuthorizationRequest->getRequestId(),
26
        ]);
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32
    public function accountSelectionRequired($clientAuthorizationRequest)
33
    {
34
        return Yii::$app->response->redirect([
35
            'user/select-account',
36
            'clientAuthorizationRequestId' => $clientAuthorizationRequest->getRequestId(),
37
        ]);
38
    }
39
    # endregion Oauth2OidcUserComponentInterface
40
41
    # region Updates User's `latest_authenticated_at`
42
    # which is used for Oauth2OidcUserInterface::getLatestAuthenticatedAt()
43
    /**
44
     * @inheritDoc
45
     * @param \sample\models\User $identity
46
     */
47
    protected function afterLogin($identity, $cookieBased, $duration)
48
    {
49
        parent::afterLogin($identity, $cookieBased, $duration);
50
51
        $identity->latest_authenticated_at = time();
52
        if (!$identity->save()) {
53
            throw new Exception('Could not save user.');
54
        }
55
    }
56
    # endregion
57
}
58