|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Da\User\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use Da\User\Contracts\AuthClientInterface; |
|
15
|
|
|
use Da\User\Event\FormEvent; |
|
16
|
|
|
use Da\User\Event\UserEvent; |
|
17
|
|
|
use Da\User\Form\LoginForm; |
|
18
|
|
|
use Da\User\Query\SocialNetworkAccountQuery; |
|
19
|
|
|
use Da\User\Service\SocialNetworkAccountConnectService; |
|
20
|
|
|
use Da\User\Service\SocialNetworkAuthenticateService; |
|
21
|
|
|
use Da\User\Traits\ContainerAwareTrait; |
|
22
|
|
|
use Yii; |
|
23
|
|
|
use yii\authclient\AuthAction; |
|
24
|
|
|
use yii\base\Module; |
|
25
|
|
|
use yii\filters\AccessControl; |
|
26
|
|
|
use yii\filters\VerbFilter; |
|
27
|
|
|
use yii\web\Controller; |
|
28
|
|
|
use yii\web\Response; |
|
29
|
|
|
use yii\widgets\ActiveForm; |
|
30
|
|
|
|
|
31
|
|
|
class SecurityController extends Controller |
|
32
|
|
|
{ |
|
33
|
|
|
use ContainerAwareTrait; |
|
34
|
|
|
|
|
35
|
|
|
protected $socialNetworkAccountQuery; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* SecurityController constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $id |
|
41
|
|
|
* @param Module $module |
|
42
|
|
|
* @param SocialNetworkAccountQuery $socialNetworkAccountQuery |
|
43
|
|
|
* @param array $config |
|
44
|
|
|
*/ |
|
45
|
5 |
|
public function __construct( |
|
46
|
|
|
$id, |
|
47
|
|
|
Module $module, |
|
48
|
|
|
SocialNetworkAccountQuery $socialNetworkAccountQuery, |
|
49
|
|
|
array $config = [] |
|
50
|
|
|
) { |
|
51
|
5 |
|
$this->socialNetworkAccountQuery = $socialNetworkAccountQuery; |
|
52
|
5 |
|
parent::__construct($id, $module, $config); |
|
53
|
5 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
5 |
|
public function behaviors() |
|
59
|
|
|
{ |
|
60
|
|
|
return [ |
|
61
|
|
|
'access' => [ |
|
62
|
5 |
|
'class' => AccessControl::className(), |
|
63
|
|
|
'rules' => [ |
|
64
|
|
|
[ |
|
65
|
|
|
'allow' => true, |
|
66
|
|
|
'actions' => ['login', 'auth', 'blocked'], |
|
67
|
|
|
'roles' => ['?'], |
|
68
|
|
|
], |
|
69
|
|
|
[ |
|
70
|
|
|
'allow' => true, |
|
71
|
|
|
'actions' => ['login', 'auth', 'logout'], |
|
72
|
|
|
'roles' => ['@'], |
|
73
|
|
|
], |
|
74
|
|
|
], |
|
75
|
5 |
|
], |
|
76
|
|
|
'verbs' => [ |
|
77
|
5 |
|
'class' => VerbFilter::className(), |
|
78
|
|
|
'actions' => [ |
|
79
|
|
|
'logout' => ['post'], |
|
80
|
|
|
], |
|
81
|
|
|
], |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
5 |
|
public function actions() |
|
89
|
|
|
{ |
|
90
|
|
|
return [ |
|
91
|
|
|
'auth' => [ |
|
92
|
5 |
|
'class' => AuthAction::className(), |
|
93
|
|
|
// if user is not logged in, will try to log him in, otherwise |
|
94
|
|
|
// will try to connect social account to user. |
|
95
|
5 |
|
'successCallback' => Yii::$app->user->isGuest |
|
96
|
5 |
|
? [$this, 'authenticate'] |
|
97
|
5 |
|
: [$this, 'connect'], |
|
98
|
|
|
], |
|
99
|
|
|
]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Controller action responsible for handling login page and actions. |
|
104
|
|
|
* @return string|Response |
|
105
|
|
|
*/ |
|
106
|
5 |
|
public function actionLogin() |
|
107
|
|
|
{ |
|
108
|
5 |
|
if (!Yii::$app->user->getIsGuest()) { |
|
109
|
|
|
return $this->goHome(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** @var LoginForm $form */ |
|
113
|
5 |
|
$form = $this->make(LoginForm::class); |
|
114
|
|
|
/** @var FormEvent $event */ |
|
115
|
5 |
|
$event = $this->make(FormEvent::class, [$form]); |
|
116
|
|
|
|
|
117
|
5 |
|
if (Yii::$app->request->isAjax && $form->load(Yii::$app->request->post())) { |
|
118
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
|
119
|
|
|
return ActiveForm::validate($form); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
5 |
|
if ($form->load(Yii::$app->request->post())) { |
|
123
|
5 |
|
$this->trigger(FormEvent::EVENT_BEFORE_LOGIN, $event); |
|
124
|
5 |
|
if ($form->login()) { |
|
125
|
5 |
|
$this->trigger(FormEvent::EVENT_AFTER_LOGIN, $event); |
|
126
|
|
|
|
|
127
|
5 |
|
return $this->goBack(); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
5 |
|
return $this->render( |
|
132
|
5 |
|
'login', |
|
133
|
|
|
[ |
|
134
|
5 |
|
'model' => $form, |
|
135
|
5 |
|
'module' => $this->module, |
|
136
|
|
|
] |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function actionLogout() |
|
141
|
|
|
{ |
|
142
|
|
|
$event = $this->make(UserEvent::class, [Yii::$app->getUser()->getIdentity()]); |
|
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
$this->trigger(UserEvent::EVENT_BEFORE_LOGOUT, $event); |
|
145
|
|
|
|
|
146
|
|
|
if (Yii::$app->getUser()->logout()) { |
|
147
|
|
|
$this->trigger(UserEvent::EVENT_AFTER_LOGOUT, $event); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $this->goHome(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function authenticate(AuthClientInterface $client) |
|
154
|
|
|
{ |
|
155
|
|
|
$this->make(SocialNetworkAuthenticateService::class, [$this, $this->action, $client])->run(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function connect(AuthClientInterface $client) |
|
159
|
|
|
{ |
|
160
|
|
|
if (Yii::$app->user->isGuest) { |
|
161
|
|
|
Yii::$app->session->setFlash('danger', Yii::t('user', 'Something went wrong')); |
|
162
|
|
|
|
|
163
|
|
|
return; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$this->make(SocialNetworkAccountConnectService::class, [$this, $client])->run(); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: