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\models\User; |
15
|
|
|
use hisite\actions\RenderAction; |
16
|
|
|
use hisite\actions\RedirectAction; |
17
|
|
|
use Yii; |
18
|
|
|
use yii\authclient\AuthAction; |
19
|
|
|
use yii\web\BadRequestHttpException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Site controller. |
23
|
|
|
*/ |
24
|
|
|
class SiteController extends \hisite\controllers\SiteController |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function actions() |
30
|
|
|
{ |
31
|
|
|
return array_merge(parent::actions(), [ |
32
|
|
|
'auth' => [ |
33
|
|
|
'class' => AuthAction::class, |
34
|
|
|
'successCallback' => [$this, 'onAuthSuccess'], |
35
|
|
|
], |
36
|
|
|
'index' => [ |
37
|
|
|
'class' => RedirectAction::class, |
38
|
|
|
'url' => ['/dashboard/dashboard'], |
39
|
|
|
], |
40
|
|
|
'profile' => [ |
41
|
|
|
'class' => RedirectAction::class, |
42
|
|
|
'url' => function () { |
43
|
|
|
$user = Yii::$app->user; |
44
|
|
|
|
45
|
|
|
return $user->isGuest ? ['/site/login'] : ['@client/view', 'id' => $user->identity->id]; |
46
|
|
|
}, |
47
|
|
|
], |
48
|
|
|
'lockscreen' => [ |
49
|
|
|
'class' => RenderAction::class, |
50
|
|
|
], |
51
|
|
|
]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function onAuthSuccess($client) |
55
|
|
|
{ |
56
|
|
|
$attributes = $client->getUserAttributes(); |
57
|
|
|
$user = new User(); |
58
|
|
|
foreach ($user->attributes() as $k) { |
59
|
|
|
if (isset($attributes[$k])) { |
60
|
|
|
$user->{$k} = $attributes[$k]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
$user->save(); |
64
|
|
|
Yii::$app->user->login($user, 3600 * 24 * 30); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function actionLogin() |
68
|
|
|
{ |
69
|
|
|
if (!Yii::$app->user->isGuest) { |
70
|
|
|
return $this->redirect(['/site/index']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->redirect(['/site/auth', 'authclient' => 'hiam']); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function actionLogout() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$back = Yii::$app->request->getHostInfo(); |
79
|
|
|
$url = Yii::$app->authClientCollection->getClient()->buildUrl('site/logout', compact('back')); |
80
|
|
|
Yii::$app->user->logout(); |
81
|
|
|
|
82
|
|
|
return Yii::$app->response->redirect($url); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
public function actionSignup() |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$back = Yii::$app->request->getHostInfo(); |
88
|
|
|
$url = Yii::$app->authClientCollection->getClient()->buildUrl('site/signup', compact('back')); |
89
|
|
|
|
90
|
|
|
return Yii::$app->response->redirect($url); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.