/
SimpleFM.php
206 lines (173 loc) · 6.03 KB
/
SimpleFM.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
<?php
/**
* This source file is subject to the MIT license that is bundled with this package in the file LICENSE.txt.
*
* @package Soliant\SimpleFM\ZF2
* @copyright Copyright (c) 2007-2013 Soliant Consulting, Inc. (http://www.soliantconsulting.com)
* @author jsmall@soliantconsulting.com
*/
namespace Soliant\SimpleFM\ZF2\Authentication\Adapter;
use Soliant\SimpleFM\ZF2\Authentication\Mapper\Identity;
use Soliant\SimpleFM\Adapter;
use Zend\Authentication\Result;
class SimpleFM implements \Zend\Authentication\Adapter\AdapterInterface
{
/**
* @var array
*/
protected $credentials;
/**
* @var string
*/
protected $username;
/**
* @var string
*/
protected $password;
/**
* URI of user login form
*
* @var string
*/
protected $loginUrl;
/**
* Adapter to be used for login validation
*
* @var string
*/
protected $simpleFmValidateAdapter;
/**
* App encryption key used to encrypt the password if persisted in the session identity
*
* @var string
*/
protected $encryptionKey;
/**
* App username from the app config
*
* @var string
*/
protected $appUsername;
/**
* Admin password from the app config
*
* @var string
*/
protected $appPassword;
/**
* The FileMaker identity layout name for the authenticate request
*
* @var string
*/
protected $identityLayout;
/**
* The FileMaker identity username field name for the authenticate request
*
* @var string
*/
protected $accountNameField;
/**
* Constructor
*
* @param array $config Configuration settings:
* 'loginUrl' => string Example: '/login'
* 'validateSimpleFmAdapter' => Soliant\SimpleFM\Adapter
* 'encryptionKey' => string Example: '56cb36c21eb9a29c1317092b973a5f9cba393a367de783af45a2799f7302c',
* 'appUsername' => string Example: 'webSystem'
* 'appPassword' => string Example: '317akx1gr43m4pd'
* @throws Soliant\SimpleFM\ZF2\Authentication\Adapter\InvalidArgumentException
* @return void
*/
public function __construct(array $config, Adapter $simpleFmValidateAdapter)
{
$this->simpleFmValidateAdapter = $simpleFmValidateAdapter;
if (empty($config['loginUrl'])) {
throw new Exception\InvalidArgumentException('Config key \'loginUrl\' is required');
}
$this->loginUrl = $config['loginUrl'];
if (empty($config['encryptionKey'])) {
throw new Exception\InvalidArgumentException('Config key \'encryptionKey\' is required');
}
$this->encryptionKey = $config['encryptionKey'];
if (!empty($config['appUsername'])) {
$this->setUsername($config['appUsername']);
}
if (!empty($config['appPassword'])) {
$this->setPassword($config['appPassword']);
}
if (empty($config['identityLayout'])) {
throw new Exception\InvalidArgumentException('Config key \'identityLayout\' is required');
}
$this->identityLayout = $config['identityLayout'];
if (empty($config['accountNameField'])) {
throw new Exception\InvalidArgumentException('Config key \'accountNameField\' is required');
}
$this->accountNameField = $config['accountNameField'];
}
/**
* @return Soliant\SimpleFM\ZF2\Authentication\Adapter\Auth
*/
public function setUsername($username){
$this->username = $username;
$this->credentials['username'] = $username;
return $this;
}
/**
* @return Soliant\SimpleFM\ZF2\Authentication\Adapter\Auth
*/
public function setPassword($password){
$this->password = $password;
$this->credentials['password'] = $password;
return $this;
}
/**
* @return Zend\Authentication\Result
*/
public function authenticate()
{
$this->simpleFmValidateAdapter->setLayoutname($this->identityLayout);
$this->simpleFmValidateAdapter->setCredentials($this->credentials);
$command = array(
$this->accountNameField => "==" . self::escapeStringForFileMakerSearch($this->username),
'-find' => NULL,
);
$this->simpleFmValidateAdapter->setCommandarray($command);
$result = $this->simpleFmValidateAdapter->execute();
$error = $result['error'];
$errortext = $result['errortext'];
$errortype = $result['errortype'];
// Based on the status, return auth result
switch ($error) {
case '0':
// Return null as identity only for error 0
$identity = new Identity($this->username, $this->password, $this->encryptionKey, $result['rows'][0]);
$identity->setIsLoggedIn(TRUE);
return new Result(
Result::SUCCESS,
$identity
);
case '401':
// Return null identity plus reason as message array for HTTP 401
if ($errortype == 'HTTP') {
$identity = null;
return new Result(
Result::FAILURE,
$identity,
array('reason' => 'Username and/or password not valid' ,'sfm_auth_response' => $result)
);
}
default:
// Return empty identity plus reason as message array for every other result status
$identity = null;
return new Result(
Result::FAILURE,
$identity,
array('reason' => $errortype . ' error ' . $error . ': ' . $errortext ,'sfm_auth_response' => $result)
);
}
}
static public function escapeStringForFileMakerSearch($string)
{
return str_replace('@', '\@', $string);
}
}