|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Db\Service\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Db\Config\AuthInterface; |
|
6
|
|
|
use Lagdo\DbAdmin\Db\Service\Audit; |
|
7
|
|
|
use Lagdo\DbAdmin\Driver\DriverInterface; |
|
8
|
|
|
|
|
9
|
|
|
use function gmdate; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Connection to the audit database |
|
13
|
|
|
*/ |
|
14
|
|
|
class ConnectionProxy extends Audit\ConnectionProxy |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var int|null |
|
18
|
|
|
*/ |
|
19
|
|
|
private int|null $ownerId = null; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The constructor |
|
23
|
|
|
* |
|
24
|
|
|
* @param AuthInterface $auth |
|
25
|
|
|
* @param DriverInterface $driver |
|
26
|
|
|
* @param array $database |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(private AuthInterface $auth, |
|
29
|
|
|
DriverInterface $driver, array $database) |
|
30
|
|
|
{ |
|
31
|
|
|
parent::__construct($driver, $database); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
|
|
public function currentTime(): string |
|
38
|
|
|
{ |
|
39
|
|
|
return gmdate('Y-m-d H:i:s'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $username |
|
44
|
|
|
* |
|
45
|
|
|
* @return int |
|
46
|
|
|
*/ |
|
47
|
|
|
private function readOwnerId(string $username): int |
|
48
|
|
|
{ |
|
49
|
|
|
$query = "SELECT id FROM dbadmin_owners WHERE username=:username LIMIT 1"; |
|
50
|
|
|
$statement = $this->executeQuery($query, ['username' => $username]); |
|
51
|
|
|
return !$statement || !($row = $statement->fetchAssoc()) ? 0 : (int)$row['id']; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $username |
|
56
|
|
|
* |
|
57
|
|
|
* @return int |
|
58
|
|
|
*/ |
|
59
|
|
|
private function newOwnerId(string $username): int |
|
60
|
|
|
{ |
|
61
|
|
|
// Try to save the user and return his id. |
|
62
|
|
|
$query = "INSERT INTO dbadmin_owners(username) VALUES (:username)"; |
|
63
|
|
|
$statement = $this->executeQuery($query, ['username' => $username]); |
|
64
|
|
|
if ($statement !== false) { |
|
65
|
|
|
return $this->readOwnerId($username); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->logWarning('Unable to save new owner in the query audit database.'); |
|
69
|
|
|
return 0; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param bool $canCreate |
|
74
|
|
|
* |
|
75
|
|
|
* @return int |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getOwnerId(bool $canCreate = true): int |
|
78
|
|
|
{ |
|
79
|
|
|
$user = $this->auth->user(); |
|
80
|
|
|
if (!$this->connected() || !$user) { |
|
81
|
|
|
return 0; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ($this->ownerId !== null || ($this->ownerId = $this->readOwnerId($user)) > 0) { |
|
85
|
|
|
return $this->ownerId; |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// Try to create a new owner entry for the user. |
|
89
|
|
|
return !$canCreate ? 0 : ($this->ownerId = $this->newOwnerId($user)); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|