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()) { |
|
|
|
|
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()) { |
|
|
|
|
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
|
|
|
|