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