blob: 882c5d5c2657fce8c0fe6a5afdd3f0a1e404532b (
plain)
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
|
<?php
/**
* IUser interface.
*
* This interface must be implemented by user objects.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Revision: $ $Date: $
* @package System.Security
* @since 3.0
*/
interface IUser
{
public function getManager();
public function getName();
public function setName($value);
public function getIsGuest();
public function setIsGuest($value);
public function getRoles();
public function setRoles($value);
/**
* @param string role to be tested
* @return boolean whether the user is of this role
*/
public function isInRole($role);
public function saveToString();
public function loadFromString($string);
}
class TUser extends TComponent implements IUser
{
private $_manager;
private $_isGuest=false;
private $_name='';
private $_roles=array();
public function __construct($manager=null)
{
parent::__construct();
$this->_manager=$manager;
}
public function getManager()
{
return $this->_manager;
}
public function getName()
{
return $this->_name;
}
public function setName($value)
{
$this->_name=$value;
}
public function getIsGuest()
{
return $this->_isGuest;
}
public function setIsGuest($value)
{
$this->_isGuest=TPropertyValue::ensureBoolean($value);
if($this->_isGuest)
{
$this->_name='';
$this->_roles=array();
}
}
public function getRoles()
{
return $this->_roles;
}
public function setRoles($value)
{
if(is_array($value))
$this->_roles=$value;
else
{
$this->_roles=array();
foreach(explode(',',$value) as $role)
$this->_roles[]=trim($value);
}
}
public function isInRole($role)
{
return in_array($role,$this->_roles);
}
public function saveToString()
{
return serialize(array($this->_name,$this->_roles,$this->_isGuest));
}
public function loadFromString($data)
{
if(!empty($data))
{
$array=unserialize($data);
$this->_name=$array[0];
$this->_roles=$array[1];
$this->_isGuest=$array[2];
}
return $this;
}
}
class TUserManager extends TComponent implements IModule
{
private $_id;
private $_users=array();
private $_guestName='Guest';
private $_passwordMode='MD5';
public function init($application,$config)
{
foreach($config->getElementsByTagName('user') as $node)
$this->_users[$node->getAttribute('name')]=$node->getAttribute('password');
}
public function getID()
{
return $this->_id;
}
public function setID($value)
{
$this->_id=$value;
}
public function getGuestName()
{
return $this->_guestName;
}
public function setGuestName($value)
{
$this->_guestName=$value;
}
public function getPasswordMode()
{
return $this->_passwordMode;
}
public function setPasswordMode($value)
{
$this->_passwordMode=TPropertyValue::ensureEnum($value,array('Clear','MD5','SHA1'));
}
public function validateUser($username,$password)
{
if($this->_passwordMode==='MD5')
$password=md5($password);
else if($this->_passwordMode==='SHA1')
$password=sha1($password);
return (isset($this->_users[$username]) && $this->_users[$username]===$password);
}
public function logout($user)
{
$user->setIsGuest(true);
$user->setName($this->getGuestName());
}
public function getUser($username=null)
{
if($username===null)
{
$user=new TUser($this);
$user->setIsGuest($username===null);
return $user;
}
else if(isset($this->_users[$username]))
{
$user=new TUser($this);
$user->setName($username);
return $user;
}
else
return null;
}
}
?>
|