-
Notifications
You must be signed in to change notification settings - Fork 0
/
User_class.php
624 lines (567 loc) · 18.2 KB
/
User_class.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
<?php
/*
User_class.php - Container for user related info
Copyright (C) 2002-2004 Stephen Lawrence Jr., Khoa Nguyen
Copyright (C) 2005-2013 Stephen Lawrence Jr.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
if (!defined('User_class')) {
define('User_class', 'true', false);
class User extends databaseData
{
public $root_id;
public $id;
public $username;
public $first_name;
public $last_name;
public $email;
public $phone;
public $department;
public $pw_reset_code;
public $can_add;
public $can_checkin;
/**
* @param int $id
* @param PDO $connection
*/
public function __construct($id, PDO $connection)
{
$this->root_id = $GLOBALS['CONFIG']['root_id'];
$this->field_name = 'username';
$this->field_id = 'id';
$this->tablename = $GLOBALS['CONFIG']['db_prefix'] . $this->TABLE_USER;
$this->result_limit = 1; //there is only 1 user with a certain user_name or user_id
parent::setTableName($this->TABLE_USER);
parent::__construct($id, $connection);
$query = "
SELECT
id,
username,
department,
phone,
email,
last_name,
first_name,
pw_reset_code,
can_add,
can_checkin
FROM
{$GLOBALS['CONFIG']['db_prefix']}user
WHERE
id = :id";
$stmt = $connection->prepare($query);
$stmt->execute(array(':id' => $this->id));
$result = $stmt->fetch();
list(
$this->id,
$this->username,
$this->department,
$this->phone,
$this->email,
$this->last_name,
$this->first_name,
$this->pw_reset_code,
$this->can_add,
$this->can_checkin
) = $result;
}
/**
* Return department name for current user
* @return string
*/
public function getDeptName()
{
$query = "
SELECT
d.name
FROM
{$GLOBALS['CONFIG']['db_prefix']}department d,
{$GLOBALS['CONFIG']['db_prefix']}user u
WHERE
u.id = :id
AND
u.department = d.id";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(
':id' => $this->id
));
$result = $stmt->fetchColumn();
return $result;
}
/**
* Return department ID for current user
* @return string
*/
public function getDeptId()
{
return $this->department;
}
/**
* Return an array of publishable documents
* @return array
* @param object $publishable
*/
public function getPublishedData($publishable)
{
$data_published = array();
$index = 0;
$query = "
SELECT
d.id
FROM
{$GLOBALS['CONFIG']['db_prefix']}data d,
{$GLOBALS['CONFIG']['db_prefix']}user u
WHERE
d.owner = :id
AND
u.id = d.owner
AND
d.publishable = :publishable ";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(
':publishable' => $publishable,
':id' => $this->id
));
$result = $stmt->fetchAll();
foreach ($result as $row) {
$data_published[$index] = $row;
$index++;
}
return $data_published;
}
/**
* Check whether user from object has Admin rights
* @return Boolean
*/
public function isAdmin()
{
if ($this->isRoot()) {
return true;
}
$query = "
SELECT
admin
FROM
{$GLOBALS['CONFIG']['db_prefix']}admin
WHERE
id = :id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(
':id' => $this->id
));
$result = $stmt->fetchColumn();
if ($stmt->rowCount() !=1) {
return false;
}
return $result;
}
/**
* Check whether user from object is root
* @return bool
*/
public function isRoot()
{
return ($this->root_id == $this->getId());
}
/**
* @return boolean
*/
public function canAdd()
{
if ($this->isAdmin()) {
return true;
}
if ($this->can_add) {
return true;
}
return false;
}
/**
* @return boolean
*/
public function canCheckIn()
{
if ($this->isAdmin()) {
return true;
}
if ($this->can_checkin) {
return true;
}
return false;
}
/**
* @return string
*/
public function getPassword()
{
$query = "
SELECT
password
FROM
$this->tablename
WHERE
id = :id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':id' => $this->id));
$result = $stmt->fetchColumn();
if ($stmt->rowCount() !=1) {
header('Location:' . $GLOBALS['CONFIG']['base_url'] . 'error.php?ec=14');
exit;
}
return $result;
}
/**
* @param string $non_encrypted_password
* @return bool
*/
public function changePassword($non_encrypted_password)
{
$query = "
UPDATE
$this->tablename
SET
password = md5(:non_encrypted_password)
WHERE
id = :id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(
':non_encrypted_password' => $non_encrypted_password,
':id' => $this->id
));
return true;
}
/**
* @param string $non_encrypted_password
* @return bool
*/
public function validatePassword($non_encrypted_password)
{
$query = "
SELECT
username
FROM
$this->tablename
WHERE
id = :id
AND
password = md5(:non_encrypted_password)
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(
':non_encrypted_password' => $non_encrypted_password,
':id' => $this->id
));
if ($stmt->rowCount() == 1) {
return true;
} else {
// Check the old password() style user password
$query = "
SELECT
username
FROM
$this->tablename
WHERE
id = :id
AND
password = password(:non_encrypted_password)
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(
':non_encrypted_password' => $non_encrypted_password,
':id' => $this->id
));
if ($stmt->rowCount() == 1) {
return true;
}
}
return false;
}
/**
* @param string $new_name
* @return bool
*/
public function changeName($new_name)
{
$query = "
UPDATE
$this->tablename
SET
username = :new_name
WHERE
id = :id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(
':new_name' => $new_name,
':id' => $this->id
));
return true;
}
/**
* Determine if the current user is a reviewer or not
* @return boolean
*/
public function isReviewer()
{
// If they are an admin, they can review
if ($this->isAdmin()) {
return true;
}
// Lets see if this non-admin user has a department they can review for, if so, they are a reviewer
$query = "
SELECT
dept_id
FROM
{$GLOBALS['CONFIG']['db_prefix']}dept_reviewer
WHERE
user_id = :id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(
':id' => $this->id
));
if ($stmt->rowCount() > 0) {
return true;
} else {
return false;
}
}
/**
* Determine if the current user is a reviewer for a specific ID
* @param int $file_id
* @return boolean
*/
public function isReviewerForFile($file_id)
{
$query = "SELECT
d.id
FROM
{$GLOBALS['CONFIG']['db_prefix']}data as d,
{$GLOBALS['CONFIG']['db_prefix']}dept_reviewer as dr
WHERE
dr.dept_id = d.department AND
dr.user_id = :user_id AND
d.department = dr.dept_id AND
d.id = :file_id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(
':user_id' => $this->id,
':file_id' => $file_id
));
$num_rows = $stmt->rowCount();
if ($num_rows < 1) {
return false;
}
return true;
}
/**
* this functions assume that you are an admin thus allowing you to review all departments
* @return array
*/
public function getAllRevieweeIds()
{
if ($this->isAdmin()) {
$query = "SELECT id FROM {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA WHERE publishable = 0";
$stmt = $this->connection->prepare($query);
$stmt->execute(array());
$result = $stmt->fetchAll();
$file_data = array();
$index = 0;
foreach ($result as $row) {
$file_data[$index] = $row[0];
$index++;
}
return $file_data;
}
}
/**
* getRevieweeIds - Return an array of files that need reviewing under this person
* @return array
*/
public function getRevieweeIds()
{
if ($this->isReviewer()) {
// Which departments can this user review?
$query = "SELECT dept_id FROM {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DEPT_REVIEWER WHERE user_id = :id";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(
':id' => $this->id
));
$result = $stmt->fetchAll();
$num_depts = $stmt->rowCount();
$index = 0;
// Build the query
$query = "SELECT id FROM {$GLOBALS['CONFIG']['db_prefix']}data WHERE (";
foreach ($result as $row) {
$dept = $row['dept_id'];
if ($index != $num_depts -1) {
$query = $query . " department = :dept OR ";
} else {
$query = $query . " department = :dept )";
}
$index++;
}
$query = $query . " AND publishable = 0";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':dept' => $dept));
$result = $stmt->fetchAll();
$file_data = array();
$num_files = $stmt->rowCount();
for ($index = 0; $index< $num_files; $index++) {
$fid = $result[$index]['id'];
$file_data[$index] = $fid;
}
return $file_data;
}
}
/**
* @return array
*/
public function getAllRejectedFileIds()
{
$query = "SELECT id FROM {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA WHERE publishable = '-1'";
$stmt = $this->connection->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll();
$file_data = array();
$num_files = $stmt->rowCount();
for ($index = 0; $index< $num_files; $index++) {
list($fid) = $result[$index];
$file_data[$index] = $fid;
}
return $file_data;
}
/**
* @return array
*/
public function getRejectedFileIds()
{
$query = "SELECT id FROM {$GLOBALS['CONFIG']['db_prefix']}data WHERE publishable = '-1' and owner = :id";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(
':id' => $this->id
));
$result = $stmt->fetchAll();
$file_data = array();
$num_files = $stmt->rowCount();
for ($index = 0; $index< $num_files; $index++) {
list($fid) = $result[$index];
$file_data[$index] = $fid;
}
return $file_data;
}
/**
* @return array
*/
public function getExpiredFileIds()
{
$query = "SELECT id FROM {$GLOBALS['CONFIG']['db_prefix']}data WHERE status = -1 AND owner = :id";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(
':id' => $this->id
));
$result = $stmt->fetchAll();
$len = $stmt->rowCount();
$file_data = array();
for ($index = 0; $index< $len; $index++) {
list($fid) = $result[$index];
$file_data[$index] = $fid;
}
return $file_data;
}
/**
* @return int
*/
public function getNumExpiredFiles()
{
$query = "SELECT id FROM {$GLOBALS['CONFIG']['db_prefix']}data WHERE status =- 1 AND owner = :id";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(
':id' => $this->id
));
return $stmt->rowCount();
}
/**
* @return mixed
*/
public function getEmailAddress()
{
return $this->email;
}
/**
* @return mixed
*/
public function getPhoneNumber()
{
return $this->phone;
}
/**
* /Return full name array where array[0]=firstname and array[1]=lastname
* @return mixed
*/
public function getFullName()
{
$full_name[0] = $this->first_name;
$full_name[1] = $this->last_name;
return $full_name;
}
/**
* Return username of current user
* @return mixed
*/
public function getUserName()
{
return $this->username;
}
/**
* Return list of checked out files to root
* @return array
*/
public function getCheckedOutFiles()
{
if ($this->isRoot()) {
$query = "SELECT id FROM {$GLOBALS['CONFIG']['db_prefix']}data WHERE status > 0";
$stmt = $this->connection->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll();
$len = $stmt->rowCount();
$file_data = array();
for ($index = 0; $index < $len; $index++) {
list($fid) = $result[$index];
$file_data[$index] = $fid;
}
return $file_data;
}
}
/**
* getAllUsers - Returns an array of all the active users
* @param $pdo
* @return array
*/
public static function getAllUsers($pdo)
{
$query = "SELECT id, last_name, first_name FROM {$GLOBALS['CONFIG']['db_prefix']}user ORDER BY last_name";
$stmt = $pdo->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
$userListArray[] = $row;
}
return $userListArray;
}
}
}