1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Auth\Model; |
4
|
|
|
|
5
|
|
|
use Drone\Db\TableGateway\TableGateway; |
6
|
|
|
|
7
|
|
|
class UserTbl extends TableGateway |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Returns the next primary key in the table |
11
|
|
|
* |
12
|
|
|
* @return string |
13
|
|
|
*/ |
14
|
|
|
public function getNextId() |
15
|
|
|
{ |
16
|
|
|
$table = $this->getEntity()->getTableName(); |
17
|
|
|
|
18
|
|
|
$config = include 'module/Auth/config/user.config.php'; |
19
|
|
|
$id_field = $config["authentication"]["gateway"]["table_info"]["columns"]["id_field"]; |
20
|
|
|
|
21
|
|
|
$sql = "SELECT CASE WHEN MAX($id_field) IS NULL THEN 1 ELSE MAX($id_field) + 1 END AS USER_ID FROM $table"; |
22
|
|
|
|
23
|
|
|
$this->getDriver()->getDb()->execute($sql); |
24
|
|
|
$rowset = $this->getDriver()->getDb()->getArrayResult(); |
25
|
|
|
$row = array_shift($rowset); |
26
|
|
|
|
27
|
|
|
return $row["USER_ID"]; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Returns the user by the id |
32
|
|
|
* |
33
|
|
|
* @param string $id |
34
|
|
|
* |
35
|
|
|
* @return User |
36
|
|
|
*/ |
37
|
|
|
public function getUserById($id) |
38
|
|
|
{ |
39
|
|
|
$config = include 'module/Auth/config/user.config.php'; |
40
|
|
|
$id_field = $config["authentication"]["gateway"]["table_info"]["columns"]["id_field"]; |
41
|
|
|
|
42
|
|
|
$rowset = $this->select([ |
43
|
|
|
$id_field => $id |
44
|
|
|
]); |
45
|
|
|
|
46
|
|
|
$row = array_shift($rowset); |
47
|
|
|
|
48
|
|
|
$filtered_array = array(); |
49
|
|
|
|
50
|
|
|
foreach ($row as $key => $value) |
51
|
|
|
{ |
52
|
|
|
if (is_string($key)) |
53
|
|
|
$filtered_array[$key] = $value; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$user = new User(); |
57
|
|
|
$user->exchangeArray($filtered_array); |
58
|
|
|
|
59
|
|
|
return $user; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns the user by the username credential |
64
|
|
|
* |
65
|
|
|
* @param string $username |
66
|
|
|
* |
67
|
|
|
* @return User |
68
|
|
|
*/ |
69
|
|
|
public function getUserByUsernameCredential($username) |
70
|
|
|
{ |
71
|
|
|
$config = include 'module/Auth/config/user.config.php'; |
72
|
|
|
$username_credential = $config["authentication"]["gateway"]["credentials"]["username"]; |
73
|
|
|
|
74
|
|
|
$rowset = $this->select([ |
75
|
|
|
$username_credential => $username |
76
|
|
|
]); |
77
|
|
|
|
78
|
|
|
if (!count($rowset)) |
79
|
|
|
throw new \Exception("The user does not exists!"); |
80
|
|
|
|
81
|
|
|
$row = array_shift($rowset); |
82
|
|
|
|
83
|
|
|
$filtered_array = array(); |
84
|
|
|
|
85
|
|
|
foreach ($row as $key => $value) |
86
|
|
|
{ |
87
|
|
|
if (is_string($key)) |
88
|
|
|
$filtered_array[$key] = $value; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$user = new User(); |
92
|
|
|
$user->exchangeArray($filtered_array); |
93
|
|
|
|
94
|
|
|
return $user; |
95
|
|
|
} |
96
|
|
|
} |