hook.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. function plugin_singlesignon_display_login() {
  3. global $CFG_GLPI;
  4. $signon_provider = new PluginSinglesignonProvider();
  5. $rows = $signon_provider->find('`is_active` = 1');
  6. $html = [];
  7. foreach ($rows as $row) {
  8. $query = [];
  9. if (isset($_REQUEST['redirect'])) {
  10. $query['redirect'] = $_REQUEST['redirect'];
  11. }
  12. $url = PluginSinglesignonProvider::getCallbackUrl($row['id'], $query);
  13. $html[] = '<a href="' . $url . '" class="singlesignon" style="color: #CFCFCF">' .
  14. sprintf(__sso('[ Login with %s ]'), $row['name']) . '</a>';
  15. }
  16. echo implode("<br />\n", $html);
  17. echo '<script type="text/javascript">
  18. $(".singlesignon").on("click", function (e) {
  19. e.preventDefault();
  20. var url = $(this).attr("href");
  21. var left = ($(window).width()/2)-(600/2);
  22. var top = ($(window).height()/2)-(800/2);
  23. var newWindow = window.open(url, "singlesignon", "width=600,height=800,left=" + left + ",top=" + top);
  24. if (window.focus) {
  25. newWindow.focus();
  26. }
  27. });
  28. </script>';
  29. }
  30. function plugin_singlesignon_install() {
  31. /* @var $DB DB */
  32. global $DB;
  33. $currentVersion = '0.0.0';
  34. $default = [
  35. ];
  36. $current = Config::getConfigurationValues('singlesignon');
  37. if (isset($current['version'])) {
  38. $currentVersion = $current['version'];
  39. }
  40. foreach ($default as $key => $value) {
  41. if (!isset($current[$key])) {
  42. $current[$key] = $value;
  43. }
  44. }
  45. Config::setConfigurationValues('singlesignon', $current);
  46. if (!sso_TableExists("glpi_plugin_singlesignon_providers")) {
  47. $query = "CREATE TABLE `glpi_plugin_singlesignon_providers` (
  48. `id` int(11) NOT NULL auto_increment,
  49. `type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  50. `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  51. `client_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  52. `client_secret` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  53. `scope` varchar(255) COLLATE utf8_unicode_ci NULL,
  54. `extra_options` varchar(255) COLLATE utf8_unicode_ci NULL,
  55. `url_authorize` varchar(255) COLLATE utf8_unicode_ci NULL,
  56. `url_access_token` varchar(255) COLLATE utf8_unicode_ci NULL,
  57. `url_resource_owner_details` varchar(255) COLLATE utf8_unicode_ci NULL,
  58. `is_active` tinyint(1) NOT NULL DEFAULT '0',
  59. `is_deleted` tinyint(1) NOT NULL default '0',
  60. `comment` text COLLATE utf8_unicode_ci,
  61. `date_mod` datetime DEFAULT NULL,
  62. `date_creation` datetime DEFAULT NULL,
  63. PRIMARY KEY (`id`),
  64. KEY `date_mod` (`date_mod`),
  65. KEY `date_creation` (`date_creation`)
  66. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
  67. $DB->query($query) or die("error creating glpi_plugin_singlesignon_providers " . $DB->error());
  68. // $query = "INSERT INTO `glpi_plugin_singlesignon_providers`
  69. // (`id`, `name`, `serial`, `is_deleted`)
  70. // VALUES (1, 'example 1', 'serial 1', 0),
  71. // (2, 'example 2', 'serial 2', 0),
  72. // (3, 'example 3', 'serial 3', 0)";
  73. // $DB->query($query) or die("error populate glpi_plugin_example " . $DB->error());
  74. }
  75. // add display preferences
  76. $query_display_pref = "SELECT id
  77. FROM glpi_displaypreferences
  78. WHERE itemtype = 'PluginSinglesignonProvider'";
  79. $res_display_pref = $DB->query($query_display_pref);
  80. if ($DB->numrows($res_display_pref) == 0) {
  81. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','2','1','0');");
  82. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','3','2','0');");
  83. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','5','4','0');");
  84. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','6','5','0');");
  85. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','10','6','0');");
  86. }
  87. Config::setConfigurationValues('singlesignon', [
  88. 'version' => PLUGIN_SINGLESIGNON_VERSION,
  89. ]);
  90. return true;
  91. }
  92. function plugin_singlesignon_uninstall() {
  93. global $DB;
  94. $config = new Config();
  95. $rows = $config->find("`context` LIKE 'singlesignon%'");
  96. foreach ($rows as $id => $row) {
  97. $config->delete(['id' => $id]);
  98. }
  99. // Old version tables
  100. if (sso_TableExists("glpi_plugin_singlesignon_providers")) {
  101. $query = "DROP TABLE `glpi_plugin_singlesignon_providers`";
  102. $DB->query($query) or die("error deleting glpi_plugin_singlesignon_providers");
  103. }
  104. return true;
  105. }