Skip to content

Commit bcdae86

Browse files
cconard96trasher
authored andcommitted
Modernize permission handling
1 parent 9e17e9d commit bcdae86

6 files changed

Lines changed: 99 additions & 59 deletions

File tree

hook.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,8 @@
3737
use GlpiPlugin\Example\Example;
3838
use Dropdown as GlpiDropdown;
3939

40-
// Hook called on profile change
41-
// Good place to evaluate the user right on this plugin
42-
// And to save it in the session
4340
function plugin_change_profile_example() {
44-
// For example : same right of computer
45-
if (Session::haveRight('computer', UPDATE)) {
46-
$_SESSION["glpi_plugin_example_profile"] = ['example' => 'w'];
47-
48-
} else if (Session::haveRight('computer', READ)) {
49-
$_SESSION["glpi_plugin_example_profile"] = ['example' => 'r'];
50-
51-
} else {
52-
unset($_SESSION["glpi_plugin_example_profile"]);
53-
}
41+
// Some logic that runs when the profile is changed
5442
}
5543

5644

@@ -469,10 +457,14 @@ function plugin_example_addParamFordynamicReport($itemtype) {
469457
function plugin_example_install() {
470458
global $DB;
471459

472-
$config = new Config();
473-
$config->setConfigurationValues('plugin:Example', ['configuration' => false]);
460+
$migration = new Migration(PLUGIN_EXAMPLE_VERSION);
461+
Config::setConfigurationValues('plugin:Example', ['configuration' => false]);
462+
463+
// Adds the right(s) to all pre-existing profiles with no access by default
464+
ProfileRight::addProfileRights([Example::$rightname]);
474465

475-
ProfileRight::addProfileRights(['example:read']);
466+
// Grants full access to profiles that can update the Config (super-admins)
467+
$migration->addRight(Example::$rightname, ALLSTANDARDRIGHT, [Config::$rightname => UPDATE]);
476468

477469
$default_charset = DBConnection::getDefaultCharset();
478470
$default_collation = DBConnection::getDefaultCollation();
@@ -571,7 +563,7 @@ function plugin_example_uninstall() {
571563
$config = new Config();
572564
$config->deleteConfigurationValues('plugin:Example', ['configuration' => false]);
573565

574-
ProfileRight::deleteProfileRights(['example:read']);
566+
ProfileRight::deleteProfileRights([Example::$rightname]);
575567

576568
$notif = new Notification();
577569
$options = ['itemtype' => 'Ticket',

report.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
define('GLPI_ROOT', '../..');
4141
include (GLPI_ROOT . "/inc/includes.php");
4242

43-
Session::checkRight("config", "w");
43+
Session::checkRight(Config::$rightname, UPDATE);
4444

4545
Html::header("TITRE", $_SERVER['PHP_SELF'], "plugins");
4646

setup.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ function plugin_init_example() {
8585
}
8686
}
8787
// Display a menu entry ?
88-
$_SESSION["glpi_plugin_example_profile"]['example'] = 'w';
89-
if (isset($_SESSION["glpi_plugin_example_profile"])) { // Right set in change_profile hook
88+
Plugin::registerClass(\GlpiPlugin\Example\Profile::class, ['addtabon' => ['Profile']]);
89+
if (Example::canView()) { // Right set in change_profile hook
9090
$PLUGIN_HOOKS['menu_toadd']['example'] = ['plugins' => Example::class,
9191
'tools' => Example::class];
9292

src/Example.php

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,43 +46,17 @@
4646
class Example extends CommonDBTM {
4747

4848
static $tags = '[EXAMPLE_ID]';
49+
public static $rightname = 'plugin_example';
4950

5051
// Should return the localized name of the type
5152
static function getTypeName($nb = 0) {
5253
return 'Example Type';
5354
}
5455

55-
56-
static function canCreate() {
57-
58-
if (isset($_SESSION["glpi_plugin_example_profile"])) {
59-
return ($_SESSION["glpi_plugin_example_profile"]['example'] == 'w');
60-
}
61-
return false;
62-
}
63-
64-
65-
static function canView() {
66-
67-
if (isset($_SESSION["glpi_plugin_example_profile"])) {
68-
return ($_SESSION["glpi_plugin_example_profile"]['example'] == 'w'
69-
|| $_SESSION["glpi_plugin_example_profile"]['example'] == 'r');
70-
}
71-
return false;
72-
}
73-
74-
75-
/**
76-
* @see CommonGLPI::getMenuName()
77-
**/
7856
static function getMenuName() {
7957
return __('Example plugin');
8058
}
8159

82-
83-
/**
84-
* @see CommonGLPI::getAdditionalMenuLinks()
85-
**/
8660
static function getAdditionalMenuLinks() {
8761
global $CFG_GLPI;
8862
$links = [];
@@ -406,11 +380,6 @@ static function getHistoryEntry($data) {
406380

407381
//////////////////////////////
408382
////// SPECIFIC MODIF MASSIVE FUNCTIONS ///////
409-
/**
410-
* @since version 0.85
411-
*
412-
* @see CommonDBTM::getSpecificMassiveActions()
413-
**/
414383
function getSpecificMassiveActions($checkitem = null) {
415384

416385
$actions = parent::getSpecificMassiveActions($checkitem);
@@ -423,12 +392,6 @@ function getSpecificMassiveActions($checkitem = null) {
423392
return $actions;
424393
}
425394

426-
427-
/**
428-
* @since version 0.85
429-
*
430-
* @see CommonDBTM::showMassiveActionsSubForm()
431-
**/
432395
static function showMassiveActionsSubForm(MassiveAction $ma) {
433396

434397
switch ($ma->getAction()) {

src/Profile.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/**
4+
* -------------------------------------------------------------------------
5+
* Example plugin for GLPI
6+
* -------------------------------------------------------------------------
7+
*
8+
* LICENSE
9+
*
10+
* This file is part of Example.
11+
*
12+
* Example is free software; you can redistribute it and/or modify
13+
* it under the terms of the GNU General Public License as published by
14+
* the Free Software Foundation; either version 2 of the License, or
15+
* (at your option) any later version.
16+
*
17+
* Example is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU General Public License
23+
* along with Example. If not, see <http://www.gnu.org/licenses/>.
24+
* -------------------------------------------------------------------------
25+
* @copyright Copyright (C) 2006-2022 by Example plugin team.
26+
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
27+
* @link https://github.com/pluginsGLPI/example
28+
* -------------------------------------------------------------------------
29+
*/
30+
31+
namespace GlpiPlugin\Example;
32+
33+
use CommonGLPI;
34+
use Html;
35+
use Session;
36+
37+
final class Profile extends \Profile
38+
{
39+
public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
40+
{
41+
return __('Example plugin');
42+
}
43+
44+
public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0)
45+
{
46+
$profile = new self();
47+
$profile->showFormExample($item->getID());
48+
}
49+
50+
public function showFormExample(int $profiles_id): void
51+
{
52+
if (!$this->can($profiles_id, READ)) {
53+
return;
54+
}
55+
56+
echo "<div class='spaced'>";
57+
58+
$can_edit = Session::haveRight(self::$rightname, UPDATE);
59+
if ($can_edit) {
60+
echo "<form method='post' action='" . htmlspecialchars(self::getFormURL()) . "'>";
61+
}
62+
63+
$matrix_options = [
64+
'canedit' => $can_edit,
65+
];
66+
$rights = [
67+
[
68+
'itemtype' => Example::class,
69+
'label' => Example::getTypeName(Session::getPluralNumber()),
70+
'field' => Example::$rightname
71+
]
72+
];
73+
$matrix_options['title'] = self::getTypeName(1);
74+
$this->displayRightsChoiceMatrix($rights, $matrix_options);
75+
76+
if ($can_edit) {
77+
echo "<div class='text-center'>";
78+
echo Html::hidden('id', ['value' => $profiles_id]);
79+
echo Html::submit(_sx('button', 'Save'), ['name' => 'update']);
80+
echo "</div>\n";
81+
Html::closeForm();
82+
}
83+
echo '</div>';
84+
}
85+
}

stat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
define('GLPI_ROOT', '../..');
4141
include (GLPI_ROOT . "/inc/includes.php");
4242

43-
Session::checkRight("config", "w");
43+
Session::checkRight(Config::$rightname, UPDATE);
4444

4545
Html::header("TITLE", $_SERVER['PHP_SELF'], "plugins");
4646

0 commit comments

Comments
 (0)