Changes to illustrate changes in plugin management and common used solution for rights

- move install/uninstall functions in hook.php
- provide a change_profile to save right in the session
- change haveRight to use data from session



git-svn-id: https://forge.glpi-project.org/svn/example/trunk@70 349b9182-4a13-0410-896f-e5e9767dd1b3
This commit is contained in:
remicollet 2009-03-22 08:42:39 +00:00
parent 79992b49d4
commit 9132ad67ad
2 changed files with 109 additions and 85 deletions

View File

@ -41,6 +41,23 @@ class pluginExample extends CommonDBTM {
} }
}; };
// Hook called on profile change
// Good place to evaluate the user right on this plugin
// And to save it in the session
function plugin_change_profile_example() {
// For example : same right of computer
if (haveRight('computer','w')) {
$_SESSION["glpi_plugin_example_profile"]=array('example'=>'w');
} else if (haveRight('computer','r')) {
$_SESSION["glpi_plugin_example_profile"]=array('example'=>'r');
} else {
unset($_SESSION["glpi_plugin_example_profile"]);
}
}
// Define dropdown relations // Define dropdown relations
function plugin_example_getDatabaseRelations(){ function plugin_example_getDatabaseRelations(){
@ -638,5 +655,64 @@ function plugin_example_addParamFordynamicReport($device_type){
return false; return false;
} }
// Install process for plugin : need to return true if succeeded
function plugin_example_install(){
global $DB;
if (!TableExists("glpi_plugin_example")){
$query="CREATE TABLE `glpi_plugin_example` (
`ID` int(11) NOT NULL auto_increment,
`name` varchar(255) collate utf8_unicode_ci default NULL,
`serial` varchar(255) collate utf8_unicode_ci NOT NULL,
`FK_dropdown` int(11) NOT NULL default '0',
`deleted` smallint(6) NOT NULL default '0',
`is_template` smallint(6) NOT NULL default '0',
`tplname` varchar(255) collate utf8_unicode_ci default NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
";
$DB->query($query) or die("error creating glpi_plugin_example ". $DB->error());
$query="INSERT INTO `glpi_plugin_example` (`ID`, `name`, `serial`, `FK_dropdown`, `deleted`, `is_template`, `tplname`) VALUES
(1, 'example 1', 'serial 1', 1, 0, 0, NULL),
(2, 'example 2', 'serial 2', 2, 0, 0, NULL),
(3, 'example 3', 'serial 3', 1, 0, 0, NULL);";
$DB->query($query) or die("error populate glpi_plugin_example ". $DB->error());
}
if (!TableExists("glpi_dropdown_plugin_example")){
$query="CREATE TABLE `glpi_dropdown_plugin_example` (
`ID` int(11) NOT NULL auto_increment,
`name` varchar(255) collate utf8_unicode_ci default NULL,
`comments` text collate utf8_unicode_ci,
PRIMARY KEY (`ID`),
KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$DB->query($query) or die("error creating glpi_dropdown_plugin_example". $DB->error());
$query="INSERT INTO `glpi_dropdown_plugin_example` (`ID`, `name`, `comments`) VALUES
(1, 'dp 1', 'comment 1'),
(2, 'dp2', 'comment 2');";
$DB->query($query) or die("error populate glpi_dropdown_plugin_example". $DB->error());
}
return true;
}
// Uninstall process for plugin : need to return true if succeeded
function plugin_example_uninstall(){
global $DB;
if (TableExists("glpi_plugin_example")){
$query="DROP TABLE `glpi_plugin_example`;";
$DB->query($query) or die("error creating glpi_plugin_example");
}
if (TableExists("glpi_dropdown_plugin_example")){
$query="DROP TABLE `glpi_dropdown_plugin_example`;";
$DB->query($query) or die("error creating glpi_dropdown_plugin_example");
}
return true;
}
?> ?>

118
setup.php
View File

@ -37,22 +37,39 @@
function plugin_init_example() { function plugin_init_example() {
global $PLUGIN_HOOKS,$LANG,$CFG_GLPI; global $PLUGIN_HOOKS,$LANG,$CFG_GLPI;
// Display a menu entry ? // Params : plugin name - string type - ID - Array of attributes
$PLUGIN_HOOKS['menu_entry']['example'] = true; registerPluginType('example', 'PLUGIN_EXAMPLE_TYPE', 1001, array(
$PLUGIN_HOOKS['submenu_entry']['example']['add'] = 'example.form.php'; 'classname' => 'pluginExample',
$PLUGIN_HOOKS['submenu_entry']['example']["<img src='".$CFG_GLPI["root_doc"]."/pics/menu_showall.png' title='".$LANG['plugin_example']["test"]."' alt='".$LANG['plugin_example']["test"]."'>"] = 'index.php'; 'tablename' => 'glpi_plugin_example',
$PLUGIN_HOOKS['submenu_entry']['example'][$LANG['plugin_example']["test"]] = 'index.php'; 'formpage' => 'example.form.php',
$PLUGIN_HOOKS['submenu_entry']['example']['config'] = 'index.php'; 'searchpage' => 'index.php',
'typename' => 'Example Type',
'deleted_tables' => false,
'template_tables' => false,
'specif_entities_tables' => false,
'recursive_type' => false
));
$PLUGIN_HOOKS["helpdesk_menu_entry"]['example'] = true; // Display a menu entry ?
if (plugin_example_haveTypeRight(PLUGIN_EXAMPLE_TYPE,'r')) { // Right set in change_profile hook
$PLUGIN_HOOKS['menu_entry']['example'] = true;
$PLUGIN_HOOKS['submenu_entry']['example']['add'] = 'example.form.php';
$PLUGIN_HOOKS['submenu_entry']['example']["<img src='".$CFG_GLPI["root_doc"]."/pics/menu_showall.png' title='".$LANG['plugin_example']["test"]."' alt='".$LANG['plugin_example']["test"]."'>"] = 'index.php';
$PLUGIN_HOOKS['submenu_entry']['example'][$LANG['plugin_example']["test"]] = 'index.php';
$PLUGIN_HOOKS['submenu_entry']['example']['config'] = 'index.php';
$PLUGIN_HOOKS["helpdesk_menu_entry"]['example'] = true;
}
// Config page // Config page
$PLUGIN_HOOKS['config_page']['example'] = 'config.php'; if (haveRight('config','w')) {
$PLUGIN_HOOKS['config_page']['example'] = 'config.php';
}
// Init session // Init session
//$PLUGIN_HOOKS['init_session']['example'] = 'plugin_init_session_example'; //$PLUGIN_HOOKS['init_session']['example'] = 'plugin_init_session_example';
// Change profile // Change profile
//$PLUGIN_HOOKS['change_profile']['example'] = 'plugin_change_profile_example'; $PLUGIN_HOOKS['change_profile']['example'] = 'plugin_change_profile_example';
// Change entity // Change entity
//$PLUGIN_HOOKS['change_entity']['example'] = 'plugin_change_entity_example'; //$PLUGIN_HOOKS['change_entity']['example'] = 'plugin_change_entity_example';
@ -107,19 +124,6 @@ function plugin_init_example() {
// Stats // Stats
$PLUGIN_HOOKS['stats']['example'] = array('stat.php'=>'New stat', 'stat.php?other'=>'New stats 2',); $PLUGIN_HOOKS['stats']['example'] = array('stat.php'=>'New stat', 'stat.php?other'=>'New stats 2',);
// Params : plugin name - string type - ID - Array of attributes
registerPluginType('example', 'PLUGIN_EXAMPLE_TYPE', 1001, array(
'classname' => 'pluginExample',
'tablename' => 'glpi_plugin_example',
'formpage' => 'example.form.php',
'searchpage' => 'index.php',
'typename' => 'Example Type',
'deleted_tables' => false,
'template_tables' => false,
'specif_entities_tables' => false,
'recursive_type' => false
));
} }
@ -134,65 +138,6 @@ function plugin_version_example(){
); );
} }
// Install process for plugin : need to return true if succeeded
function plugin_example_install(){
global $DB;
if (!TableExists("glpi_plugin_example")){
$query="CREATE TABLE `glpi_plugin_example` (
`ID` int(11) NOT NULL auto_increment,
`name` varchar(255) collate utf8_unicode_ci default NULL,
`serial` varchar(255) collate utf8_unicode_ci NOT NULL,
`FK_dropdown` int(11) NOT NULL default '0',
`deleted` smallint(6) NOT NULL default '0',
`is_template` smallint(6) NOT NULL default '0',
`tplname` varchar(255) collate utf8_unicode_ci default NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
";
$DB->query($query) or die("error creating glpi_plugin_example ". $DB->error());
$query="INSERT INTO `glpi_plugin_example` (`ID`, `name`, `serial`, `FK_dropdown`, `deleted`, `is_template`, `tplname`) VALUES
(1, 'example 1', 'serial 1', 1, 0, 0, NULL),
(2, 'example 2', 'serial 2', 2, 0, 0, NULL),
(3, 'example 3', 'serial 3', 1, 0, 0, NULL);";
$DB->query($query) or die("error populate glpi_plugin_example ". $DB->error());
}
if (!TableExists("glpi_dropdown_plugin_example")){
$query="CREATE TABLE `glpi_dropdown_plugin_example` (
`ID` int(11) NOT NULL auto_increment,
`name` varchar(255) collate utf8_unicode_ci default NULL,
`comments` text collate utf8_unicode_ci,
PRIMARY KEY (`ID`),
KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$DB->query($query) or die("error creating glpi_dropdown_plugin_example". $DB->error());
$query="INSERT INTO `glpi_dropdown_plugin_example` (`ID`, `name`, `comments`) VALUES
(1, 'dp 1', 'comment 1'),
(2, 'dp2', 'comment 2');";
$DB->query($query) or die("error populate glpi_dropdown_plugin_example". $DB->error());
}
return true;
}
// Uninstall process for plugin : need to return true if succeeded
function plugin_example_uninstall(){
global $DB;
if (TableExists("glpi_plugin_example")){
$query="DROP TABLE `glpi_plugin_example`;";
$DB->query($query) or die("error creating glpi_plugin_example");
}
if (TableExists("glpi_dropdown_plugin_example")){
$query="DROP TABLE `glpi_dropdown_plugin_example`;";
$DB->query($query) or die("error creating glpi_dropdown_plugin_example");
}
return true;
}
// Optional : check prerequisites before install : may print errors or add to message after redirect // Optional : check prerequisites before install : may print errors or add to message after redirect
function plugin_example_check_prerequisites(){ function plugin_example_check_prerequisites(){
@ -220,12 +165,15 @@ function plugin_example_check_config($verbose=false){
// Define rights for the plugin types // Define rights for the plugin types
function plugin_example_haveTypeRight($type,$right){ function plugin_example_haveTypeRight($type,$right){
if (!isset($_SESSION["glpi_plugin_example_profile"])) {
// No right
return false;
}
switch ($type){ switch ($type){
case PLUGIN_EXAMPLE_TYPE : case PLUGIN_EXAMPLE_TYPE :
// 1 - All rights for all users // Evaluate the right from data saved in session by change_profile hook
// return true; return ($right=='r' || $_SESSION["glpi_plugin_example_profile"]=='w');
// 2 - Similarity right : same right of computer
return haveRight("computer",$right);
break; break;
} }
} }