1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2011-2012 Andreas Heigl<[email protected]> |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
6
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
7
|
|
|
* in the Software without restriction, including without limitation the rights |
8
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
10
|
|
|
* furnished to do so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included in |
13
|
|
|
* all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21
|
|
|
* THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
* @category HybridAuth |
24
|
|
|
* @author Andreas Heigl<[email protected]> |
25
|
|
|
* @copyright 2011-2012 Andreas Heigl |
26
|
|
|
* @license http://www.opesource.org/licenses/mit-license.php MIT-License |
27
|
|
|
* @version 0.0 |
28
|
|
|
* @since 05.12.2012 |
29
|
|
|
* @link http://github.com/heiglandreas/OrgHeiglHybridAuth |
30
|
|
|
*/ |
31
|
|
|
namespace OrgHeiglHybridAuth\View\Helper; |
32
|
|
|
|
33
|
|
|
use OrgHeiglHybridAuth\UserToken; |
34
|
|
|
use Zend\View\Helper\AbstractHelper; |
35
|
|
|
use Zend\View\Helper\Url; |
36
|
|
|
use Zend\ServiceManager\ServiceLocatorAwareInterface; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* A view helper that either generates a link to a login-widget or a logout-link |
40
|
|
|
* |
41
|
|
|
* @category HybridAuth |
42
|
|
|
* @author Andreas Heigl<[email protected]> |
43
|
|
|
* @copyright 2011-2012 Andreas Heigl |
44
|
|
|
* @license http://www.opesource.org/licenses/mit-license.php MIT-License |
45
|
|
|
* @version 0.0 |
46
|
|
|
* @since 05.12.2012 |
47
|
|
|
* @link http://github.com/heiglandreas/OrgHeiglHybridAuth |
48
|
|
|
*/ |
49
|
|
|
class HybridAuth extends AbstractHelper |
50
|
|
|
{ |
51
|
|
|
protected $config; |
52
|
|
|
|
53
|
|
|
protected $token; |
54
|
|
|
|
55
|
|
|
protected $urlHelper; |
56
|
|
|
|
57
|
|
|
public function __construct($config, UserToken $authToken, Url $urlHelper) |
58
|
|
|
{ |
59
|
|
|
$this->config = $config; |
60
|
|
|
$this->token = $authToken; |
61
|
|
|
$this->urlHelper = $urlHelper; |
62
|
|
|
} |
63
|
|
|
/** |
64
|
|
|
* create a link to either |
65
|
|
|
* |
66
|
|
|
* @param string $provider The provider to be used |
67
|
|
|
* @param string $route The route to redirect to |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function __invoke($provider = null, $route = '') |
72
|
|
|
{ |
73
|
|
|
$route = base64_encode($route); |
74
|
|
|
$providers = (array) $this->config['backend']; |
75
|
|
|
$urlHelper = $this->urlHelper; |
76
|
|
|
|
77
|
|
|
if ($this->token->isAuthenticated()) { |
78
|
|
|
// Display Logged in information |
79
|
|
|
|
80
|
|
|
$user = sprintf($this->config['logoffstring'], $this->token->getDisplayName()); |
81
|
|
|
$link = $urlHelper( |
82
|
|
|
'hybridauth/logout', |
83
|
|
|
['redirect' => $route] |
84
|
|
|
); |
85
|
|
|
$link = sprintf($this->config['link'], $user, $link); |
86
|
|
|
return sprintf($this->config['logoffcontainer'], $link); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (null !== $provider && in_array($provider, $providers)) { |
90
|
|
|
return $urlHelper( |
91
|
|
|
'hybridauth/login', |
92
|
|
|
array('redirect' => $route, 'provider' => $provider) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (1 == count($providers)) { |
97
|
|
|
return sprintf( |
98
|
|
|
$this->config['item'], |
99
|
|
|
sprintf( |
100
|
|
|
$this->config['link'], |
101
|
|
|
sprintf( |
102
|
|
|
$this->config['loginstring'], |
103
|
|
|
' using ' . current($providers) |
104
|
|
|
), |
105
|
|
|
$urlHelper( |
106
|
|
|
'hybridauth/login', |
107
|
|
|
[ |
108
|
|
|
'redirect' => $route, |
109
|
|
|
'provider' => strtolower(current($providers)), |
110
|
|
|
] |
111
|
|
|
) |
112
|
|
|
), |
113
|
|
|
null |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$xhtml = array(); |
118
|
|
|
foreach ($providers as $name => $backend) { |
119
|
|
|
$link = $urlHelper( |
120
|
|
|
'hybridauth/login', |
121
|
|
|
[ |
122
|
|
|
'redirect' => $route, |
123
|
|
|
'provider' => $backend |
124
|
|
|
] |
125
|
|
|
); |
126
|
|
|
$xhtml[] = sprintf( |
127
|
|
|
$this->config['item'], |
128
|
|
|
sprintf( |
129
|
|
|
$this->config['link'], |
130
|
|
|
(is_string($name)?$name:$backend), |
131
|
|
|
$link |
132
|
|
|
), |
133
|
|
|
$this->config['itemAttribs'] |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return sprintf( |
138
|
|
|
$this->config['logincontainer'], |
139
|
|
|
sprintf( |
140
|
|
|
$this->config['loginstring'], |
141
|
|
|
' using' |
142
|
|
|
), |
143
|
|
|
sprintf( |
144
|
|
|
$this->config['itemlist'], |
145
|
|
|
implode("\n",$xhtml), |
146
|
|
|
$this->config['listAttribs'] |
147
|
|
|
) |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get the backends |
154
|
|
|
* |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
|
public function getBackends($backends = null) |
158
|
|
|
{ |
159
|
|
|
if (null === $backends) { |
160
|
|
|
$backends = $this->config['backend']; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$backends = (array) $backends; |
164
|
|
|
|
165
|
|
|
return $backends; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|