hook.php 5.3 KB

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