-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZendAuthAdapterAlfresco.php
231 lines (195 loc) · 5.65 KB
/
ZendAuthAdapterAlfresco.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* Zend Auth Adapter Alfresco
*
* Adapter to make the authentication through Alfresco <http://www.alfresco.com/> webservice.
*
* @author Bruno Cavalcante <[email protected]>
* @version 0.1
*/
/**
* @see Zend_Auth_Adapter_Interface
*/
class ZendAuthAdapterAlfresco implements Zend_Auth_Adapter_Interface
{
protected $_username;
protected $_password;
protected $_wsdlAddress;
protected $_response;
protected $_result;
/**
* Sets username and password for authentication
*
* @return void
*/
public function __construct($username, $password, $wsdlAddress)
{
$this->setUsername($username);
$this->setPassword($password);
$this->setWsdlAddress($wsdlAddress);
$this->setDefaultResult();
}
private function setDefaultResult()
{
$this->_result = array('code' => null, 'messages' => array(), 'identity' => null);
}
private function getUsername()
{
return $this->_username;
}
private function setUsername($username)
{
$this->_username = $username;
}
private function getPassword()
{
return $this->_password;
}
private function setPassword($password)
{
$this->_password = $password;
}
private function getWsdlAddress()
{
return $this->_wsdlAddress;
}
private function setWsdlAddress($wsdlAddress)
{
$this->_wsdlAddress = $wsdlAddress;
}
private function getResponse()
{
return $this->_response;
}
private function setResponse($response)
{
$this->_response = $response;
}
private function getResultCode()
{
return $this->_result['code'];
}
private function setResultCode($code)
{
$this->_result['code'] = $code;
}
private function getResultMessages()
{
return $this->_result['messages'];
}
private function addResultMessage($message)
{
$this->_result['messages'][] = $message;
}
private function getResultIdentity()
{
return $this->_result['identity'];
}
private function setResultIdentity($identity)
{
$this->_result['identity'] = $identity;
}
/**
* Performs an authentication attempt
*
* @throws Zend_Auth_Adapter_Exception If authentication cannot be performed
* @return Zend_Auth_Result
*/
public function authenticate()
{
try {
$this->authenticateOnAlfresco();
$this->setApplicationUser();
} catch (Exception $e) {
$this->throwAuthorizationError($e);
}
$code = $this->getResultCode();
$identity = $this->getResultIdentity();
$messages = $this->getResultMessages();
return new Zend_Auth_Result($code, $identity, $messages);
}
private function authenticateOnAlfresco()
{
$username = $this->getUsername();
$password = $this->getPassword();
$soapClient = $this->getSoapClient();
$response = $soapClient->startSession(array('username' => $username, 'password' => $password));
$this->setResponse($response);
}
private function getSoapClient()
{
$soap_client = new Zend_Soap_Client($this->getWsdlAddress());
return $soap_client;
}
private function setApplicationUser()
{
$messages = array();
$code = $this->getSuccessCode();
$identity = $this->getIdentityFromResponse();
$this->setResultCode($code);
$this->setResultIdentity($identity);
}
private function getSuccessCode()
{
return Zend_Auth_Result::SUCCESS;
}
private function getIdentityFromResponse()
{
$identity = array();
$identity['username'] = $this->getUsernameFromResponse();
$identity['ticket'] = $this->getTicketFromResponse();
$identity['sessionid'] = $this->getSessionIdFromResponse();
return $identity;
}
private function getUsernameFromResponse()
{
return $this->getResponse()->startSessionReturn->username;
}
private function getTicketFromResponse()
{
return $this->getResponse()->startSessionReturn->ticket;
}
private function getSessionIdFromResponse()
{
return $this->getResponse()->startSessionReturn->sessionid;
}
private function throwAuthorizationError($exception)
{
$code = $this->getDefaultErrorCode();
if ($this->isConnectionFailure()) {
$code = $this->getConnectionFailureErrorCode();
} elseif ($this->isAlfrescoError()) {
$code = $this->getAlfrescoErrorCode($exception);
}
$this->setResultCode($code);
}
private function getDefaultErrorCode()
{
Zend_Auth_Result::FAILURE_UNCATEGORIZED;
}
private function isConnectionFailure($exception)
{
return isset($exception->faultcode);
}
private function getConnectionFailureErrorCode()
{
return Zend_Auth_Result::FAILURE;
}
private function isAlfrescoError($exception)
{
return isset($exception->detail);
}
private function getAlfrescoErrorCode($exception)
{
$errorCode = $exception->detail->AuthenticationFault->errorCode;
switch ($errorCode) {
case 100:
$code = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
break;
default:
$code = Zend_Auth_Result::FAILURE_UNCATEGORIZED;
break;
}
return $code;
}
}