hook.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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[] = PluginSinglesignonProvider::renderButton($url, $row['name']);
  18. }
  19. if (!empty($html)) {
  20. echo '<div class="singlesignon-box">';
  21. echo implode(" \n", $html);
  22. echo PluginSinglesignonProvider::renderButton('#', __('GLPI'), 'vsubmit old-login');
  23. echo '</div>';
  24. ?>
  25. <style>
  26. #display-login .singlesignon-box span {
  27. display: inline-block;
  28. margin: 5px;
  29. }
  30. #display-login .singlesignon-box .old-login {
  31. display: none;
  32. }
  33. #boxlogin .singlesignon-box span {
  34. display: block;
  35. }
  36. #boxlogin .singlesignon-box .vsubmit {
  37. width: 100%;
  38. height: 30px;
  39. font-size: 1.3em !important;
  40. text-align: center;
  41. box-sizing: border-box;
  42. }
  43. </style>
  44. <script type="text/javascript">
  45. $(document).ready(function() {
  46. // On click, open a popup
  47. $(document).on("click", ".singlesignon.oauth-login", function(e) {
  48. e.preventDefault();
  49. var url = $(this).attr("href");
  50. var left = ($(window).width() / 2) - (600 / 2);
  51. var top = ($(window).height() / 2) - (800 / 2);
  52. var newWindow = window.open(url, "singlesignon", "width=600,height=800,left=" + left + ",top=" + top);
  53. if (window.focus) {
  54. newWindow.focus();
  55. }
  56. });
  57. var $boxLogin = $('#boxlogin');
  58. var $form = $boxLogin.find('form');
  59. var $boxButtons = $('.singlesignon-box');
  60. // Move the buttons to before form
  61. $boxButtons.prependTo($boxLogin);
  62. $boxButtons.find('span').addClass('login_input');
  63. // Show old form
  64. $(document).on("click", ".singlesignon.old-login", function(e) {
  65. e.preventDefault();
  66. $boxButtons.slideToggle();
  67. $form.slideToggle(function() {
  68. $('#login_name').focus();
  69. });
  70. });
  71. var $line = $('<p />', {
  72. class: 'login_input'
  73. }).prependTo($form);
  74. var $backLogin = $('<label />', {
  75. css: {
  76. cursor: 'pointer'
  77. },
  78. text: "<< " + <?php echo json_encode(__('Back')) ?>,
  79. }).appendTo($line);
  80. $backLogin.on('click', function(e) {
  81. e.preventDefault();
  82. $boxButtons.slideToggle();
  83. $form.slideToggle();
  84. });
  85. $form.hide();
  86. });
  87. </script>
  88. <?php
  89. }
  90. }
  91. function plugin_singlesignon_install() {
  92. /* @var $DB DB */
  93. global $DB;
  94. $currentVersion = '0.0.0';
  95. $default = [];
  96. $current = Config::getConfigurationValues('singlesignon');
  97. if (isset($current['version'])) {
  98. $currentVersion = $current['version'];
  99. }
  100. foreach ($default as $key => $value) {
  101. if (!isset($current[$key])) {
  102. $current[$key] = $value;
  103. }
  104. }
  105. Config::setConfigurationValues('singlesignon', $current);
  106. if (!sso_TableExists("glpi_plugin_singlesignon_providers")) {
  107. $query = "CREATE TABLE `glpi_plugin_singlesignon_providers` (
  108. `id` int(11) NOT NULL auto_increment,
  109. `type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  110. `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  111. `client_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  112. `client_secret` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  113. `scope` varchar(255) COLLATE utf8_unicode_ci NULL,
  114. `extra_options` varchar(255) COLLATE utf8_unicode_ci NULL,
  115. `url_authorize` varchar(255) COLLATE utf8_unicode_ci NULL,
  116. `url_access_token` varchar(255) COLLATE utf8_unicode_ci NULL,
  117. `url_resource_owner_details` varchar(255) COLLATE utf8_unicode_ci NULL,
  118. `is_active` tinyint(1) NOT NULL DEFAULT '0',
  119. `is_deleted` tinyint(1) NOT NULL default '0',
  120. `comment` text COLLATE utf8_unicode_ci,
  121. `date_mod` datetime DEFAULT NULL,
  122. `date_creation` datetime DEFAULT NULL,
  123. PRIMARY KEY (`id`),
  124. KEY `date_mod` (`date_mod`),
  125. KEY `date_creation` (`date_creation`)
  126. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
  127. $DB->query($query) or die("error creating glpi_plugin_singlesignon_providers " . $DB->error());
  128. // $query = "INSERT INTO `glpi_plugin_singlesignon_providers`
  129. // (`id`, `name`, `serial`, `is_deleted`)
  130. // VALUES (1, 'example 1', 'serial 1', 0),
  131. // (2, 'example 2', 'serial 2', 0),
  132. // (3, 'example 3', 'serial 3', 0)";
  133. // $DB->query($query) or die("error populate glpi_plugin_example " . $DB->error());
  134. }
  135. // add display preferences
  136. $query_display_pref = "SELECT id
  137. FROM glpi_displaypreferences
  138. WHERE itemtype = 'PluginSinglesignonProvider'";
  139. $res_display_pref = $DB->query($query_display_pref);
  140. if ($DB->numrows($res_display_pref) == 0) {
  141. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','2','1','0');");
  142. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','3','2','0');");
  143. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','5','4','0');");
  144. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','6','5','0');");
  145. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','10','6','0');");
  146. }
  147. Config::setConfigurationValues('singlesignon', [
  148. 'version' => PLUGIN_SINGLESIGNON_VERSION,
  149. ]);
  150. return true;
  151. }
  152. function plugin_singlesignon_uninstall() {
  153. global $DB;
  154. $config = new Config();
  155. $condition = "`context` LIKE 'singlesignon%'";
  156. if (version_compare(GLPI_VERSION, '9.4', '>=')) {
  157. $condition = [$condition];
  158. }
  159. $rows = $config->find($condition);
  160. foreach ($rows as $id => $row) {
  161. $config->delete(['id' => $id]);
  162. }
  163. // Old version tables
  164. if (sso_TableExists("glpi_plugin_singlesignon_providers")) {
  165. $query = "DROP TABLE `glpi_plugin_singlesignon_providers`";
  166. $DB->query($query) or die("error deleting glpi_plugin_singlesignon_providers");
  167. }
  168. return true;
  169. }