-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.php
181 lines (159 loc) · 5.71 KB
/
hook.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
<?php
/**
* -------------------------------------------------------------------------
* AssetUserHistory plugin for GLPI
* @copyright Copyright (C) 2023 by i-Vertix (https://i-vertix.com)
* @license MIT https://opensource.org/licenses/mit-license.php
* @link https://github.com/i-Vertix/assetuserhistory
* -------------------------------------------------------------------------
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* --------------------------------------------------------------------------
*/
/**
* Plugin install process
*
* @return boolean
* @noinspection PhpUnused
*/
function plugin_assetuserhistory_install(): bool
{
global $DB;
$migration = new Migration(PLUGIN_ASSETUSERHISTORY_VERSION);
// COMPUTER
if (!$DB->tableExists("glpi_plugin_assetuserhistory_history")) {
$query = "CREATE TABLE IF NOT EXISTS `glpi_plugin_assetuserhistory_history` (
`id` int unsigned not null primary key auto_increment,
`users_id` int unsigned not null ,
`assets_id` int unsigned not null ,
`assets_type` varchar(255) not null ,
`assigned` timestamp null,
`revoked` timestamp null
)";
$DB->queryOrDie($query, "table glpi_plugin_assetuserhistory_history created");
}
// WITHOUT STRICT_TRANS_TABLES TRIGGERS DO NOT WORK CORRECTLY
$DB->query("SET SESSION sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION'");
$tablesForTriggers = [
["glpi_computers", "Computer"],
["glpi_monitors", "Monitor"],
["glpi_networkequipments", "NetworkEquipment"],
["glpi_peripherals", "Peripheral"],
["glpi_phones", "Phone"],
["glpi_printers", "Printer"],
];
foreach ($tablesForTriggers as [$table, $type]) {
$name = strtolower($type);
$DB->queryOrDie("
create or replace trigger plugin_assetuserhistory_". $name. "_add
after insert
on " . $table . "
for each row
begin
DECLARE aType varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
SET aType = '" . $type . "';
if (NEW.users_id <> 0 and NEW.users_id is not null and NEW.users_id is not null) then
insert into glpi_plugin_assetuserhistory_history (users_id, assets_id, assets_type, assigned)
values (NEW.users_id, NEW.id, aType, NOW());
end if;
end;
");
$DB->queryOrDie("
create or replace trigger plugin_assetuserhistory_" . $name . "_update
after update
on " . $table . "
for each row
begin
DECLARE assets_type varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
SET assets_type = '" . $type . "';
if (NEW.users_id <> OLD.users_id) then
update glpi_plugin_assetuserhistory_history
set revoked = NOW()
where users_id = OLD.users_id
and assets_id = NEW.id
and assets_type = assets_type
and revoked is null;
if (NEW.users_id <> 0) then
insert into glpi_plugin_assetuserhistory_history (users_id, assets_id, assets_type, assigned)
values (NEW.users_id, NEW.id, assets_type, NOW());
end if;
end if;
end;
");
}
$migration->executeMigration();
return true;
}
/**
* Plugin uninstall process
*
* @return boolean
* @noinspection PhpUnused
*/
function plugin_assetuserhistory_uninstall(): bool
{
global $DB;
if ($DB->tableExists("glpi_plugin_assetuserhistory_history")) {
$query = "DROP TABLE glpi_plugin_assetuserhistory_history;";
$DB->queryOrDie($query, "table glpi_plugin_assetuserhistory_history deleted");
}
$triggerNames = [
"computer",
"monitor",
"networkequipment",
"peripheral",
"phone",
"printer",
];
foreach ($triggerNames as $name) {
$DB->queryOrDie("drop trigger if exists plugin_assetuserhistory_" . $name . "_add;");
$DB->queryOrDie("drop trigger if exists plugin_assetuserhistory_" . $name . "_update;");
}
return true;
}
/**
* @param CommonDBTM $item
* @return void
* @noinspection PhpUnused
*/
function plugin_assetuserhistory_item_purge_asset(CommonDBTM $item): void
{
global $DB;
$stmt = $DB->prepare("delete from glpi_plugin_assetuserhistory_history where assets_type = ? and assets_id = ?");
$type = $item::getType();
$id = $item->getID();
$stmt->bind_param("si", $type, $id);
$stmt->execute();
}
/**
* @param User $item
* @return void
* @noinspection PhpUnused
*/
function plugin_assetuserhistory_item_purge_user(User $item): void
{
global $DB;
$stmt = $DB->prepare("delete from glpi_plugin_assetuserhistory_history where users_id = ?");
$id = $item->getID();
$stmt->bind_param("i", $id);
$stmt->execute();
}