|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\Hello\Modules\AbstractModule\Controllers\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Controllers\Traits\AbstractControllerTrait; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Trait HasAuthenticationVariablesTrait |
|
9
|
|
|
* @package KM42\Hello\Modules\AbstractModule\Controllers\Traits |
|
10
|
|
|
*/ |
|
11
|
|
|
trait HasAuthenticationVariablesTrait |
|
12
|
|
|
{ |
|
13
|
|
|
use AbstractControllerTrait; |
|
14
|
|
|
|
|
15
|
|
|
protected $authenticationVariable = null; |
|
16
|
|
|
protected $authenticationValues = null; |
|
17
|
|
|
|
|
18
|
|
|
public function setAuthenticationVariablesInView() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->getView()->set('authenticationVariables', $this->getAuthenticationVariables()); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected function getAuthenticationVariables() |
|
27
|
|
|
{ |
|
28
|
|
|
if ($this->authenticationVariable === null) { |
|
29
|
|
|
$this->initAuthenticationVariables(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return $this->authenticationVariable; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function initAuthenticationVariables() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->authenticationVariable = $this->generateAuthenticationVariables(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function generateAuthenticationVariables() |
|
44
|
|
|
{ |
|
45
|
|
|
$variables = [ |
|
46
|
|
|
'redirect' => $this->getAuthenticationVarRedirect(), |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
return $variables; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return string|null |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function getAuthenticationVarRedirect() |
|
56
|
|
|
{ |
|
57
|
|
|
if (method_exists($this, 'generateAuthenticationVarRedirect')) { |
|
58
|
|
|
$redirect = $this->generateAuthenticationVarRedirect(); |
|
59
|
|
|
} else { |
|
60
|
|
|
$redirect = $this->getRequest()->query->has('redirect') |
|
61
|
|
|
? $this->getRequest()->query->get('redirect') |
|
62
|
|
|
: null; |
|
63
|
|
|
} |
|
64
|
|
|
return $this->isEncodedVariable($redirect) ? $redirect : $this->encodeVariable($redirect); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function getAuthenticationValues() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->checkInitAuthenticationValues(); |
|
74
|
|
|
return $this->authenticationValues; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function checkInitAuthenticationValues() |
|
78
|
|
|
{ |
|
79
|
|
|
if ($this->authenticationValues === null) { |
|
80
|
|
|
$this->initAuthenticationValues(); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param $key |
|
86
|
|
|
* @param null $default |
|
|
|
|
|
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function getAuthenticationValue($key, $default = null) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->checkInitAuthenticationValues(); |
|
92
|
|
|
return isset($this->authenticationValues[$key]) ? $this->authenticationValues[$key] : $default; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function initAuthenticationValues() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->authenticationValues = $this->generateAuthenticationValues(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function generateAuthenticationValues() |
|
107
|
|
|
{ |
|
108
|
|
|
$variables = $this->getAuthenticationVariables(); |
|
109
|
|
|
$return = []; |
|
110
|
|
|
foreach ($variables as $key => $value) { |
|
111
|
|
|
$return[$key] = $this->isEncodedVariable($value) ? $this->decodeVariables($value) : $value; |
|
112
|
|
|
} |
|
113
|
|
|
return $return; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param $value |
|
118
|
|
|
* @return mixed |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function decodeVariables($value) |
|
121
|
|
|
{ |
|
122
|
|
|
return base64_decode(strtr($value, '$_-', '+/=')); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param $value |
|
127
|
|
|
* @return mixed |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function encodeVariable($value) |
|
130
|
|
|
{ |
|
131
|
|
|
return strtr(base64_encode($value), '+/=', '$_-'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param $data |
|
136
|
|
|
* @return bool |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function isEncodedVariable($data) |
|
139
|
|
|
{ |
|
140
|
|
|
if ($this->encodeVariable($this->decodeVariables($data)) === $data) { |
|
141
|
|
|
return true; |
|
142
|
|
|
} |
|
143
|
|
|
return false; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|