forked from roelio/TBS-Zend-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuth.php
113 lines (92 loc) · 2.63 KB
/
Auth.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
<?php
namespace TBS;
/**
*
* This class is almost an identical copy of the Zend_Auth class.
* Their are a few things different which are commented on.
*
* @author Roel Obdam
*
*/
class Auth
{
protected static $_instance = null;
protected $_storage = null;
public static function getInstance()
{
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
protected function __construct()
{}
protected function __clone()
{}
public function setStorage(\Zend_Auth_Storage_Interface $storage)
{
$this->_storage = $storage;
return $this;
}
// The default storage is the MultipleIdenties class
public function getStorage()
{
if (NULL === $this->_storage) {
$this->setStorage(new Auth\Storage\MultipleIdentities());
}
return $this->_storage;
}
/**
*
* This function doesn't delete the identity information but adds the new
* identity to the storage. This function only works with adapters that
* create a Generic identity.
*
* @param \Zend_Auth_Adapter_Interface $adapter
* @throws Exception
*/
public function authenticate(\Zend_Auth_Adapter_Interface $adapter)
{
$result = $adapter->authenticate();
$identity = $result->getIdentity();
if(NULL === $identity) {
return $result;
}
if(get_class($identity) !== 'TBS\Auth\Identity\Generic' &&
!is_subclass_of($identity, 'TBS\Auth\Identity\Generic')) {
throw new \Exception('Not a valid identity');
}
$currentIdentity = $this->getIdentity();
if(false === $currentIdentity
|| get_class($currentIdentity) !== 'TBS\Auth\Identity\Container')
{
$currentIdentity = new Auth\Identity\Container();
}
$currentIdentity->add($result->getIdentity());
if ($this->hasIdentity()) {
$this->clearIdentity();
}
if ($result->isValid()) {
$this->getStorage()->write($currentIdentity);
}
return $result;
}
// The three functions below accept the provider parameter so that a
// specific identity can be retreived or removed.
public function hasIdentity($provider = null)
{
return !$this->getStorage()->isEmpty($provider);
}
public function getIdentity($provider = null)
{
$storage = $this->getStorage();
if ($storage->isEmpty($provider)) {
return false;
}
return $storage->read($provider);
}
public function clearIdentity($provider = null)
{
$this->getStorage()->clear($provider);
}
}