1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* HiPanel core package |
5
|
|
|
* |
6
|
|
|
* @link https://hipanel.com/ |
7
|
|
|
* @package hipanel-core |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hipanel\controllers; |
13
|
|
|
|
14
|
|
|
use hipanel\components\LanguageSwitcher; |
15
|
|
|
use hipanel\models\ContactForm; |
16
|
|
|
use hipanel\models\PasswordResetRequestForm; |
17
|
|
|
use hipanel\models\ResetPasswordForm; |
18
|
|
|
use hipanel\models\User; |
19
|
|
|
use Yii; |
20
|
|
|
use yii\base\InvalidParamException; |
21
|
|
|
use yii\filters\AccessControl; |
22
|
|
|
use yii\filters\VerbFilter; |
23
|
|
|
use yii\web\BadRequestHttpException; |
24
|
|
|
use yii\web\Controller; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Site controller. |
28
|
|
|
*/ |
29
|
|
|
class SiteController extends Controller |
30
|
|
|
{ |
31
|
|
|
// public $layout = 'site'; |
|
|
|
|
32
|
|
|
|
33
|
|
|
public function behaviors() |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
'access' => [ |
37
|
|
|
'class' => AccessControl::className(), |
38
|
|
|
'only' => ['logout', 'signup', 'lockscreen'], |
39
|
|
|
'rules' => [ |
40
|
|
|
[ |
41
|
|
|
'actions' => ['signup'], |
42
|
|
|
'allow' => true, |
43
|
|
|
'roles' => ['?'], |
44
|
|
|
], |
45
|
|
|
[ |
46
|
|
|
'actions' => ['logout', 'lockscreen'], |
47
|
|
|
'roles' => ['@'], |
48
|
|
|
'allow' => true, |
49
|
|
|
], |
50
|
|
|
], |
51
|
|
|
], |
52
|
|
|
/* |
|
|
|
|
53
|
|
|
'verbs' => [ |
54
|
|
|
'class' => VerbFilter::className(), |
55
|
|
|
'actions' => [ |
56
|
|
|
'logout' => ['post'], |
57
|
|
|
], |
58
|
|
|
], |
59
|
|
|
*/ |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function actions() |
67
|
|
|
{ |
68
|
|
|
return [ |
69
|
|
|
'auth' => [ |
70
|
|
|
'class' => 'yii\authclient\AuthAction', |
71
|
|
|
'successCallback' => [$this, 'successCallback'], |
72
|
|
|
], |
73
|
|
|
'error' => [ |
74
|
|
|
'class' => 'yii\web\ErrorAction', |
75
|
|
|
], |
76
|
|
|
'captcha' => [ |
77
|
|
|
'class' => 'yii\captcha\CaptchaAction', |
78
|
|
|
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, |
79
|
|
|
], |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function successCallback($client) |
84
|
|
|
{ |
85
|
|
|
$attributes = $client->getUserAttributes(); |
86
|
|
|
$user = new User(); |
87
|
|
|
foreach ($user->attributes() as $k) { |
88
|
|
|
$user->{$k} = $attributes[$k]; |
89
|
|
|
} |
90
|
|
|
$user->save(); |
91
|
|
|
Yii::$app->user->login($user, 3600 * 24 * 30); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function actionIndex() |
95
|
|
|
{ |
96
|
|
|
return $this->redirect(['/hipanel/index']); |
97
|
|
|
// return $this->render('index'); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function actionLockscreen() |
101
|
|
|
{ |
102
|
|
|
return $this->render('lockscreen'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function actionLogin() |
106
|
|
|
{ |
107
|
|
|
if (!Yii::$app->user->isGuest) { |
108
|
|
|
return $this->redirect(['/hipanel/index']); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this->redirect(['/site/auth', 'authclient' => 'hiam']); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function actionProfile() |
115
|
|
|
{ |
116
|
|
|
return $this->redirect(['@client/view', 'id' => Yii::$app->user->identity->id]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
View Code Duplication |
public function actionLogout() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$back = Yii::$app->request->getHostInfo(); |
122
|
|
|
$url = Yii::$app->authClientCollection->getClient()->buildUrl('site/logout', compact('back')); |
123
|
|
|
Yii::$app->user->logout(); |
124
|
|
|
|
125
|
|
|
return Yii::$app->response->redirect($url); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
View Code Duplication |
public function actionSignup() |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$back = Yii::$app->request->getHostInfo(); |
131
|
|
|
$url = Yii::$app->authClientCollection->getClient()->buildUrl('site/signup', compact('back')); |
132
|
|
|
|
133
|
|
|
return Yii::$app->response->redirect($url); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function actionContact() |
137
|
|
|
{ |
138
|
|
|
$model = new ContactForm(); |
139
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
140
|
|
|
if ($model->sendEmail(Yii::$app->params['adminEmail'])) { |
141
|
|
|
Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.'); |
142
|
|
|
} else { |
143
|
|
|
Yii::$app->session->setFlash('error', 'There was an error sending email.'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $this->refresh(); |
147
|
|
|
} else { |
148
|
|
|
return $this->render('contact', [ |
149
|
|
|
'model' => $model, |
150
|
|
|
]); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function actionAbout() |
155
|
|
|
{ |
156
|
|
|
return $this->render('about'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function actionRequestPasswordReset() |
160
|
|
|
{ |
161
|
|
|
$model = new PasswordResetRequestForm(); |
162
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
163
|
|
|
if ($model->sendEmail()) { |
164
|
|
|
Yii::$app->getSession()->setFlash('success', 'Check your email for further instructions.'); |
|
|
|
|
165
|
|
|
|
166
|
|
|
return $this->goHome(); |
167
|
|
|
} else { |
168
|
|
|
Yii::$app->getSession()->setFlash('error', 'Sorry, we are unable to reset password for email provided.'); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $this->render('requestPasswordResetToken', [ |
173
|
|
|
'model' => $model, |
174
|
|
|
]); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function actionResetPassword($token) |
178
|
|
|
{ |
179
|
|
|
try { |
180
|
|
|
$model = new ResetPasswordForm($token); |
181
|
|
|
} catch (InvalidParamException $e) { |
182
|
|
|
throw new BadRequestHttpException($e->getMessage()); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) { |
186
|
|
|
Yii::$app->getSession()->setFlash('success', 'New password was saved.'); |
|
|
|
|
187
|
|
|
|
188
|
|
|
return $this->goHome(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $this->render('resetPassword', [ |
192
|
|
|
'model' => $model, |
193
|
|
|
]); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function actionLanguage($language) |
197
|
|
|
{ |
198
|
|
|
/** @var LanguageSwitcher $languageSwitcher */ |
199
|
|
|
$languageSwitcher = Yii::$app->get('languageSwitcher'); |
200
|
|
|
$languageSwitcher->setLanguage($language); |
201
|
|
|
|
202
|
|
|
$url = Yii::$app->request->referrer; |
203
|
|
|
if ($url === null) { |
204
|
|
|
$url = Yii::$app->getHomeUrl(); |
|
|
|
|
205
|
|
|
} |
206
|
|
|
return Yii::$app->response->redirect($url); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.