Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Authentication / Validator / Authentication.php
blob906841e9af24af59a02766d696b04cc5ad7a12ff
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Authentication\Validator;
12 use Traversable;
13 use Zend\Authentication\Adapter\ValidatableAdapterInterface;
14 use Zend\Authentication\AuthenticationService;
15 use Zend\Authentication\Result;
16 use Zend\Authentication\Exception;
17 use Zend\Stdlib\ArrayUtils;
18 use Zend\Validator\AbstractValidator;
20 /**
21 * Authentication Validator
23 class Authentication extends AbstractValidator
25 /**
26 * Error codes
27 * @const string
29 const IDENTITY_NOT_FOUND = 'identityNotFound';
30 const IDENTITY_AMBIGUOUS = 'identityAmbiguous';
31 const CREDENTIAL_INVALID = 'credentialInvalid';
32 const UNCATEGORIZED = 'uncategorized';
33 const GENERAL = 'general';
35 /**
36 * Error Messages
37 * @var array
39 protected $messageTemplates = array(
40 self::IDENTITY_NOT_FOUND => 'Invalid identity',
41 self::IDENTITY_AMBIGUOUS => 'Identity is ambiguous',
42 self::CREDENTIAL_INVALID => 'Invalid password',
43 self::UNCATEGORIZED => 'Authentication failed',
44 self::GENERAL => 'Authentication failed',
47 /**
48 * Authentication Adapter
49 * @var ValidatableAdapterInterface
51 protected $adapter;
53 /**
54 * Identity (or field)
55 * @var string
57 protected $identity;
59 /**
60 * Credential (or field)
61 * @var string
63 protected $credential;
65 /**
66 * Authentication Service
67 * @var AuthenticationService
69 protected $service;
71 /**
72 * Sets validator options
74 * @param mixed $options
76 public function __construct($options = null)
78 if ($options instanceof Traversable) {
79 $options = ArrayUtils::iteratorToArray($options);
82 if (is_array($options)) {
83 if (array_key_exists('adapter', $options)) {
84 $this->setAdapter($options['adapter']);
86 if (array_key_exists('identity', $options)) {
87 $this->setIdentity($options['identity']);
89 if (array_key_exists('credential', $options)) {
90 $this->setCredential($options['credential']);
92 if (array_key_exists('service', $options)) {
93 $this->setService($options['service']);
96 parent::__construct($options);
99 /**
100 * Get Adapter
102 * @return ValidatableAdapterInterface
104 public function getAdapter()
106 return $this->adapter;
110 * Set Adapter
112 * @param ValidatableAdapterInterface $adapter
113 * @return Authentication
115 public function setAdapter(ValidatableAdapterInterface $adapter)
117 $this->adapter = $adapter;
119 return $this;
123 * Get Identity
125 * @return mixed
127 public function getIdentity()
129 return $this->identity;
133 * Set Identity
135 * @param mixed $identity
136 * @return Authentication
138 public function setIdentity($identity)
140 $this->identity = $identity;
142 return $this;
146 * Get Credential
148 * @return mixed
150 public function getCredential()
152 return $this->credential;
156 * Set Credential
158 * @param mixed $credential
159 * @return Authentication
161 public function setCredential($credential)
163 $this->credential = $credential;
165 return $this;
169 * Get Service
171 * @return AuthenticationService
173 public function getService()
175 return $this->service;
179 * Set Service
181 * @param AuthenticationService $service
182 * @return Authentication
184 public function setService(AuthenticationService $service)
186 $this->service = $service;
188 return $this;
192 * Is Valid
194 * @param mixed $value
195 * @param array $context
196 * @return bool
198 public function isValid($value = null, $context = null)
200 if ($value !== null) {
201 $this->setCredential($value);
204 if (($context !== null) && array_key_exists($this->identity, $context)) {
205 $identity = $context[$this->identity];
206 } else {
207 $identity = $this->identity;
209 if (!$this->identity) {
210 throw new Exception\RuntimeException('Identity must be set prior to validation');
213 if (($context !== null) && array_key_exists($this->credential, $context)) {
214 $credential = $context[$this->credential];
215 } else {
216 $credential = $this->credential;
219 if (!$this->adapter) {
220 throw new Exception\RuntimeException('Adapter must be set prior to validation');
222 $this->adapter->setIdentity($identity);
223 $this->adapter->setCredential($credential);
225 if (!$this->service) {
226 throw new Exception\RuntimeException('AuthenticationService must be set prior to validation');
228 $result = $this->service->authenticate($this->adapter);
230 if ($result->getCode() != Result::SUCCESS) {
231 switch ($result->getCode()) {
232 case Result::FAILURE_IDENTITY_NOT_FOUND:
233 $this->error(self::IDENTITY_NOT_FOUND);
234 break;
235 case Result::FAILURE_CREDENTIAL_INVALID:
236 $this->error(self::CREDENTIAL_INVALID);
237 break;
238 case Result::FAILURE_IDENTITY_AMBIGUOUS:
239 $this->error(self::IDENTITY_AMBIGUOUS);
240 break;
241 case Result::FAILURE_UNCATEGORIZED:
242 $this->error(self::UNCATEGORIZED);
243 break;
244 default:
245 $this->error(self::GENERAL);
248 return false;
251 return true;