1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Keeps track of logged in users |
4
|
|
|
* |
5
|
|
|
* @package Intraface |
6
|
|
|
* @author Lars Olesen <[email protected]> |
7
|
|
|
* @since 0.1.0 |
8
|
|
|
* @version @package-version@ |
9
|
|
|
*/ |
10
|
|
|
class Intraface_Auth |
11
|
|
|
{ |
12
|
|
|
private $identity; |
|
|
|
|
13
|
|
|
private $observers = array(); |
14
|
|
|
private $session_id; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Constructor |
18
|
|
|
* |
19
|
|
|
* @return void |
|
|
|
|
20
|
|
|
*/ |
21
|
3 |
|
public function __construct($session_id) |
22
|
|
|
{ |
23
|
3 |
|
$this->session_id = $session_id; |
24
|
3 |
|
} |
25
|
|
|
|
26
|
2 |
|
public function authenticate($adapter) |
27
|
|
|
{ |
28
|
2 |
|
if (is_callable(array($adapter, 'getIdentification'))) { |
29
|
2 |
|
$identification = $adapter->getIdentification(); |
30
|
2 |
|
} else { |
31
|
|
|
$identification = '[unidentifiable]'; |
32
|
|
|
} |
33
|
|
|
|
34
|
2 |
|
if ($object = $adapter->auth()) { |
35
|
2 |
|
$this->notifyObservers('login', $identification . ' logged in'); |
36
|
2 |
|
} else { |
37
|
|
|
$this->notifyObservers('login', $identification . ' could not login'); |
38
|
|
|
} |
39
|
|
|
|
40
|
2 |
|
return $object; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* hasIdentity() |
45
|
|
|
* |
46
|
|
|
* @return mixed user id or false |
47
|
|
|
*/ |
48
|
1 |
|
public function hasIdentity() |
|
|
|
|
49
|
|
|
{ |
50
|
1 |
|
if (!empty($_SESSION['intraface_logged_in_user_id'])) { |
51
|
1 |
|
return true; |
52
|
|
|
} else { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* logout() |
59
|
|
|
* |
60
|
|
|
* @return boolean |
61
|
|
|
*/ |
62
|
|
|
public function clearIdentity() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$_SESSION['user'] = ''; |
65
|
|
|
unset($_SESSION['user']); |
66
|
|
|
//session_destroy(); |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
public function getIdentity($db) |
71
|
|
|
{ |
72
|
1 |
|
if ($this->hasIdentity()) { |
73
|
1 |
|
$adapter = new Intraface_Auth_User($db, $this->session_id); |
74
|
1 |
|
if (!$user = $adapter->isLoggedIn()) { |
75
|
|
|
throw new Exception('No valid user was found'); |
76
|
|
|
} |
77
|
|
|
// $user->clearCachedPermission(); |
|
|
|
|
78
|
1 |
|
return $user; |
79
|
|
|
} |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Implements the observer pattern |
85
|
|
|
* |
86
|
|
|
* @param object $observer |
87
|
|
|
* |
88
|
|
|
* @return boolean |
89
|
|
|
*/ |
90
|
|
|
public function attachObserver($observer) |
91
|
|
|
{ |
92
|
|
|
$this->observers[] = $observer; |
93
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Notifies observers |
98
|
|
|
* |
99
|
|
|
* @param string $code Which code |
100
|
|
|
* @param string $msg Which message to pass to observers |
101
|
|
|
*/ |
102
|
2 |
|
private function notifyObservers($code, $msg) |
103
|
|
|
{ |
104
|
2 |
|
foreach ($this->getObservers() as $observer) { |
105
|
|
|
$observer->update($code, $msg); |
106
|
2 |
|
} |
107
|
2 |
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Implements the observer pattern |
112
|
|
|
* |
113
|
|
|
* @return array with observers |
114
|
|
|
*/ |
115
|
2 |
|
public function getObservers() |
116
|
|
|
{ |
117
|
2 |
|
return $this->observers; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.