-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass.user.php
More file actions
304 lines (269 loc) · 7.04 KB
/
class.user.php
File metadata and controls
304 lines (269 loc) · 7.04 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
namespace Module\Authentication;
require_once(DIRBASE . '/modules/db/class.record.php');
/**
* Example of using the User module
*
* <code>
* if (class_exists('User')) {
* // Try to login
* try {
* $u = new \Module\Authentication\User();
*
* // check if the admin user exists
* $uid = $u->authenticate('developer@envivent.com', 'test');
*
* echo "Authentication successful. User ID is ", $uid;
* } catch (\Exception $e) {
* // Admin uses doesn't exist. Let's create it.
* $u->columns['role_id'] = 1;
* $u->columns['email'] = 'developer@envivent.com';
* $u->columns['password'] = $u->saltPassword('test');
* $u->save();
* }
* }
* </code>
*/
class User extends \Module\DB\Record {
/**
* The name of the users table
* @var string
*/
public $table = 'users';
/**
* The meta data for the table, e.g. fields and field data
* @var mixed
*/
public $metadata;
/**
* The users Role
* @var Role
*/
private $role;
/**
* Authenticates a user
* @param string $email The username
* @param string $pass The password
* @return int The user_id
*/
public function authenticate($email, $password) {
$query = $this->db->prepare("SELECT * FROM users WHERE email=:email");
$query->execute(array(
':email' => $email
));
$query->setFetchMode(\PDO::FETCH_ASSOC);
if ($row = $query->fetch()) {
$this->load($row['id']);
$hash = crypt($password, $row['password']);
if ($hash === $row['password']) {
return $row['id'];
} else {
throw new \Exception("Invalid user or password.");
}
} else {
throw new \Exception("Invalid user or password.");
}
}
/**
* Gets the permission for their role
* @param string $key What role do you want to query?
* @return string the value associated with the permission e.g. True, False, etc.
*/
public function hasPermission($key) {
if (!isset($this->role)) {
throw \Exception('Role has not been set.');
}
return $this->role->getPermission($key);
}
/**
* Gets the data associated with the user
* @param string $key The data you want to lookup
* @return mixed The data associated with the key
*/
public function getUserData($key) {
if (isset($this->columns[$key])) {
return $this->columns[$key];
} else {
if (isset($this->metadata[$key])) {
return $this->metadata[$key];
}
}
}
/**
* Loads the user
* @param integer $id The user ID
* @return void
*/
public function load($id=0) {
parent::load($id);
// Load usermeta data
$query = $this->db->query("SELECT * FROM usermeta where user_id={$this->columns['id']}");
$query->setFetchMode(\PDO::FETCH_ASSOC);
$query->execute();
$metadata = $query->fetchAll();
foreach ($metadata as $data) {
$temp = new UserMeta($data['id']);
$this->metadata[$temp->columns['key']] = $temp->columns['value'];
}
// Load the Role
$this->role = new Role($this->columns['role_id']);
}
/**
* Sets user data by a key
* @param string $key The key to store the data into
* @param mixed $value The data to store at the key
*/
public function setUserData($key, $value) {
if (isset($this->columns[$key])) {
$this->columns[$key] = $value;
} else {
if (isset($this->metadata[$key])) {
$this->metadata[$key] = $value;
} else {
$temp = new UserMeta();
$temp->columns['key'] = $key;
$temp->columns['value'] = $value;
$temp->save();
}
}
}
/**
* Salts the password
* @param string $pass The password
* @return string The encrypted password
*/
public function saltPassword($password, $cost=11) {
/* To generate the salt, first generate enough random bytes. Because
* base64 returns one character for each 6 bits, the we should generate
* at least 22*6/8=16.5 bytes, so we generate 17. Then we get the first
* 22 base64 characters
*/
$salt=substr(base64_encode(openssl_random_pseudo_bytes(17)),0,22);
/* As blowfish takes a salt with the alphabet ./A-Za-z0-9 we have to
* replace any '+' in the base64 string with '.'. We don't have to do
* anything about the '=', as this only occurs when the b64 string is
* padded, which is always after the first 22 characters.
*/
$salt=str_replace("+",".",$salt);
/* Next, create a string that will be passed to crypt, containing all
* of the settings, separated by dollar signs
*/
$param='$'.implode('$', array(
"2y", //select the most secure version of blowfish (>=PHP 5.3.7)
str_pad($cost,2,"0",STR_PAD_LEFT), //add the cost in two digits
$salt //add the salt
));
//now do the actual hashing
return crypt($password,$param);
}
/**
* Checks if a user is loaded
* @return boolean True, if loaded
*/
public function isLoaded() {
if (isset($this->columns['id'])) {
return true;
}
}
/**
* Checks if a user is logged in
* @return boolean True, if logged in
*/
public function isLoggedIn() {
if (isset($_SESSION['uid'])) {
return $_SESSION['uid'];
} else {
return false;
}
}
/**
* Checks if a user is an admin (role_id == 1)
* @return boolean True, if role_id == 1
*/
public function isAdmin() {
if (!$this->isLoggedIn()) {
return false;
}
if (!$this->isLoaded()) {
$this->load($uid);
}
if ($this->columns['role_id'] == 1) {
return true;
} else {
return false;
}
}
/**
* Gets the name of the role of the user
* @return string The role
*/
public function getRole() {
return $this->role->columns['name'];
}
}
/**
* The users meta data
*/
class UserMeta extends \Module\DB\Record {
public $table = "usermeta";
}
/**
* The users role
*/
class Role extends \Module\DB\Record {
public $table = 'roles';
private $_permissions;
/**
* The the permission for the role
* @param string $key The permission key
* @return mixed The role data, e.g. True/False, etc.
*/
public function getPermission($key) {
if (isset($this->_permissions[$key])) {
return $this->_permissions[$key];
} else {
throw new \Exception("{$key} permission does not exist.");
}
}
/**
* Loads the role
* @param integer $id role_id
* @return void
*/
public function load($id=0) {
parent::load($id);
// Load permissions
$query = $this->db->query("SELECT * FROM permissions where role_id={$this->columns['id']}");
$query->setFetchMode(\PDO::FETCH_ASSOC);
$query->execute();
$permissions = $query->fetchAll();
foreach ($permissions as $p) {
$temp = new Permission($p['id']);
$this->_permissions[$temp->columns['key']] = $temp->columns['value'];
}
}
/**
* Sets a permission for this role
* @param string $key The permission key
* @param void
*/
public function setPermission($key, $value) {
if (isset($this->_permissions[$key])) {
$this->_permissions[$key] = $value;
} else {
// Create a new permission
$temp = new Permission();
$temp->columns['key'] = $key;
$temp->columns['value'] = $value;
$temp->save();
// Add it to the permissions array
array_push($this->_permissions, $temp);
}
}
}
/**
* A row in the permission table
*/
class Permission extends \Module\DB\Record {
public $table = 'permissions';
}