/
Identity.php
107 lines (89 loc) · 2.45 KB
/
Identity.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
<?php
/*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
*/
namespace Ridg\Auth\Adapter;
class Identity implements \Zend_Auth_Adapter_Interface
{
const AUTH_FAIL = 'Authentication Failed';
const AUTH_SUCCESS = ' Authentication Successful';
/**
* @var \Doctrine\ORM\EntityManager
*/
protected $_em;
/**
* @var string
*/
protected $_identity = null;
/**
* @var string
*/
protected $_passHash = null;
/**
* @param \Doctrine\ORM\EntityManager $em
*/
public function __construct(\Doctrine\ORM\EntityManager $em)
{
$this->_em = $em;
}
/**
* @todo fire event on auth failure
* {@interitdoc}
*
* return \Zend_Auth_Result
*/
public function authenticate()
{
$result = $this->_em->getRepository('User\Model\Identity')->findOneByIdentity($this->_identity);
// Identity not found
if (null == $result) {
return new \Zend_Auth_Result(\Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, null, array(self::AUTH_FAIL));
}
// Identity doesn't require a password, return successful
if (null === $result->getPassHash()) {
return new \Zend_Auth_Result(\Zend_Auth_Result::SUCCESS, $result, array(self::AUTH_SUCCESS));
}
// Identity requires password, check it
if ($this->_passHash != $result->getPassHash()) {
return new \Zend_Auth_Result(\Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, null, array(self::AUTH_FAIL));
}
return new \Zend_Auth_Result(\Zend_Auth_Result::SUCCESS, $result, array(self::AUTH_FAIL));
}
/**
* @param string $identity
* @return Identity
*/
public function setIdentity($identity)
{
$this->_identity = $identity;
return $this;
}
/**
* @param string $password
* @return Identity
*/
public function setPassword($password)
{
$this->setPassHash($this->hash($password));
return $this;
}
/**
* @param string $passHash
* @return Identity
*/
public function setPassHash($passHash)
{
$this->_passHash = $passHash;
return $this;
}
/**
* @param string $value
* @return string
*/
protected function hash($value)
{
$filter = new \User\Filter\PassHash();
return $filter->filter($value);
}
}