-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathprofile.class.php
More file actions
310 lines (271 loc) · 9.79 KB
/
profile.class.php
File metadata and controls
310 lines (271 loc) · 9.79 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
305
306
307
308
309
310
<?php
/**
* -------------------------------------------------------------------------
* Uninstall plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Uninstall.
*
* Uninstall 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.
*
* Uninstall 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 Uninstall. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2015-2023 by Teclib'.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/uninstall
* -------------------------------------------------------------------------
*/
class PluginUninstallProfile extends Profile
{
public const RIGHT_REPLACE = 128;
/**
*
* Get rights matrix for plugin
*
* @return array rights matrix
*/
public function getGeneralRights()
{
return [
[
'itemtype' => 'PluginUninstallProfile',
'label' => PluginUninstallUninstall::getTypeName(),
'field' => "uninstall:profile",
'rights' => [READ => __s('Read'),
UPDATE => __s('Write'),
self::RIGHT_REPLACE => PluginUninstallReplace::getTypeName(),
],
],
];
}
public function showForm($ID, $options = [])
{
$profile = new Profile();
if ($ID) {
$this->getFromDB($ID);
$profile->getFromDB($ID);
} else {
$this->getEmpty();
}
if ($canedit = self::canUpdate()) {
$options['colspan'] = 1;
$options['target'] = $profile->getFormURL();
$this->fields["id"] = $ID;
$this->showFormHeader($options);
}
$rights = $this->getGeneralRights();
$profile->displayRightsChoiceMatrix($rights, ['canedit' => $canedit,
'default_class' => 'tab_bg_2',
]);
if ($canedit) {
$options['candel'] = false;
$this->showFormButtons($options);
}
return true;
}
public static function createFirstAccess($ID)
{
self::addDefaultProfileInfos(
$ID,
['uninstall:profile' => UPDATE | READ | self::RIGHT_REPLACE,
'plugin_uninstall_replace' => 1,
],
true,
);
}
/**
* Init profiles
*
**/
public static function translateARight($old_right)
{
return match ($old_right) {
'' => 0,
'r' => READ,
'w' => UPDATE + READ,
'0', '1' => $old_right,
default => 0,
};
}
/**
* @since 0.85
* Migration rights from old system to the new one for one profile
* @param $profiles_id the profile ID
*/
public static function migrateOneProfile($profiles_id)
{
/** @var DBmysql $DB */
global $DB;
//Cannot launch migration if there's nothing to migrate...
if (!$DB->tableExists('glpi_plugin_uninstall_profiles')) {
return true;
}
foreach (
$DB->request(
[
'FROM' => 'glpi_plugin_uninstall_profiles',
'WHERE' => [
'id' => $profiles_id,
],
],
) as $profile_data
) {
$translatedRight = self::translateARight($profile_data["use"]);
$translatedRight |= self::translateARight($profile_data["replace"]) ? self::RIGHT_REPLACE : 0;
ProfileRight::updateProfileRights($profiles_id, [PluginUninstallProfile::$rightname => $translatedRight]);
}
return null;
}
/**
* Initialize profiles, and migrate it necessary
*/
public static function migrateAllProfiles()
{
/** @var DBmysql $DB */
global $DB;
//Add new rights in glpi_profilerights table
foreach ([PluginUninstallProfile::$rightname] as $field) {
if (!countElementsInTable("glpi_profilerights", ['name' => $field])) {
ProfileRight::addProfileRights([$field]);
}
}
//Migration old rights in new ones
$profiles_it = $DB->request([
'SELECT' => ['id'],
'FROM' => 'glpi_profiles',
]);
foreach ($profiles_it as $prof) {
self::migrateOneProfile($prof['id']);
}
$rights_it = $DB->request([
'FROM' => 'glpi_profilerights',
'WHERE' => [
'profiles_id' => $_SESSION['glpiactiveprofile']['id'],
'name' => ['LIKE', '%plugin_uninstall%'],
],
]);
foreach ($rights_it as $prof) {
$_SESSION['glpiactiveprofile'][$prof['name']] = $prof['rights'];
}
}
public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
{
if ($item instanceof Profile && $item->getField('interface') == 'central') {
return self::createTabEntry(PluginUninstallUninstall::getTypeName(), 0, $item::getType(), PluginUninstallReplace::getIcon());
}
return '';
}
public static function addDefaultProfileInfos($profiles_id, $rights, $drop_existing = false)
{
$profileRight = new ProfileRight();
foreach ($rights as $right => $value) {
if (
countElementsInTable(
'glpi_profilerights',
['profiles_id' => $profiles_id, 'name' => $right],
) && $drop_existing
) {
$profileRight->deleteByCriteria(['profiles_id' => $profiles_id, 'name' => $right]);
}
if (
!countElementsInTable(
'glpi_profilerights',
['profiles_id' => $profiles_id, 'name' => $right],
)
) {
$myright['profiles_id'] = $profiles_id;
$myright['name'] = $right;
$myright['rights'] = $value;
$profileRight->add($myright);
//Add right to the current session
$_SESSION['glpiactiveprofile'][$right] = $value;
}
}
}
public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0)
{
if ($item instanceof Profile) {
$ID = $item->getID();
$prof = new self();
self::addDefaultProfileInfos(
$ID,
[PluginUninstallProfile::$rightname => 0],
);
$prof->showForm($ID);
}
return true;
}
public static function install($migration)
{
/** @var DBmysql $DB */
global $DB;
$default_charset = DBConnection::getDefaultCharset();
$default_collation = DBConnection::getDefaultCollation();
$default_key_sign = DBConnection::getDefaultPrimaryKeySignOption();
// From 0.2 to 1.0.0
$table = 'glpi_plugin_uninstallcomputer_profiles';
if ($DB->tableExists($table)) {
$migration->changeField($table, 'use', 'use', "char", ['value' => '0']);
$migration->migrationOneTable($table);
$DB->update(
$table,
[
'use' => 'r',
],
[
'use' => '1',
],
);
$migration->renameTable($table, 'glpi_plugin_uninstall_profiles');
}
$table = 'glpi_plugin_uninstall_profiles';
// Plugin already installed
if ($DB->tableExists($table)) {
// From 1.0.0 to 1.3.0
if ($DB->fieldExists($table, 'ID')) {
$migration->changeField($table, 'ID', 'id', 'autoincrement');
$migration->changeField($table, 'use', 'use', "varchar(1) DEFAULT ''");
}
// From 1.3.0 to 2.0.0
if (!$DB->fieldExists($table, 'replace')) {
$migration->addField($table, 'replace', "bool");
$migration->migrationOneTable($table);
// UPDATE replace access for current user
$DB->update('glpi_plugin_uninstall_profiles', [
'replace' => 1,
], ['id' => $_SESSION['glpiactiveprofile']['id']]);
}
self::migrateAllProfiles();
$migration->dropTable($table);
} else {
// plugin never installed
$query = "CREATE TABLE `" . $table . "` (
`id` int {$default_key_sign} NOT NULL DEFAULT '0',
`profile` varchar(255) NOT NULL DEFAULT '0',
`use` varchar(1) DEFAULT '',
`replace` tinyint NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;";
$DB->doQuery($query);
self::createFirstAccess($_SESSION['glpiactiveprofile']['id']);
}
return true;
}
public static function uninstall()
{
/** @var DBmysql $DB */
global $DB;
$DB->doQuery("DROP TABLE IF EXISTS `" . getTableForItemType(self::class) . "`");
}
}