-
Notifications
You must be signed in to change notification settings - Fork 39
/
Component.php
124 lines (112 loc) · 4.87 KB
/
Component.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
class Kwc_User_Login_Component extends Kwc_Abstract_Composite_Component
{
public static function getSettings($param = null)
{
$ret = parent::getSettings($param);
$ret['generators']['child']['component']['form'] = 'Kwc_User_Login_Form_Component';
$ret['rootElementClass'] = 'kwfUp-webStandard';
$ret['plugins'] = array('Kwc_User_Login_Plugin');
$ret['flags']['processInput'] = true;
$ret['componentName'] = trlKwfStatic('Login Component');
return $ret;
}
private function _getRedirectBackUrl()
{
$redirectBackUrl = Kwf_Controller_Front::getInstance()->getRouter()->assemble(array(
'controller' => 'login',
'action' => 'redirect-callback',
), 'kwf_user');
$redirectBackUrl = 'http'.(isset($_SERVER['HTTPS']) ? 's' : '').'://'
.$_SERVER['HTTP_HOST']
.$redirectBackUrl;
return $redirectBackUrl;
}
public function preProcessInput($postData)
{
if (isset($postData['redirectAuth'])) {
$authMethods = Kwf_Registry::get('userModel')->getAuthMethods();
if (!isset($authMethods[$postData['redirectAuth']])) throw new Kwf_Exception_NotFound();
$auth = $authMethods[$postData['redirectAuth']];
if (!$auth instanceof Kwf_User_Auth_Interface_Redirect) throw new Kwf_Exception_NotFound();
$redirectBackUrl = $_GET['redirect'];
$f = new Kwf_Filter_StrongRandom();
$state = 'login.'.$postData['redirectAuth'].'.'.$f->filter(null).'.'.urlencode(str_replace('.', 'kwfdot', $redirectBackUrl));
//save state in cookie to validate it later
setcookie("kwf-login-redirect", $state, 0, '/', "", false, true);
$url = $auth->getLoginRedirectUrl($this->_getRedirectBackUrl(), $state, $postData);
if ($url) {
header("Location: ".$url);
exit;
} else {
echo $auth->getLoginRedirectHtml($this->_getRedirectBackUrl(), $state, $postData);
exit;
}
}
if ($postData != array() && array_keys($postData) != array('redirect')) {
$user = null;
foreach (Kwf_Registry::get('userModel')->getAuthMethods() as $auth) {
if ($auth instanceof Kwf_User_Auth_Interface_Redirect) {
$user = $auth->getUserToLoginByParams($postData);
if ($user) break;
}
}
if ($user) {
Kwf_Registry::get('userModel')->loginUserRow($user, true);
$url = $this->_getUrlForRedirect($postData, $user);
Kwf_Util_Redirect::redirect($url);
}
}
}
public final function getUrlForRedirect($postData, $user) {
return $this->_getUrlForRedirect($postData, $user);
}
protected function _getUrlForRedirect($postData, $user)
{
if (!empty($postData['redirect']) && substr($postData['redirect'], 0, 1) == '/') {
$url = $postData['redirect'];
} else {
$url = Kwf_Component_Data_Root::getInstance()
->getChildPage(array('home' => true, 'subroot' => $this->getData()), array())
->url;
}
return $url;
}
public function getTemplateVars(Kwf_Component_Renderer_Abstract $renderer)
{
$ret = parent::getTemplateVars($renderer);
$ret['register'] = $this->_getRegisterComponent();
$ret['lostPassword'] = $this->_getLostPasswordComponent();
$ret['redirects'] = array();
foreach (Kwf_Registry::get('userModel')->getAuthMethods() as $authKey=>$auth) {
if ($auth instanceof Kwf_User_Auth_Interface_Redirect && $auth->showInFrontend()) {
$label = $auth->getLoginRedirectLabel();
$ret['redirects'][] = array(
'url' => $this->getData()->url,
'authMethod' => $authKey,
'redirect' => '%redirect%',
'name' => $this->getData()->trlStaticExecute($label['name']),
'icon' => isset($label['icon']) ? '/assets/'.$label['icon'] : false,
'formOptionsHtml' => Kwf_User_Auth_Helper::getRedirectFormOptionsHtml($auth->getLoginRedirectFormOptions()),
);
}
}
return $ret;
}
protected function _getRegisterComponent()
{
return Kwf_Component_Data_Root::getInstance()
->getComponentByClass(
'Kwc_User_Register_Component',
array('subroot' => $this->getData())
);
}
protected function _getLostPasswordComponent()
{
return Kwf_Component_Data_Root::getInstance()
->getComponentByClass(
'Kwc_User_LostPassword_Component',
array('subroot' => $this->getData())
);
}
}