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\MailChangeStrategyInterface; |
15
|
|
|
use Da\User\Event\FormEvent; |
16
|
|
|
use Da\User\Event\ProfileEvent; |
17
|
|
|
use Da\User\Event\SocialNetworkConnectEvent; |
18
|
|
|
use Da\User\Event\UserEvent; |
19
|
|
|
use Da\User\Form\SettingsForm; |
20
|
|
|
use Da\User\Model\Profile; |
21
|
|
|
use Da\User\Model\SocialNetworkAccount; |
22
|
|
|
use Da\User\Model\User; |
23
|
|
|
use Da\User\Module; |
24
|
|
|
use Da\User\Query\ProfileQuery; |
25
|
|
|
use Da\User\Query\SocialNetworkAccountQuery; |
26
|
|
|
use Da\User\Query\UserQuery; |
27
|
|
|
use Da\User\Service\EmailChangeService; |
28
|
|
|
use Da\User\Traits\ContainerAwareTrait; |
29
|
|
|
use Da\User\Validator\AjaxRequestModelValidator; |
30
|
|
|
use Yii; |
31
|
|
|
use yii\filters\AccessControl; |
32
|
|
|
use yii\filters\VerbFilter; |
33
|
|
|
use yii\web\Controller; |
34
|
|
|
use yii\web\ForbiddenHttpException; |
35
|
|
|
use yii\web\NotFoundHttpException; |
36
|
|
|
|
37
|
|
|
class SettingsController extends Controller |
38
|
|
|
{ |
39
|
|
|
use ContainerAwareTrait; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public $defaultAction = 'profile'; |
45
|
|
|
|
46
|
|
|
protected $profileQuery; |
47
|
|
|
protected $userQuery; |
48
|
|
|
protected $socialNetworkAccountQuery; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* SettingsController constructor. |
52
|
|
|
* |
53
|
|
|
* @param string $id |
54
|
|
|
* @param Module $module |
55
|
|
|
* @param ProfileQuery $profileQuery |
56
|
|
|
* @param UserQuery $userQuery |
57
|
|
|
* @param SocialNetworkAccountQuery $socialNetworkAccountQuery |
58
|
|
|
* @param array $config |
59
|
|
|
*/ |
60
|
1 |
|
public function __construct( |
61
|
|
|
$id, |
62
|
|
|
Module $module, |
63
|
|
|
ProfileQuery $profileQuery, |
64
|
|
|
UserQuery $userQuery, |
65
|
|
|
SocialNetworkAccountQuery $socialNetworkAccountQuery, |
66
|
|
|
array $config = [] |
67
|
|
|
) { |
68
|
1 |
|
$this->profileQuery = $profileQuery; |
69
|
1 |
|
$this->userQuery = $userQuery; |
70
|
1 |
|
$this->socialNetworkAccountQuery = $socialNetworkAccountQuery; |
71
|
1 |
|
parent::__construct($id, $module, $config); |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
1 |
|
public function behaviors() |
78
|
|
|
{ |
79
|
|
|
return [ |
80
|
1 |
|
'verbs' => [ |
81
|
1 |
|
'class' => VerbFilter::className(), |
82
|
|
|
'actions' => [ |
83
|
|
|
'disconnect' => ['post'], |
84
|
|
|
'delete' => ['post'], |
85
|
|
|
], |
86
|
|
|
], |
87
|
|
|
'access' => [ |
88
|
1 |
|
'class' => AccessControl::className(), |
89
|
|
|
'rules' => [ |
90
|
|
|
[ |
91
|
|
|
'allow' => true, |
92
|
|
|
'actions' => ['profile', 'account', 'networks', 'disconnect', 'delete'], |
93
|
|
|
'roles' => ['@'], |
94
|
|
|
], |
95
|
|
|
[ |
96
|
|
|
'allow' => true, |
97
|
|
|
'actions' => ['confirm'], |
98
|
|
|
'roles' => ['?', '@'], |
99
|
|
|
], |
100
|
|
|
], |
101
|
|
|
], |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function actionProfile() |
106
|
|
|
{ |
107
|
|
|
$profile = $this->profileQuery->whereUserId(Yii::$app->user->identity->getId())->one(); |
108
|
|
|
|
109
|
|
|
if ($profile === null) { |
110
|
|
|
$profile = $this->make(Profile::class); |
111
|
|
|
$profile->link('user', Yii::$app->user->identity); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$event = $this->make(ProfileEvent::class, [$profile]); |
115
|
|
|
|
116
|
|
|
$this->make(AjaxRequestModelValidator::class, [$profile])->validate(); |
117
|
|
|
|
118
|
|
|
if ($profile->load(Yii::$app->request->post())) { |
119
|
|
|
$this->trigger(UserEvent::EVENT_BEFORE_PROFILE_UPDATE, $event); |
120
|
|
|
if ($profile->save()) { |
121
|
|
|
Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'Your profile has been updated')); |
|
|
|
|
122
|
|
|
$this->trigger(UserEvent::EVENT_AFTER_PROFILE_UPDATE, $event); |
123
|
|
|
|
124
|
|
|
return $this->refresh(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->render( |
129
|
|
|
'profile', |
130
|
|
|
[ |
131
|
|
|
'model' => $profile, |
132
|
|
|
] |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
public function actionAccount() |
137
|
|
|
{ |
138
|
|
|
/** @var SettingsForm $form */ |
139
|
1 |
|
$form = $this->make(SettingsForm::class); |
140
|
1 |
|
$event = $this->make(UserEvent::class, [$form->getUser()]); |
141
|
|
|
|
142
|
1 |
|
$this->make(AjaxRequestModelValidator::class, [$form])->validate(); |
143
|
|
|
|
144
|
1 |
|
if ($form->load(Yii::$app->request->post())) { |
145
|
1 |
|
$this->trigger(UserEvent::EVENT_BEFORE_ACCOUNT_UPDATE, $event); |
146
|
|
|
|
147
|
1 |
|
if ($form->save()) { |
148
|
1 |
|
Yii::$app->getSession()->setFlash( |
|
|
|
|
149
|
1 |
|
'success', |
150
|
1 |
|
Yii::t('usuario', 'Your account details have been updated') |
151
|
|
|
); |
152
|
1 |
|
$this->trigger(UserEvent::EVENT_AFTER_ACCOUNT_UPDATE, $event); |
153
|
|
|
|
154
|
1 |
|
return $this->refresh(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
return $this->render( |
159
|
1 |
|
'account', |
160
|
|
|
[ |
161
|
1 |
|
'model' => $form, |
162
|
|
|
] |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function actionConfirm($id, $code) |
167
|
|
|
{ |
168
|
|
|
$user = $this->userQuery->whereId($id)->one(); |
169
|
|
|
|
170
|
|
|
if ($user === null || $this->module->emailChangeStrategy == MailChangeStrategyInterface::TYPE_INSECURE) { |
171
|
|
|
throw new NotFoundHttpException(); |
172
|
|
|
} |
173
|
|
|
$event = $this->make(UserEvent::class, [$user]); |
174
|
|
|
|
175
|
|
|
$this->trigger(UserEvent::EVENT_BEFORE_CONFIRMATION, $event); |
176
|
|
|
if ($this->make(EmailChangeService::class, [$code, $user])->run()) { |
177
|
|
|
$this->trigger(UserEvent::EVENT_AFTER_CONFIRMATION, $event); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $this->redirect(['account']); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function actionNetworks() |
184
|
|
|
{ |
185
|
|
|
return $this->render( |
186
|
|
|
'networks', |
187
|
|
|
[ |
188
|
|
|
'user' => Yii::$app->user->identity, |
189
|
|
|
] |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function actionDisconnect($id) |
194
|
|
|
{ |
195
|
|
|
/** @var SocialNetworkAccount $account */ |
196
|
|
|
$account = $this->socialNetworkAccountQuery->whereId($id)->one(); |
197
|
|
|
|
198
|
|
|
if ($account === null) { |
199
|
|
|
throw new NotFoundHttpException(); |
200
|
|
|
} |
201
|
|
|
if ($account->user_id != Yii::$app->user->id) { |
202
|
|
|
throw new ForbiddenHttpException(); |
203
|
|
|
} |
204
|
|
|
$event = $this->make(SocialNetworkConnectEvent::class, [Yii::$app->user->identity, $account]); |
205
|
|
|
|
206
|
|
|
$this->trigger(SocialNetworkConnectEvent::EVENT_BEFORE_DISCONNECT, $event); |
207
|
|
|
$account->delete(); |
208
|
|
|
$this->trigger(SocialNetworkConnectEvent::EVENT_AFTER_DISCONNECT, $event); |
209
|
|
|
|
210
|
|
|
return $this->redirect(['networks']); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function actionDelete() |
214
|
|
|
{ |
215
|
|
|
if (!$this->module->allowAccountDelete) { |
216
|
|
|
throw new NotFoundHttpException(\Yii::t('usuario', 'Not found')); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** @var User $user */ |
220
|
|
|
$user = Yii::$app->user->identity; |
221
|
|
|
$event = $this->make(UserEvent::class, [$user]); |
222
|
|
|
Yii::$app->user->logout(); |
223
|
|
|
|
224
|
|
|
$this->trigger(UserEvent::EVENT_BEFORE_DELETE, $event); |
225
|
|
|
$user->delete(); |
226
|
|
|
$this->trigger(UserEvent::EVENT_AFTER_DELETE, $event); |
227
|
|
|
|
228
|
|
|
Yii::$app->session->setFlash('info', Yii::t('usuario', 'Your account has been completely deleted')); |
229
|
|
|
|
230
|
|
|
return $this->goHome(); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
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: